Commit | Line | Data |
---|---|---|
b3b94faa DT |
1 | /* |
2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
3a8a9a10 | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
b3b94faa DT |
4 | * |
5 | * This copyrighted material is made available to anyone wishing to use, | |
6 | * modify, copy, or redistribute it subject to the terms and conditions | |
e9fc2aa0 | 7 | * of the GNU General Public License version 2. |
b3b94faa DT |
8 | */ |
9 | ||
10 | #include <linux/sched.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/spinlock.h> | |
13 | #include <linux/completion.h> | |
14 | #include <linux/buffer_head.h> | |
15 | #include <linux/posix_acl.h> | |
16 | #include <linux/sort.h> | |
5c676f6d | 17 | #include <linux/gfs2_ondisk.h> |
71b86f56 | 18 | #include <linux/crc32.h> |
7d308590 | 19 | #include <linux/lm_interface.h> |
fcb47e0b | 20 | #include <linux/security.h> |
b3b94faa DT |
21 | |
22 | #include "gfs2.h" | |
5c676f6d | 23 | #include "incore.h" |
b3b94faa DT |
24 | #include "acl.h" |
25 | #include "bmap.h" | |
26 | #include "dir.h" | |
27 | #include "eattr.h" | |
28 | #include "glock.h" | |
29 | #include "glops.h" | |
30 | #include "inode.h" | |
31 | #include "log.h" | |
32 | #include "meta_io.h" | |
33 | #include "ops_address.h" | |
34 | #include "ops_file.h" | |
35 | #include "ops_inode.h" | |
36 | #include "quota.h" | |
37 | #include "rgrp.h" | |
38 | #include "trans.h" | |
5c676f6d | 39 | #include "util.h" |
b3b94faa DT |
40 | |
41 | /** | |
4340fe62 | 42 | * gfs2_inode_attr_in - Copy attributes from the dinode into the VFS inode |
b3b94faa DT |
43 | * @ip: The GFS2 inode (with embedded disk inode data) |
44 | * @inode: The Linux VFS inode | |
45 | * | |
46 | */ | |
47 | ||
4340fe62 | 48 | void gfs2_inode_attr_in(struct gfs2_inode *ip) |
b3b94faa | 49 | { |
4340fe62 | 50 | struct inode *inode = &ip->i_inode; |
3ca68df6 | 51 | struct gfs2_dinode_host *di = &ip->i_di; |
4340fe62 SW |
52 | |
53 | inode->i_ino = ip->i_num.no_addr; | |
c2668711 SW |
54 | inode->i_nlink = di->di_nlink; |
55 | inode->i_uid = di->di_uid; | |
56 | inode->i_gid = di->di_gid; | |
57 | i_size_write(inode, di->di_size); | |
58 | inode->i_atime.tv_sec = di->di_atime; | |
59 | inode->i_mtime.tv_sec = di->di_mtime; | |
60 | inode->i_ctime.tv_sec = di->di_ctime; | |
b3b94faa DT |
61 | inode->i_atime.tv_nsec = 0; |
62 | inode->i_mtime.tv_nsec = 0; | |
63 | inode->i_ctime.tv_nsec = 0; | |
c2668711 | 64 | inode->i_blocks = di->di_blocks << |
feaa7bba | 65 | (GFS2_SB(inode)->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT); |
b3b94faa | 66 | |
c2668711 | 67 | if (di->di_flags & GFS2_DIF_IMMUTABLE) |
b3b94faa DT |
68 | inode->i_flags |= S_IMMUTABLE; |
69 | else | |
70 | inode->i_flags &= ~S_IMMUTABLE; | |
71 | ||
c2668711 | 72 | if (di->di_flags & GFS2_DIF_APPENDONLY) |
b3b94faa DT |
73 | inode->i_flags |= S_APPEND; |
74 | else | |
75 | inode->i_flags &= ~S_APPEND; | |
76 | } | |
77 | ||
b3b94faa DT |
78 | /** |
79 | * gfs2_inode_attr_out - Copy attributes from VFS inode into the dinode | |
80 | * @ip: The GFS2 inode | |
81 | * | |
82 | * Only copy out the attributes that we want the VFS layer | |
83 | * to be able to modify. | |
84 | */ | |
85 | ||
86 | void gfs2_inode_attr_out(struct gfs2_inode *ip) | |
87 | { | |
feaa7bba | 88 | struct inode *inode = &ip->i_inode; |
3ca68df6 | 89 | struct gfs2_dinode_host *di = &ip->i_di; |
75d3b817 SW |
90 | di->di_uid = inode->i_uid; |
91 | di->di_gid = inode->i_gid; | |
92 | di->di_atime = inode->i_atime.tv_sec; | |
93 | di->di_mtime = inode->i_mtime.tv_sec; | |
94 | di->di_ctime = inode->i_ctime.tv_sec; | |
b3b94faa DT |
95 | } |
96 | ||
feaa7bba SW |
97 | static int iget_test(struct inode *inode, void *opaque) |
98 | { | |
99 | struct gfs2_inode *ip = GFS2_I(inode); | |
629a21e7 | 100 | struct gfs2_inum_host *inum = opaque; |
feaa7bba SW |
101 | |
102 | if (ip && ip->i_num.no_addr == inum->no_addr) | |
103 | return 1; | |
b3b94faa | 104 | |
feaa7bba SW |
105 | return 0; |
106 | } | |
107 | ||
108 | static int iget_set(struct inode *inode, void *opaque) | |
b3b94faa | 109 | { |
feaa7bba | 110 | struct gfs2_inode *ip = GFS2_I(inode); |
629a21e7 | 111 | struct gfs2_inum_host *inum = opaque; |
b3b94faa | 112 | |
feaa7bba SW |
113 | ip->i_num = *inum; |
114 | return 0; | |
115 | } | |
b3b94faa | 116 | |
629a21e7 | 117 | struct inode *gfs2_ilookup(struct super_block *sb, struct gfs2_inum_host *inum) |
feaa7bba SW |
118 | { |
119 | return ilookup5(sb, (unsigned long)inum->no_formal_ino, | |
120 | iget_test, inum); | |
121 | } | |
b3b94faa | 122 | |
629a21e7 | 123 | static struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum_host *inum) |
feaa7bba SW |
124 | { |
125 | return iget5_locked(sb, (unsigned long)inum->no_formal_ino, | |
126 | iget_test, iget_set, inum); | |
b3b94faa DT |
127 | } |
128 | ||
129 | /** | |
feaa7bba SW |
130 | * gfs2_inode_lookup - Lookup an inode |
131 | * @sb: The super block | |
132 | * @inum: The inode number | |
133 | * @type: The type of the inode | |
b3b94faa | 134 | * |
feaa7bba | 135 | * Returns: A VFS inode, or an error |
b3b94faa DT |
136 | */ |
137 | ||
629a21e7 | 138 | struct inode *gfs2_inode_lookup(struct super_block *sb, struct gfs2_inum_host *inum, unsigned int type) |
b3b94faa | 139 | { |
feaa7bba SW |
140 | struct inode *inode = gfs2_iget(sb, inum); |
141 | struct gfs2_inode *ip = GFS2_I(inode); | |
142 | struct gfs2_glock *io_gl; | |
143 | int error; | |
b3b94faa | 144 | |
26d83ded SW |
145 | if (!inode) |
146 | return ERR_PTR(-ENOBUFS); | |
147 | ||
feaa7bba SW |
148 | if (inode->i_state & I_NEW) { |
149 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
150 | umode_t mode = DT2IF(type); | |
bba9dfd8 | 151 | inode->i_private = ip; |
feaa7bba SW |
152 | inode->i_mode = mode; |
153 | ||
154 | if (S_ISREG(mode)) { | |
155 | inode->i_op = &gfs2_file_iops; | |
156 | inode->i_fop = &gfs2_file_fops; | |
157 | inode->i_mapping->a_ops = &gfs2_file_aops; | |
158 | } else if (S_ISDIR(mode)) { | |
159 | inode->i_op = &gfs2_dir_iops; | |
160 | inode->i_fop = &gfs2_dir_fops; | |
161 | } else if (S_ISLNK(mode)) { | |
162 | inode->i_op = &gfs2_symlink_iops; | |
163 | } else { | |
164 | inode->i_op = &gfs2_dev_iops; | |
b3b94faa | 165 | } |
b3b94faa | 166 | |
feaa7bba SW |
167 | error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl); |
168 | if (unlikely(error)) | |
169 | goto fail; | |
170 | ip->i_gl->gl_object = ip; | |
b3b94faa | 171 | |
feaa7bba SW |
172 | error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops, CREATE, &io_gl); |
173 | if (unlikely(error)) | |
174 | goto fail_put; | |
b3b94faa | 175 | |
feaa7bba SW |
176 | ip->i_vn = ip->i_gl->gl_vn - 1; |
177 | error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh); | |
178 | if (unlikely(error)) | |
179 | goto fail_iopen; | |
b3b94faa | 180 | |
feaa7bba SW |
181 | gfs2_glock_put(io_gl); |
182 | unlock_new_inode(inode); | |
183 | } | |
b3b94faa DT |
184 | |
185 | return inode; | |
feaa7bba SW |
186 | fail_iopen: |
187 | gfs2_glock_put(io_gl); | |
188 | fail_put: | |
189 | ip->i_gl->gl_object = NULL; | |
190 | gfs2_glock_put(ip->i_gl); | |
191 | fail: | |
192 | iput(inode); | |
193 | return ERR_PTR(error); | |
b3b94faa DT |
194 | } |
195 | ||
af339c02 | 196 | static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf) |
ea744d01 SW |
197 | { |
198 | struct gfs2_dinode_host *di = &ip->i_di; | |
199 | const struct gfs2_dinode *str = buf; | |
200 | ||
af339c02 SW |
201 | if (ip->i_num.no_addr != be64_to_cpu(str->di_num.no_addr)) { |
202 | if (gfs2_consist_inode(ip)) | |
203 | gfs2_dinode_print(ip); | |
204 | return -EIO; | |
205 | } | |
206 | if (ip->i_num.no_formal_ino != be64_to_cpu(str->di_num.no_formal_ino)) | |
207 | return -ESTALE; | |
ea744d01 | 208 | |
b60623c2 | 209 | ip->i_inode.i_mode = be32_to_cpu(str->di_mode); |
e7f14f4d | 210 | ip->i_inode.i_rdev = 0; |
b60623c2 | 211 | switch (ip->i_inode.i_mode & S_IFMT) { |
e7f14f4d SW |
212 | case S_IFBLK: |
213 | case S_IFCHR: | |
214 | ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major), | |
215 | be32_to_cpu(str->di_minor)); | |
216 | break; | |
217 | }; | |
218 | ||
ea744d01 SW |
219 | di->di_uid = be32_to_cpu(str->di_uid); |
220 | di->di_gid = be32_to_cpu(str->di_gid); | |
221 | di->di_nlink = be32_to_cpu(str->di_nlink); | |
222 | di->di_size = be64_to_cpu(str->di_size); | |
223 | di->di_blocks = be64_to_cpu(str->di_blocks); | |
224 | di->di_atime = be64_to_cpu(str->di_atime); | |
225 | di->di_mtime = be64_to_cpu(str->di_mtime); | |
226 | di->di_ctime = be64_to_cpu(str->di_ctime); | |
ea744d01 SW |
227 | |
228 | di->di_goal_meta = be64_to_cpu(str->di_goal_meta); | |
229 | di->di_goal_data = be64_to_cpu(str->di_goal_data); | |
230 | di->di_generation = be64_to_cpu(str->di_generation); | |
231 | ||
232 | di->di_flags = be32_to_cpu(str->di_flags); | |
233 | di->di_payload_format = be32_to_cpu(str->di_payload_format); | |
234 | di->di_height = be16_to_cpu(str->di_height); | |
235 | ||
236 | di->di_depth = be16_to_cpu(str->di_depth); | |
237 | di->di_entries = be32_to_cpu(str->di_entries); | |
238 | ||
239 | di->di_eattr = be64_to_cpu(str->di_eattr); | |
af339c02 | 240 | return 0; |
ea744d01 SW |
241 | } |
242 | ||
b3b94faa DT |
243 | /** |
244 | * gfs2_inode_refresh - Refresh the incore copy of the dinode | |
245 | * @ip: The GFS2 inode | |
246 | * | |
247 | * Returns: errno | |
248 | */ | |
249 | ||
250 | int gfs2_inode_refresh(struct gfs2_inode *ip) | |
251 | { | |
252 | struct buffer_head *dibh; | |
253 | int error; | |
254 | ||
255 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
256 | if (error) | |
257 | return error; | |
258 | ||
feaa7bba | 259 | if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) { |
b3b94faa DT |
260 | brelse(dibh); |
261 | return -EIO; | |
262 | } | |
263 | ||
af339c02 | 264 | error = gfs2_dinode_in(ip, dibh->b_data); |
b3b94faa | 265 | brelse(dibh); |
b3b94faa DT |
266 | ip->i_vn = ip->i_gl->gl_vn; |
267 | ||
af339c02 | 268 | return error; |
b3b94faa DT |
269 | } |
270 | ||
feaa7bba | 271 | int gfs2_dinode_dealloc(struct gfs2_inode *ip) |
b3b94faa | 272 | { |
feaa7bba | 273 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
274 | struct gfs2_alloc *al; |
275 | struct gfs2_rgrpd *rgd; | |
276 | int error; | |
277 | ||
278 | if (ip->i_di.di_blocks != 1) { | |
279 | if (gfs2_consist_inode(ip)) | |
4cc14f0b | 280 | gfs2_dinode_print(ip); |
b3b94faa DT |
281 | return -EIO; |
282 | } | |
283 | ||
284 | al = gfs2_alloc_get(ip); | |
285 | ||
286 | error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); | |
287 | if (error) | |
288 | goto out; | |
289 | ||
290 | error = gfs2_rindex_hold(sdp, &al->al_ri_gh); | |
291 | if (error) | |
292 | goto out_qs; | |
293 | ||
294 | rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr); | |
295 | if (!rgd) { | |
296 | gfs2_consist_inode(ip); | |
297 | error = -EIO; | |
298 | goto out_rindex_relse; | |
299 | } | |
300 | ||
301 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, | |
302 | &al->al_rgd_gh); | |
303 | if (error) | |
304 | goto out_rindex_relse; | |
305 | ||
420b9e5e | 306 | error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1); |
b3b94faa DT |
307 | if (error) |
308 | goto out_rg_gunlock; | |
309 | ||
310 | gfs2_trans_add_gl(ip->i_gl); | |
311 | ||
312 | gfs2_free_di(rgd, ip); | |
313 | ||
b3b94faa DT |
314 | gfs2_trans_end(sdp); |
315 | clear_bit(GLF_STICKY, &ip->i_gl->gl_flags); | |
316 | ||
feaa7bba | 317 | out_rg_gunlock: |
b3b94faa | 318 | gfs2_glock_dq_uninit(&al->al_rgd_gh); |
feaa7bba | 319 | out_rindex_relse: |
b3b94faa | 320 | gfs2_glock_dq_uninit(&al->al_ri_gh); |
feaa7bba | 321 | out_qs: |
b3b94faa | 322 | gfs2_quota_unhold(ip); |
36327521 | 323 | out: |
feaa7bba | 324 | gfs2_alloc_put(ip); |
b3b94faa DT |
325 | return error; |
326 | } | |
327 | ||
b3b94faa DT |
328 | /** |
329 | * gfs2_change_nlink - Change nlink count on inode | |
330 | * @ip: The GFS2 inode | |
331 | * @diff: The change in the nlink count required | |
332 | * | |
333 | * Returns: errno | |
334 | */ | |
335 | ||
336 | int gfs2_change_nlink(struct gfs2_inode *ip, int diff) | |
337 | { | |
feaa7bba | 338 | struct gfs2_sbd *sdp = ip->i_inode.i_sb->s_fs_info; |
b3b94faa | 339 | struct buffer_head *dibh; |
cd915493 | 340 | u32 nlink; |
b3b94faa DT |
341 | int error; |
342 | ||
29937ac6 | 343 | BUG_ON(ip->i_di.di_nlink != ip->i_inode.i_nlink); |
b3b94faa DT |
344 | nlink = ip->i_di.di_nlink + diff; |
345 | ||
346 | /* If we are reducing the nlink count, but the new value ends up being | |
347 | bigger than the old one, we must have underflowed. */ | |
348 | if (diff < 0 && nlink > ip->i_di.di_nlink) { | |
349 | if (gfs2_consist_inode(ip)) | |
4cc14f0b | 350 | gfs2_dinode_print(ip); |
b3b94faa DT |
351 | return -EIO; |
352 | } | |
353 | ||
354 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
355 | if (error) | |
356 | return error; | |
357 | ||
358 | ip->i_di.di_nlink = nlink; | |
359 | ip->i_di.di_ctime = get_seconds(); | |
29937ac6 | 360 | ip->i_inode.i_nlink = nlink; |
b3b94faa | 361 | |
d4e9c4c3 | 362 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
539e5d6b | 363 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa | 364 | brelse(dibh); |
feaa7bba | 365 | mark_inode_dirty(&ip->i_inode); |
b3b94faa | 366 | |
feaa7bba SW |
367 | if (ip->i_di.di_nlink == 0) { |
368 | struct gfs2_rgrpd *rgd; | |
369 | struct gfs2_holder ri_gh, rg_gh; | |
370 | ||
371 | error = gfs2_rindex_hold(sdp, &ri_gh); | |
372 | if (error) | |
373 | goto out; | |
374 | error = -EIO; | |
375 | rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr); | |
376 | if (!rgd) | |
377 | goto out_norgrp; | |
378 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh); | |
379 | if (error) | |
380 | goto out_norgrp; | |
381 | ||
f92a0b6f | 382 | clear_nlink(&ip->i_inode); |
feaa7bba SW |
383 | gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */ |
384 | gfs2_glock_dq_uninit(&rg_gh); | |
385 | out_norgrp: | |
386 | gfs2_glock_dq_uninit(&ri_gh); | |
387 | } | |
388 | out: | |
389 | return error; | |
b3b94faa DT |
390 | } |
391 | ||
c752666c SW |
392 | struct inode *gfs2_lookup_simple(struct inode *dip, const char *name) |
393 | { | |
394 | struct qstr qstr; | |
71b86f56 | 395 | gfs2_str2qstr(&qstr, name); |
c752666c SW |
396 | return gfs2_lookupi(dip, &qstr, 1, NULL); |
397 | } | |
398 | ||
399 | ||
b3b94faa DT |
400 | /** |
401 | * gfs2_lookupi - Look up a filename in a directory and return its inode | |
402 | * @d_gh: An initialized holder for the directory glock | |
403 | * @name: The name of the inode to look for | |
404 | * @is_root: If 1, ignore the caller's permissions | |
405 | * @i_gh: An uninitialized holder for the new inode glock | |
406 | * | |
407 | * There will always be a vnode (Linux VFS inode) for the d_gh inode unless | |
408 | * @is_root is true. | |
409 | * | |
410 | * Returns: errno | |
411 | */ | |
412 | ||
feaa7bba SW |
413 | struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name, |
414 | int is_root, struct nameidata *nd) | |
b3b94faa | 415 | { |
c9fd4307 | 416 | struct super_block *sb = dir->i_sb; |
feaa7bba | 417 | struct gfs2_inode *dip = GFS2_I(dir); |
b3b94faa | 418 | struct gfs2_holder d_gh; |
629a21e7 | 419 | struct gfs2_inum_host inum; |
b3b94faa | 420 | unsigned int type; |
7359a19c | 421 | int error = 0; |
c752666c | 422 | struct inode *inode = NULL; |
b3b94faa DT |
423 | |
424 | if (!name->len || name->len > GFS2_FNAMESIZE) | |
c752666c | 425 | return ERR_PTR(-ENAMETOOLONG); |
b3b94faa | 426 | |
c752666c SW |
427 | if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) || |
428 | (name->len == 2 && memcmp(name->name, "..", 2) == 0 && | |
429 | dir == sb->s_root->d_inode)) { | |
320dd101 SW |
430 | igrab(dir); |
431 | return dir; | |
b3b94faa DT |
432 | } |
433 | ||
434 | error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh); | |
435 | if (error) | |
c752666c | 436 | return ERR_PTR(error); |
b3b94faa DT |
437 | |
438 | if (!is_root) { | |
faf450ef | 439 | error = permission(dir, MAY_EXEC, NULL); |
b3b94faa DT |
440 | if (error) |
441 | goto out; | |
442 | } | |
443 | ||
c752666c | 444 | error = gfs2_dir_search(dir, name, &inum, &type); |
b3b94faa DT |
445 | if (error) |
446 | goto out; | |
447 | ||
feaa7bba | 448 | inode = gfs2_inode_lookup(sb, &inum, type); |
b3b94faa | 449 | |
7359a19c | 450 | out: |
b3b94faa | 451 | gfs2_glock_dq_uninit(&d_gh); |
c752666c SW |
452 | if (error == -ENOENT) |
453 | return NULL; | |
feaa7bba | 454 | return inode; |
b3b94faa DT |
455 | } |
456 | ||
cd915493 | 457 | static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino) |
b3b94faa | 458 | { |
feaa7bba | 459 | struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode); |
b3b94faa | 460 | struct buffer_head *bh; |
e6972647 | 461 | struct gfs2_inum_range_host ir; |
b3b94faa DT |
462 | int error; |
463 | ||
464 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); | |
465 | if (error) | |
466 | return error; | |
f55ab26a | 467 | mutex_lock(&sdp->sd_inum_mutex); |
b3b94faa DT |
468 | |
469 | error = gfs2_meta_inode_buffer(ip, &bh); | |
470 | if (error) { | |
f55ab26a | 471 | mutex_unlock(&sdp->sd_inum_mutex); |
b3b94faa DT |
472 | gfs2_trans_end(sdp); |
473 | return error; | |
474 | } | |
475 | ||
476 | gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode)); | |
477 | ||
478 | if (ir.ir_length) { | |
479 | *formal_ino = ir.ir_start++; | |
480 | ir.ir_length--; | |
d4e9c4c3 | 481 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
b3b94faa DT |
482 | gfs2_inum_range_out(&ir, |
483 | bh->b_data + sizeof(struct gfs2_dinode)); | |
484 | brelse(bh); | |
f55ab26a | 485 | mutex_unlock(&sdp->sd_inum_mutex); |
b3b94faa DT |
486 | gfs2_trans_end(sdp); |
487 | return 0; | |
488 | } | |
489 | ||
490 | brelse(bh); | |
491 | ||
f55ab26a | 492 | mutex_unlock(&sdp->sd_inum_mutex); |
b3b94faa DT |
493 | gfs2_trans_end(sdp); |
494 | ||
495 | return 1; | |
496 | } | |
497 | ||
cd915493 | 498 | static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino) |
b3b94faa | 499 | { |
feaa7bba SW |
500 | struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode); |
501 | struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode); | |
b3b94faa DT |
502 | struct gfs2_holder gh; |
503 | struct buffer_head *bh; | |
e6972647 | 504 | struct gfs2_inum_range_host ir; |
b3b94faa DT |
505 | int error; |
506 | ||
507 | error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); | |
508 | if (error) | |
509 | return error; | |
510 | ||
511 | error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0); | |
512 | if (error) | |
513 | goto out; | |
f55ab26a | 514 | mutex_lock(&sdp->sd_inum_mutex); |
b3b94faa DT |
515 | |
516 | error = gfs2_meta_inode_buffer(ip, &bh); | |
517 | if (error) | |
518 | goto out_end_trans; | |
907b9bce | 519 | |
b3b94faa DT |
520 | gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode)); |
521 | ||
522 | if (!ir.ir_length) { | |
523 | struct buffer_head *m_bh; | |
cd915493 | 524 | u64 x, y; |
b44b84d7 | 525 | __be64 z; |
b3b94faa DT |
526 | |
527 | error = gfs2_meta_inode_buffer(m_ip, &m_bh); | |
528 | if (error) | |
529 | goto out_brelse; | |
530 | ||
b44b84d7 AV |
531 | z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)); |
532 | x = y = be64_to_cpu(z); | |
b3b94faa DT |
533 | ir.ir_start = x; |
534 | ir.ir_length = GFS2_INUM_QUANTUM; | |
535 | x += GFS2_INUM_QUANTUM; | |
536 | if (x < y) | |
537 | gfs2_consist_inode(m_ip); | |
b44b84d7 | 538 | z = cpu_to_be64(x); |
d4e9c4c3 | 539 | gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1); |
b44b84d7 | 540 | *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z; |
b3b94faa DT |
541 | |
542 | brelse(m_bh); | |
543 | } | |
544 | ||
545 | *formal_ino = ir.ir_start++; | |
546 | ir.ir_length--; | |
547 | ||
d4e9c4c3 | 548 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
b3b94faa DT |
549 | gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode)); |
550 | ||
420b9e5e | 551 | out_brelse: |
b3b94faa | 552 | brelse(bh); |
420b9e5e | 553 | out_end_trans: |
f55ab26a | 554 | mutex_unlock(&sdp->sd_inum_mutex); |
b3b94faa | 555 | gfs2_trans_end(sdp); |
420b9e5e | 556 | out: |
b3b94faa | 557 | gfs2_glock_dq_uninit(&gh); |
b3b94faa DT |
558 | return error; |
559 | } | |
560 | ||
cd915493 | 561 | static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum) |
b3b94faa DT |
562 | { |
563 | int error; | |
564 | ||
565 | error = pick_formal_ino_1(sdp, inum); | |
566 | if (error <= 0) | |
567 | return error; | |
568 | ||
569 | error = pick_formal_ino_2(sdp, inum); | |
570 | ||
571 | return error; | |
572 | } | |
573 | ||
574 | /** | |
575 | * create_ok - OK to create a new on-disk inode here? | |
576 | * @dip: Directory in which dinode is to be created | |
577 | * @name: Name of new dinode | |
578 | * @mode: | |
579 | * | |
580 | * Returns: errno | |
581 | */ | |
582 | ||
feaa7bba | 583 | static int create_ok(struct gfs2_inode *dip, const struct qstr *name, |
b3b94faa DT |
584 | unsigned int mode) |
585 | { | |
586 | int error; | |
587 | ||
faf450ef | 588 | error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL); |
b3b94faa DT |
589 | if (error) |
590 | return error; | |
591 | ||
592 | /* Don't create entries in an unlinked directory */ | |
593 | if (!dip->i_di.di_nlink) | |
594 | return -EPERM; | |
595 | ||
feaa7bba | 596 | error = gfs2_dir_search(&dip->i_inode, name, NULL, NULL); |
b3b94faa DT |
597 | switch (error) { |
598 | case -ENOENT: | |
599 | error = 0; | |
600 | break; | |
601 | case 0: | |
602 | return -EEXIST; | |
603 | default: | |
604 | return error; | |
605 | } | |
606 | ||
cd915493 | 607 | if (dip->i_di.di_entries == (u32)-1) |
b3b94faa | 608 | return -EFBIG; |
cd915493 | 609 | if (S_ISDIR(mode) && dip->i_di.di_nlink == (u32)-1) |
b3b94faa DT |
610 | return -EMLINK; |
611 | ||
612 | return 0; | |
613 | } | |
614 | ||
615 | static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode, | |
616 | unsigned int *uid, unsigned int *gid) | |
617 | { | |
feaa7bba | 618 | if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir && |
b60623c2 | 619 | (dip->i_inode.i_mode & S_ISUID) && dip->i_di.di_uid) { |
b3b94faa DT |
620 | if (S_ISDIR(*mode)) |
621 | *mode |= S_ISUID; | |
622 | else if (dip->i_di.di_uid != current->fsuid) | |
623 | *mode &= ~07111; | |
624 | *uid = dip->i_di.di_uid; | |
625 | } else | |
626 | *uid = current->fsuid; | |
627 | ||
b60623c2 | 628 | if (dip->i_inode.i_mode & S_ISGID) { |
b3b94faa DT |
629 | if (S_ISDIR(*mode)) |
630 | *mode |= S_ISGID; | |
631 | *gid = dip->i_di.di_gid; | |
632 | } else | |
633 | *gid = current->fsgid; | |
634 | } | |
635 | ||
629a21e7 | 636 | static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_inum_host *inum, |
4340fe62 | 637 | u64 *generation) |
b3b94faa | 638 | { |
feaa7bba | 639 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
b3b94faa DT |
640 | int error; |
641 | ||
642 | gfs2_alloc_get(dip); | |
643 | ||
644 | dip->i_alloc.al_requested = RES_DINODE; | |
645 | error = gfs2_inplace_reserve(dip); | |
646 | if (error) | |
647 | goto out; | |
648 | ||
feaa7bba | 649 | error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0); |
b3b94faa DT |
650 | if (error) |
651 | goto out_ipreserv; | |
652 | ||
4340fe62 | 653 | inum->no_addr = gfs2_alloc_di(dip, generation); |
b3b94faa DT |
654 | |
655 | gfs2_trans_end(sdp); | |
656 | ||
4340fe62 | 657 | out_ipreserv: |
b3b94faa | 658 | gfs2_inplace_release(dip); |
4340fe62 | 659 | out: |
b3b94faa | 660 | gfs2_alloc_put(dip); |
b3b94faa DT |
661 | return error; |
662 | } | |
663 | ||
664 | /** | |
665 | * init_dinode - Fill in a new dinode structure | |
666 | * @dip: the directory this inode is being created in | |
667 | * @gl: The glock covering the new inode | |
668 | * @inum: the inode number | |
669 | * @mode: the file permissions | |
670 | * @uid: | |
671 | * @gid: | |
672 | * | |
673 | */ | |
674 | ||
675 | static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl, | |
629a21e7 | 676 | const struct gfs2_inum_host *inum, unsigned int mode, |
4340fe62 | 677 | unsigned int uid, unsigned int gid, |
e7f14f4d | 678 | const u64 *generation, dev_t dev) |
b3b94faa | 679 | { |
feaa7bba | 680 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
b96ca4fa | 681 | struct gfs2_dinode *di; |
b3b94faa DT |
682 | struct buffer_head *dibh; |
683 | ||
684 | dibh = gfs2_meta_new(gl, inum->no_addr); | |
d4e9c4c3 | 685 | gfs2_trans_add_bh(gl, dibh, 1); |
b3b94faa DT |
686 | gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI); |
687 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); | |
b96ca4fa SW |
688 | di = (struct gfs2_dinode *)dibh->b_data; |
689 | ||
2442a098 SW |
690 | di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino); |
691 | di->di_num.no_addr = cpu_to_be64(inum->no_addr); | |
b96ca4fa SW |
692 | di->di_mode = cpu_to_be32(mode); |
693 | di->di_uid = cpu_to_be32(uid); | |
694 | di->di_gid = cpu_to_be32(gid); | |
695 | di->di_nlink = cpu_to_be32(0); | |
696 | di->di_size = cpu_to_be64(0); | |
697 | di->di_blocks = cpu_to_be64(1); | |
698 | di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds()); | |
e7f14f4d SW |
699 | di->di_major = cpu_to_be32(MAJOR(dev)); |
700 | di->di_minor = cpu_to_be32(MINOR(dev)); | |
b96ca4fa | 701 | di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr); |
4340fe62 | 702 | di->di_generation = cpu_to_be64(*generation); |
b96ca4fa | 703 | di->di_flags = cpu_to_be32(0); |
b3b94faa DT |
704 | |
705 | if (S_ISREG(mode)) { | |
706 | if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) || | |
707 | gfs2_tune_get(sdp, gt_new_files_jdata)) | |
b96ca4fa | 708 | di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA); |
b3b94faa DT |
709 | if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) || |
710 | gfs2_tune_get(sdp, gt_new_files_directio)) | |
b96ca4fa | 711 | di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO); |
b3b94faa | 712 | } else if (S_ISDIR(mode)) { |
568f4c96 SW |
713 | di->di_flags |= cpu_to_be32(dip->i_di.di_flags & |
714 | GFS2_DIF_INHERIT_DIRECTIO); | |
715 | di->di_flags |= cpu_to_be32(dip->i_di.di_flags & | |
716 | GFS2_DIF_INHERIT_JDATA); | |
b3b94faa DT |
717 | } |
718 | ||
b96ca4fa | 719 | di->__pad1 = 0; |
b2a580d8 | 720 | di->di_payload_format = cpu_to_be32(0); |
b96ca4fa SW |
721 | di->di_height = cpu_to_be32(0); |
722 | di->__pad2 = 0; | |
723 | di->__pad3 = 0; | |
724 | di->di_depth = cpu_to_be16(0); | |
725 | di->di_entries = cpu_to_be32(0); | |
726 | memset(&di->__pad4, 0, sizeof(di->__pad4)); | |
727 | di->di_eattr = cpu_to_be64(0); | |
728 | memset(&di->di_reserved, 0, sizeof(di->di_reserved)); | |
729 | ||
b3b94faa DT |
730 | brelse(dibh); |
731 | } | |
732 | ||
733 | static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl, | |
629a21e7 | 734 | unsigned int mode, const struct gfs2_inum_host *inum, |
e7f14f4d | 735 | const u64 *generation, dev_t dev) |
b3b94faa | 736 | { |
feaa7bba | 737 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
b3b94faa DT |
738 | unsigned int uid, gid; |
739 | int error; | |
740 | ||
741 | munge_mode_uid_gid(dip, &mode, &uid, &gid); | |
b3b94faa DT |
742 | gfs2_alloc_get(dip); |
743 | ||
744 | error = gfs2_quota_lock(dip, uid, gid); | |
745 | if (error) | |
746 | goto out; | |
747 | ||
748 | error = gfs2_quota_check(dip, uid, gid); | |
749 | if (error) | |
750 | goto out_quota; | |
751 | ||
feaa7bba | 752 | error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0); |
b3b94faa DT |
753 | if (error) |
754 | goto out_quota; | |
755 | ||
e7f14f4d | 756 | init_dinode(dip, gl, inum, mode, uid, gid, generation, dev); |
b3b94faa | 757 | gfs2_quota_change(dip, +1, uid, gid); |
b3b94faa DT |
758 | gfs2_trans_end(sdp); |
759 | ||
feaa7bba | 760 | out_quota: |
b3b94faa | 761 | gfs2_quota_unlock(dip); |
feaa7bba | 762 | out: |
b3b94faa | 763 | gfs2_alloc_put(dip); |
b3b94faa DT |
764 | return error; |
765 | } | |
766 | ||
feaa7bba SW |
767 | static int link_dinode(struct gfs2_inode *dip, const struct qstr *name, |
768 | struct gfs2_inode *ip) | |
b3b94faa | 769 | { |
feaa7bba | 770 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
b3b94faa DT |
771 | struct gfs2_alloc *al; |
772 | int alloc_required; | |
773 | struct buffer_head *dibh; | |
774 | int error; | |
775 | ||
776 | al = gfs2_alloc_get(dip); | |
777 | ||
778 | error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); | |
779 | if (error) | |
780 | goto fail; | |
781 | ||
feaa7bba | 782 | error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name); |
c752666c SW |
783 | if (alloc_required < 0) |
784 | goto fail; | |
b3b94faa DT |
785 | if (alloc_required) { |
786 | error = gfs2_quota_check(dip, dip->i_di.di_uid, | |
787 | dip->i_di.di_gid); | |
788 | if (error) | |
789 | goto fail_quota_locks; | |
790 | ||
791 | al->al_requested = sdp->sd_max_dirres; | |
792 | ||
793 | error = gfs2_inplace_reserve(dip); | |
794 | if (error) | |
795 | goto fail_quota_locks; | |
796 | ||
320dd101 | 797 | error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + |
b3b94faa | 798 | al->al_rgd->rd_ri.ri_length + |
907b9bce | 799 | 2 * RES_DINODE + |
b3b94faa DT |
800 | RES_STATFS + RES_QUOTA, 0); |
801 | if (error) | |
802 | goto fail_ipreserv; | |
803 | } else { | |
feaa7bba | 804 | error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0); |
b3b94faa DT |
805 | if (error) |
806 | goto fail_quota_locks; | |
807 | } | |
808 | ||
b60623c2 | 809 | error = gfs2_dir_add(&dip->i_inode, name, &ip->i_num, IF2DT(ip->i_inode.i_mode)); |
b3b94faa DT |
810 | if (error) |
811 | goto fail_end_trans; | |
812 | ||
813 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
814 | if (error) | |
815 | goto fail_end_trans; | |
816 | ip->i_di.di_nlink = 1; | |
d4e9c4c3 | 817 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
539e5d6b | 818 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa | 819 | brelse(dibh); |
b3b94faa DT |
820 | return 0; |
821 | ||
320dd101 | 822 | fail_end_trans: |
b3b94faa DT |
823 | gfs2_trans_end(sdp); |
824 | ||
320dd101 | 825 | fail_ipreserv: |
b3b94faa DT |
826 | if (dip->i_alloc.al_rgd) |
827 | gfs2_inplace_release(dip); | |
828 | ||
320dd101 | 829 | fail_quota_locks: |
b3b94faa DT |
830 | gfs2_quota_unlock(dip); |
831 | ||
320dd101 | 832 | fail: |
b3b94faa | 833 | gfs2_alloc_put(dip); |
b3b94faa DT |
834 | return error; |
835 | } | |
836 | ||
fcb47e0b RH |
837 | static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip) |
838 | { | |
839 | int err; | |
840 | size_t len; | |
841 | void *value; | |
842 | char *name; | |
843 | struct gfs2_ea_request er; | |
844 | ||
845 | err = security_inode_init_security(&ip->i_inode, &dip->i_inode, | |
846 | &name, &value, &len); | |
847 | ||
848 | if (err) { | |
849 | if (err == -EOPNOTSUPP) | |
850 | return 0; | |
851 | return err; | |
852 | } | |
853 | ||
854 | memset(&er, 0, sizeof(struct gfs2_ea_request)); | |
855 | ||
856 | er.er_type = GFS2_EATYPE_SECURITY; | |
857 | er.er_name = name; | |
858 | er.er_data = value; | |
859 | er.er_name_len = strlen(name); | |
860 | er.er_data_len = len; | |
861 | ||
862 | err = gfs2_ea_set_i(ip, &er); | |
863 | ||
864 | kfree(value); | |
865 | kfree(name); | |
866 | ||
867 | return err; | |
868 | } | |
869 | ||
b3b94faa DT |
870 | /** |
871 | * gfs2_createi - Create a new inode | |
872 | * @ghs: An array of two holders | |
873 | * @name: The name of the new file | |
874 | * @mode: the permissions on the new inode | |
875 | * | |
876 | * @ghs[0] is an initialized holder for the directory | |
877 | * @ghs[1] is the holder for the inode lock | |
878 | * | |
7359a19c | 879 | * If the return value is not NULL, the glocks on both the directory and the new |
b3b94faa DT |
880 | * file are held. A transaction has been started and an inplace reservation |
881 | * is held, as well. | |
882 | * | |
7359a19c | 883 | * Returns: An inode |
b3b94faa DT |
884 | */ |
885 | ||
feaa7bba | 886 | struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name, |
e7f14f4d | 887 | unsigned int mode, dev_t dev) |
b3b94faa | 888 | { |
7359a19c | 889 | struct inode *inode; |
5c676f6d | 890 | struct gfs2_inode *dip = ghs->gh_gl->gl_object; |
feaa7bba SW |
891 | struct inode *dir = &dip->i_inode; |
892 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); | |
629a21e7 | 893 | struct gfs2_inum_host inum; |
b3b94faa | 894 | int error; |
4340fe62 | 895 | u64 generation; |
b3b94faa DT |
896 | |
897 | if (!name->len || name->len > GFS2_FNAMESIZE) | |
7359a19c | 898 | return ERR_PTR(-ENAMETOOLONG); |
b3b94faa | 899 | |
b3b94faa DT |
900 | gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs); |
901 | error = gfs2_glock_nq(ghs); | |
902 | if (error) | |
903 | goto fail; | |
904 | ||
905 | error = create_ok(dip, name, mode); | |
906 | if (error) | |
907 | goto fail_gunlock; | |
908 | ||
feaa7bba | 909 | error = pick_formal_ino(sdp, &inum.no_formal_ino); |
b3b94faa DT |
910 | if (error) |
911 | goto fail_gunlock; | |
912 | ||
4340fe62 | 913 | error = alloc_dinode(dip, &inum, &generation); |
b3b94faa DT |
914 | if (error) |
915 | goto fail_gunlock; | |
916 | ||
feaa7bba | 917 | if (inum.no_addr < dip->i_num.no_addr) { |
b3b94faa DT |
918 | gfs2_glock_dq(ghs); |
919 | ||
feaa7bba | 920 | error = gfs2_glock_nq_num(sdp, inum.no_addr, |
320dd101 SW |
921 | &gfs2_inode_glops, LM_ST_EXCLUSIVE, |
922 | GL_SKIP, ghs + 1); | |
b3b94faa | 923 | if (error) { |
7359a19c | 924 | return ERR_PTR(error); |
b3b94faa DT |
925 | } |
926 | ||
927 | gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs); | |
928 | error = gfs2_glock_nq(ghs); | |
929 | if (error) { | |
930 | gfs2_glock_dq_uninit(ghs + 1); | |
7359a19c | 931 | return ERR_PTR(error); |
b3b94faa DT |
932 | } |
933 | ||
934 | error = create_ok(dip, name, mode); | |
935 | if (error) | |
936 | goto fail_gunlock2; | |
937 | } else { | |
feaa7bba | 938 | error = gfs2_glock_nq_num(sdp, inum.no_addr, |
320dd101 SW |
939 | &gfs2_inode_glops, LM_ST_EXCLUSIVE, |
940 | GL_SKIP, ghs + 1); | |
b3b94faa DT |
941 | if (error) |
942 | goto fail_gunlock; | |
943 | } | |
944 | ||
e7f14f4d | 945 | error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev); |
b3b94faa DT |
946 | if (error) |
947 | goto fail_gunlock2; | |
948 | ||
feaa7bba SW |
949 | inode = gfs2_inode_lookup(dir->i_sb, &inum, IF2DT(mode)); |
950 | if (IS_ERR(inode)) | |
b3b94faa DT |
951 | goto fail_gunlock2; |
952 | ||
feaa7bba | 953 | error = gfs2_inode_refresh(GFS2_I(inode)); |
b3b94faa DT |
954 | if (error) |
955 | goto fail_iput; | |
956 | ||
feaa7bba | 957 | error = gfs2_acl_create(dip, GFS2_I(inode)); |
b3b94faa DT |
958 | if (error) |
959 | goto fail_iput; | |
960 | ||
fcb47e0b RH |
961 | error = gfs2_security_init(dip, GFS2_I(inode)); |
962 | if (error) | |
963 | goto fail_iput; | |
964 | ||
feaa7bba | 965 | error = link_dinode(dip, name, GFS2_I(inode)); |
b3b94faa DT |
966 | if (error) |
967 | goto fail_iput; | |
968 | ||
7359a19c SW |
969 | if (!inode) |
970 | return ERR_PTR(-ENOMEM); | |
971 | return inode; | |
b3b94faa | 972 | |
320dd101 | 973 | fail_iput: |
feaa7bba | 974 | iput(inode); |
320dd101 | 975 | fail_gunlock2: |
b3b94faa | 976 | gfs2_glock_dq_uninit(ghs + 1); |
320dd101 | 977 | fail_gunlock: |
b3b94faa | 978 | gfs2_glock_dq(ghs); |
320dd101 | 979 | fail: |
7359a19c | 980 | return ERR_PTR(error); |
b3b94faa DT |
981 | } |
982 | ||
b3b94faa DT |
983 | /** |
984 | * gfs2_rmdiri - Remove a directory | |
985 | * @dip: The parent directory of the directory to be removed | |
986 | * @name: The name of the directory to be removed | |
987 | * @ip: The GFS2 inode of the directory to be removed | |
988 | * | |
989 | * Assumes Glocks on dip and ip are held | |
990 | * | |
991 | * Returns: errno | |
992 | */ | |
993 | ||
feaa7bba SW |
994 | int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name, |
995 | struct gfs2_inode *ip) | |
b3b94faa | 996 | { |
b3b94faa DT |
997 | struct qstr dotname; |
998 | int error; | |
999 | ||
1000 | if (ip->i_di.di_entries != 2) { | |
1001 | if (gfs2_consist_inode(ip)) | |
4cc14f0b | 1002 | gfs2_dinode_print(ip); |
b3b94faa DT |
1003 | return -EIO; |
1004 | } | |
1005 | ||
1006 | error = gfs2_dir_del(dip, name); | |
1007 | if (error) | |
1008 | return error; | |
1009 | ||
1010 | error = gfs2_change_nlink(dip, -1); | |
1011 | if (error) | |
1012 | return error; | |
1013 | ||
71b86f56 | 1014 | gfs2_str2qstr(&dotname, "."); |
b3b94faa DT |
1015 | error = gfs2_dir_del(ip, &dotname); |
1016 | if (error) | |
1017 | return error; | |
1018 | ||
feaa7bba | 1019 | gfs2_str2qstr(&dotname, ".."); |
b3b94faa DT |
1020 | error = gfs2_dir_del(ip, &dotname); |
1021 | if (error) | |
1022 | return error; | |
1023 | ||
1024 | error = gfs2_change_nlink(ip, -2); | |
1025 | if (error) | |
1026 | return error; | |
1027 | ||
b3b94faa DT |
1028 | return error; |
1029 | } | |
1030 | ||
1031 | /* | |
1032 | * gfs2_unlink_ok - check to see that a inode is still in a directory | |
1033 | * @dip: the directory | |
1034 | * @name: the name of the file | |
1035 | * @ip: the inode | |
1036 | * | |
1037 | * Assumes that the lock on (at least) @dip is held. | |
1038 | * | |
1039 | * Returns: 0 if the parent/child relationship is correct, errno if it isn't | |
1040 | */ | |
1041 | ||
feaa7bba | 1042 | int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name, |
b3b94faa DT |
1043 | struct gfs2_inode *ip) |
1044 | { | |
629a21e7 | 1045 | struct gfs2_inum_host inum; |
b3b94faa DT |
1046 | unsigned int type; |
1047 | int error; | |
1048 | ||
feaa7bba | 1049 | if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode)) |
b3b94faa DT |
1050 | return -EPERM; |
1051 | ||
b60623c2 | 1052 | if ((dip->i_inode.i_mode & S_ISVTX) && |
b3b94faa | 1053 | dip->i_di.di_uid != current->fsuid && |
feaa7bba | 1054 | ip->i_di.di_uid != current->fsuid && !capable(CAP_FOWNER)) |
b3b94faa DT |
1055 | return -EPERM; |
1056 | ||
feaa7bba | 1057 | if (IS_APPEND(&dip->i_inode)) |
b3b94faa DT |
1058 | return -EPERM; |
1059 | ||
faf450ef | 1060 | error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL); |
b3b94faa DT |
1061 | if (error) |
1062 | return error; | |
1063 | ||
feaa7bba | 1064 | error = gfs2_dir_search(&dip->i_inode, name, &inum, &type); |
b3b94faa DT |
1065 | if (error) |
1066 | return error; | |
1067 | ||
1068 | if (!gfs2_inum_equal(&inum, &ip->i_num)) | |
1069 | return -ENOENT; | |
1070 | ||
b60623c2 | 1071 | if (IF2DT(ip->i_inode.i_mode) != type) { |
b3b94faa DT |
1072 | gfs2_consist_inode(dip); |
1073 | return -EIO; | |
1074 | } | |
1075 | ||
1076 | return 0; | |
1077 | } | |
1078 | ||
1079 | /* | |
1080 | * gfs2_ok_to_move - check if it's ok to move a directory to another directory | |
1081 | * @this: move this | |
1082 | * @to: to here | |
1083 | * | |
1084 | * Follow @to back to the root and make sure we don't encounter @this | |
1085 | * Assumes we already hold the rename lock. | |
1086 | * | |
1087 | * Returns: errno | |
1088 | */ | |
1089 | ||
1090 | int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to) | |
1091 | { | |
feaa7bba | 1092 | struct inode *dir = &to->i_inode; |
c9fd4307 | 1093 | struct super_block *sb = dir->i_sb; |
7359a19c | 1094 | struct inode *tmp; |
b3b94faa DT |
1095 | struct qstr dotdot; |
1096 | int error = 0; | |
1097 | ||
71b86f56 | 1098 | gfs2_str2qstr(&dotdot, ".."); |
b3b94faa | 1099 | |
7359a19c | 1100 | igrab(dir); |
b3b94faa DT |
1101 | |
1102 | for (;;) { | |
feaa7bba | 1103 | if (dir == &this->i_inode) { |
b3b94faa DT |
1104 | error = -EINVAL; |
1105 | break; | |
1106 | } | |
c9fd4307 | 1107 | if (dir == sb->s_root->d_inode) { |
b3b94faa DT |
1108 | error = 0; |
1109 | break; | |
1110 | } | |
1111 | ||
c752666c SW |
1112 | tmp = gfs2_lookupi(dir, &dotdot, 1, NULL); |
1113 | if (IS_ERR(tmp)) { | |
1114 | error = PTR_ERR(tmp); | |
b3b94faa | 1115 | break; |
c752666c | 1116 | } |
b3b94faa | 1117 | |
7359a19c SW |
1118 | iput(dir); |
1119 | dir = tmp; | |
b3b94faa DT |
1120 | } |
1121 | ||
7359a19c | 1122 | iput(dir); |
b3b94faa DT |
1123 | |
1124 | return error; | |
1125 | } | |
1126 | ||
1127 | /** | |
1128 | * gfs2_readlinki - return the contents of a symlink | |
1129 | * @ip: the symlink's inode | |
1130 | * @buf: a pointer to the buffer to be filled | |
1131 | * @len: a pointer to the length of @buf | |
1132 | * | |
1133 | * If @buf is too small, a piece of memory is kmalloc()ed and needs | |
1134 | * to be freed by the caller. | |
1135 | * | |
1136 | * Returns: errno | |
1137 | */ | |
1138 | ||
1139 | int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len) | |
1140 | { | |
1141 | struct gfs2_holder i_gh; | |
1142 | struct buffer_head *dibh; | |
1143 | unsigned int x; | |
1144 | int error; | |
1145 | ||
1146 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh); | |
1147 | error = gfs2_glock_nq_atime(&i_gh); | |
1148 | if (error) { | |
1149 | gfs2_holder_uninit(&i_gh); | |
1150 | return error; | |
1151 | } | |
1152 | ||
1153 | if (!ip->i_di.di_size) { | |
1154 | gfs2_consist_inode(ip); | |
1155 | error = -EIO; | |
1156 | goto out; | |
1157 | } | |
1158 | ||
1159 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
1160 | if (error) | |
1161 | goto out; | |
1162 | ||
1163 | x = ip->i_di.di_size + 1; | |
1164 | if (x > *len) { | |
1165 | *buf = kmalloc(x, GFP_KERNEL); | |
1166 | if (!*buf) { | |
1167 | error = -ENOMEM; | |
1168 | goto out_brelse; | |
1169 | } | |
1170 | } | |
1171 | ||
1172 | memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x); | |
1173 | *len = x; | |
1174 | ||
feaa7bba | 1175 | out_brelse: |
b3b94faa | 1176 | brelse(dibh); |
feaa7bba | 1177 | out: |
b3b94faa | 1178 | gfs2_glock_dq_uninit(&i_gh); |
b3b94faa DT |
1179 | return error; |
1180 | } | |
1181 | ||
1182 | /** | |
1183 | * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and | |
1184 | * conditionally update the inode's atime | |
1185 | * @gh: the holder to acquire | |
1186 | * | |
1187 | * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap | |
1188 | * Update if the difference between the current time and the inode's current | |
1189 | * atime is greater than an interval specified at mount. | |
1190 | * | |
1191 | * Returns: errno | |
1192 | */ | |
1193 | ||
1194 | int gfs2_glock_nq_atime(struct gfs2_holder *gh) | |
1195 | { | |
1196 | struct gfs2_glock *gl = gh->gh_gl; | |
1197 | struct gfs2_sbd *sdp = gl->gl_sbd; | |
5c676f6d | 1198 | struct gfs2_inode *ip = gl->gl_object; |
cd915493 | 1199 | s64 curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum); |
b3b94faa DT |
1200 | unsigned int state; |
1201 | int flags; | |
1202 | int error; | |
1203 | ||
1204 | if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) || | |
1205 | gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) || | |
1206 | gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops)) | |
1207 | return -EINVAL; | |
1208 | ||
1209 | state = gh->gh_state; | |
1210 | flags = gh->gh_flags; | |
1211 | ||
1212 | error = gfs2_glock_nq(gh); | |
1213 | if (error) | |
1214 | return error; | |
1215 | ||
1216 | if (test_bit(SDF_NOATIME, &sdp->sd_flags) || | |
1217 | (sdp->sd_vfs->s_flags & MS_RDONLY)) | |
1218 | return 0; | |
1219 | ||
1220 | curtime = get_seconds(); | |
1221 | if (curtime - ip->i_di.di_atime >= quantum) { | |
1222 | gfs2_glock_dq(gh); | |
fd88de56 SW |
1223 | gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY, |
1224 | gh); | |
b3b94faa DT |
1225 | error = gfs2_glock_nq(gh); |
1226 | if (error) | |
1227 | return error; | |
1228 | ||
1229 | /* Verify that atime hasn't been updated while we were | |
1230 | trying to get exclusive lock. */ | |
1231 | ||
1232 | curtime = get_seconds(); | |
1233 | if (curtime - ip->i_di.di_atime >= quantum) { | |
1234 | struct buffer_head *dibh; | |
48516ced | 1235 | struct gfs2_dinode *di; |
b3b94faa DT |
1236 | |
1237 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); | |
1238 | if (error == -EROFS) | |
1239 | return 0; | |
1240 | if (error) | |
1241 | goto fail; | |
1242 | ||
1243 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
1244 | if (error) | |
1245 | goto fail_end_trans; | |
1246 | ||
1247 | ip->i_di.di_atime = curtime; | |
1248 | ||
d4e9c4c3 | 1249 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
48516ced SW |
1250 | di = (struct gfs2_dinode *)dibh->b_data; |
1251 | di->di_atime = cpu_to_be64(ip->i_di.di_atime); | |
b3b94faa DT |
1252 | brelse(dibh); |
1253 | ||
1254 | gfs2_trans_end(sdp); | |
1255 | } | |
1256 | ||
1257 | /* If someone else has asked for the glock, | |
1258 | unlock and let them have it. Then reacquire | |
1259 | in the original state. */ | |
1260 | if (gfs2_glock_is_blocking(gl)) { | |
1261 | gfs2_glock_dq(gh); | |
1262 | gfs2_holder_reinit(state, flags, gh); | |
1263 | return gfs2_glock_nq(gh); | |
1264 | } | |
1265 | } | |
1266 | ||
1267 | return 0; | |
1268 | ||
feaa7bba | 1269 | fail_end_trans: |
b3b94faa | 1270 | gfs2_trans_end(sdp); |
feaa7bba | 1271 | fail: |
b3b94faa | 1272 | gfs2_glock_dq(gh); |
b3b94faa DT |
1273 | return error; |
1274 | } | |
1275 | ||
1276 | /** | |
1277 | * glock_compare_atime - Compare two struct gfs2_glock structures for sort | |
1278 | * @arg_a: the first structure | |
1279 | * @arg_b: the second structure | |
1280 | * | |
1281 | * Returns: 1 if A > B | |
1282 | * -1 if A < B | |
75d3b817 | 1283 | * 0 if A == B |
b3b94faa DT |
1284 | */ |
1285 | ||
1286 | static int glock_compare_atime(const void *arg_a, const void *arg_b) | |
1287 | { | |
75d3b817 SW |
1288 | const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a; |
1289 | const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b; | |
1290 | const struct lm_lockname *a = &gh_a->gh_gl->gl_name; | |
1291 | const struct lm_lockname *b = &gh_b->gh_gl->gl_name; | |
b3b94faa DT |
1292 | |
1293 | if (a->ln_number > b->ln_number) | |
75d3b817 SW |
1294 | return 1; |
1295 | if (a->ln_number < b->ln_number) | |
1296 | return -1; | |
1297 | if (gh_a->gh_state == LM_ST_SHARED && gh_b->gh_state == LM_ST_EXCLUSIVE) | |
1298 | return 1; | |
1299 | if (gh_a->gh_state == LM_ST_SHARED && (gh_b->gh_flags & GL_ATIME)) | |
1300 | return 1; | |
b3b94faa | 1301 | |
75d3b817 | 1302 | return 0; |
b3b94faa DT |
1303 | } |
1304 | ||
1305 | /** | |
1306 | * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an | |
1307 | * atime update | |
1308 | * @num_gh: the number of structures | |
1309 | * @ghs: an array of struct gfs2_holder structures | |
1310 | * | |
1311 | * Returns: 0 on success (all glocks acquired), | |
1312 | * errno on failure (no glocks acquired) | |
1313 | */ | |
1314 | ||
1315 | int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs) | |
1316 | { | |
1317 | struct gfs2_holder **p; | |
1318 | unsigned int x; | |
1319 | int error = 0; | |
1320 | ||
1321 | if (!num_gh) | |
1322 | return 0; | |
1323 | ||
1324 | if (num_gh == 1) { | |
1325 | ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); | |
1326 | if (ghs->gh_flags & GL_ATIME) | |
1327 | error = gfs2_glock_nq_atime(ghs); | |
1328 | else | |
1329 | error = gfs2_glock_nq(ghs); | |
1330 | return error; | |
1331 | } | |
1332 | ||
1333 | p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL); | |
1334 | if (!p) | |
1335 | return -ENOMEM; | |
1336 | ||
1337 | for (x = 0; x < num_gh; x++) | |
1338 | p[x] = &ghs[x]; | |
1339 | ||
1340 | sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL); | |
1341 | ||
1342 | for (x = 0; x < num_gh; x++) { | |
1343 | p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); | |
1344 | ||
1345 | if (p[x]->gh_flags & GL_ATIME) | |
1346 | error = gfs2_glock_nq_atime(p[x]); | |
1347 | else | |
1348 | error = gfs2_glock_nq(p[x]); | |
1349 | ||
1350 | if (error) { | |
1351 | while (x--) | |
1352 | gfs2_glock_dq(p[x]); | |
1353 | break; | |
1354 | } | |
1355 | } | |
1356 | ||
1357 | kfree(p); | |
b3b94faa DT |
1358 | return error; |
1359 | } | |
1360 | ||
b3b94faa DT |
1361 | |
1362 | static int | |
1363 | __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr) | |
1364 | { | |
1365 | struct buffer_head *dibh; | |
1366 | int error; | |
1367 | ||
1368 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
1369 | if (!error) { | |
feaa7bba SW |
1370 | error = inode_setattr(&ip->i_inode, attr); |
1371 | gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error); | |
b3b94faa DT |
1372 | gfs2_inode_attr_out(ip); |
1373 | ||
d4e9c4c3 | 1374 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
539e5d6b | 1375 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa DT |
1376 | brelse(dibh); |
1377 | } | |
1378 | return error; | |
1379 | } | |
1380 | ||
1381 | /** | |
1382 | * gfs2_setattr_simple - | |
1383 | * @ip: | |
1384 | * @attr: | |
1385 | * | |
1386 | * Called with a reference on the vnode. | |
1387 | * | |
1388 | * Returns: errno | |
1389 | */ | |
1390 | ||
1391 | int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr) | |
1392 | { | |
1393 | int error; | |
1394 | ||
5c676f6d | 1395 | if (current->journal_info) |
b3b94faa DT |
1396 | return __gfs2_setattr_simple(ip, attr); |
1397 | ||
feaa7bba | 1398 | error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0); |
b3b94faa DT |
1399 | if (error) |
1400 | return error; | |
1401 | ||
1402 | error = __gfs2_setattr_simple(ip, attr); | |
feaa7bba | 1403 | gfs2_trans_end(GFS2_SB(&ip->i_inode)); |
b3b94faa DT |
1404 | return error; |
1405 | } | |
1406 |