]>
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 | ||
b3b94faa DT |
10 | #include <linux/slab.h> |
11 | #include <linux/spinlock.h> | |
12 | #include <linux/completion.h> | |
13 | #include <linux/buffer_head.h> | |
14 | #include <linux/namei.h> | |
b3b94faa DT |
15 | #include <linux/mm.h> |
16 | #include <linux/xattr.h> | |
17 | #include <linux/posix_acl.h> | |
5c676f6d | 18 | #include <linux/gfs2_ondisk.h> |
71b86f56 | 19 | #include <linux/crc32.h> |
e9079cce | 20 | #include <linux/fiemap.h> |
b3b94faa DT |
21 | #include <asm/uaccess.h> |
22 | ||
23 | #include "gfs2.h" | |
5c676f6d | 24 | #include "incore.h" |
b3b94faa DT |
25 | #include "acl.h" |
26 | #include "bmap.h" | |
27 | #include "dir.h" | |
307cf6e6 | 28 | #include "xattr.h" |
b3b94faa DT |
29 | #include "glock.h" |
30 | #include "inode.h" | |
31 | #include "meta_io.h" | |
b3b94faa DT |
32 | #include "quota.h" |
33 | #include "rgrp.h" | |
34 | #include "trans.h" | |
5c676f6d | 35 | #include "util.h" |
b2760583 | 36 | #include "super.h" |
b3b94faa DT |
37 | |
38 | /** | |
39 | * gfs2_create - Create a file | |
40 | * @dir: The directory in which to create the file | |
41 | * @dentry: The dentry of the new file | |
42 | * @mode: The mode of the new file | |
43 | * | |
44 | * Returns: errno | |
45 | */ | |
46 | ||
47 | static int gfs2_create(struct inode *dir, struct dentry *dentry, | |
48 | int mode, struct nameidata *nd) | |
49 | { | |
feaa7bba SW |
50 | struct gfs2_inode *dip = GFS2_I(dir); |
51 | struct gfs2_sbd *sdp = GFS2_SB(dir); | |
b3b94faa DT |
52 | struct gfs2_holder ghs[2]; |
53 | struct inode *inode; | |
b3b94faa | 54 | |
b3b94faa DT |
55 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
56 | ||
57 | for (;;) { | |
e7f14f4d | 58 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0); |
7359a19c | 59 | if (!IS_ERR(inode)) { |
b3b94faa | 60 | gfs2_trans_end(sdp); |
6dbd8224 | 61 | if (dip->i_alloc->al_rgd) |
b3b94faa DT |
62 | gfs2_inplace_release(dip); |
63 | gfs2_quota_unlock(dip); | |
64 | gfs2_alloc_put(dip); | |
65 | gfs2_glock_dq_uninit_m(2, ghs); | |
3a8476dd | 66 | mark_inode_dirty(inode); |
b3b94faa | 67 | break; |
7359a19c | 68 | } else if (PTR_ERR(inode) != -EEXIST || |
3516586a | 69 | (nd && nd->flags & LOOKUP_EXCL)) { |
b3b94faa | 70 | gfs2_holder_uninit(ghs); |
7359a19c | 71 | return PTR_ERR(inode); |
b3b94faa DT |
72 | } |
73 | ||
a569c711 | 74 | inode = gfs2_lookupi(dir, &dentry->d_name, 0); |
c752666c SW |
75 | if (inode) { |
76 | if (!IS_ERR(inode)) { | |
c752666c SW |
77 | gfs2_holder_uninit(ghs); |
78 | break; | |
79 | } else { | |
80 | gfs2_holder_uninit(ghs); | |
81 | return PTR_ERR(inode); | |
82 | } | |
b3b94faa DT |
83 | } |
84 | } | |
85 | ||
b3b94faa | 86 | d_instantiate(dentry, inode); |
b3b94faa DT |
87 | |
88 | return 0; | |
89 | } | |
90 | ||
91 | /** | |
92 | * gfs2_lookup - Look up a filename in a directory and return its inode | |
93 | * @dir: The directory inode | |
94 | * @dentry: The dentry of the new inode | |
95 | * @nd: passed from Linux VFS, ignored by us | |
96 | * | |
97 | * Called by the VFS layer. Lock dir and call gfs2_lookupi() | |
98 | * | |
99 | * Returns: errno | |
100 | */ | |
101 | ||
102 | static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry, | |
103 | struct nameidata *nd) | |
104 | { | |
b3b94faa | 105 | struct inode *inode = NULL; |
b3b94faa | 106 | |
a569c711 | 107 | inode = gfs2_lookupi(dir, &dentry->d_name, 0); |
c752666c | 108 | if (inode && IS_ERR(inode)) |
e231c2ee | 109 | return ERR_CAST(inode); |
b3b94faa | 110 | |
9656b2c1 SW |
111 | if (inode) { |
112 | struct gfs2_glock *gl = GFS2_I(inode)->i_gl; | |
113 | struct gfs2_holder gh; | |
114 | int error; | |
115 | error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); | |
116 | if (error) { | |
117 | iput(inode); | |
118 | return ERR_PTR(error); | |
119 | } | |
120 | gfs2_glock_dq_uninit(&gh); | |
b3b94faa | 121 | return d_splice_alias(inode, dentry); |
9656b2c1 | 122 | } |
b3b94faa DT |
123 | d_add(dentry, inode); |
124 | ||
125 | return NULL; | |
126 | } | |
127 | ||
128 | /** | |
129 | * gfs2_link - Link to a file | |
130 | * @old_dentry: The inode to link | |
131 | * @dir: Add link to this directory | |
132 | * @dentry: The name of the link | |
133 | * | |
134 | * Link the inode in "old_dentry" into the directory "dir" with the | |
135 | * name in "dentry". | |
136 | * | |
137 | * Returns: errno | |
138 | */ | |
139 | ||
140 | static int gfs2_link(struct dentry *old_dentry, struct inode *dir, | |
141 | struct dentry *dentry) | |
142 | { | |
feaa7bba SW |
143 | struct gfs2_inode *dip = GFS2_I(dir); |
144 | struct gfs2_sbd *sdp = GFS2_SB(dir); | |
b3b94faa | 145 | struct inode *inode = old_dentry->d_inode; |
feaa7bba | 146 | struct gfs2_inode *ip = GFS2_I(inode); |
b3b94faa | 147 | struct gfs2_holder ghs[2]; |
2baee03f | 148 | struct buffer_head *dibh; |
b3b94faa DT |
149 | int alloc_required; |
150 | int error; | |
151 | ||
b60623c2 | 152 | if (S_ISDIR(inode->i_mode)) |
b3b94faa DT |
153 | return -EPERM; |
154 | ||
155 | gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); | |
156 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); | |
157 | ||
72dbf479 BP |
158 | error = gfs2_glock_nq(ghs); /* parent */ |
159 | if (error) | |
160 | goto out_parent; | |
161 | ||
162 | error = gfs2_glock_nq(ghs + 1); /* child */ | |
b3b94faa | 163 | if (error) |
72dbf479 | 164 | goto out_child; |
b3b94faa | 165 | |
d192a8e5 SW |
166 | error = -ENOENT; |
167 | if (inode->i_nlink == 0) | |
168 | goto out_gunlock; | |
169 | ||
b74c79e9 | 170 | error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC, 0); |
b3b94faa DT |
171 | if (error) |
172 | goto out_gunlock; | |
173 | ||
dbb7cae2 | 174 | error = gfs2_dir_check(dir, &dentry->d_name, NULL); |
b3b94faa DT |
175 | switch (error) { |
176 | case -ENOENT: | |
177 | break; | |
178 | case 0: | |
179 | error = -EEXIST; | |
180 | default: | |
181 | goto out_gunlock; | |
182 | } | |
183 | ||
184 | error = -EINVAL; | |
4f56110a | 185 | if (!dip->i_inode.i_nlink) |
b3b94faa DT |
186 | goto out_gunlock; |
187 | error = -EFBIG; | |
ad6203f2 | 188 | if (dip->i_entries == (u32)-1) |
b3b94faa DT |
189 | goto out_gunlock; |
190 | error = -EPERM; | |
191 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) | |
192 | goto out_gunlock; | |
193 | error = -EINVAL; | |
4f56110a | 194 | if (!ip->i_inode.i_nlink) |
b3b94faa DT |
195 | goto out_gunlock; |
196 | error = -EMLINK; | |
4f56110a | 197 | if (ip->i_inode.i_nlink == (u32)-1) |
b3b94faa DT |
198 | goto out_gunlock; |
199 | ||
c752666c SW |
200 | alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name); |
201 | if (error < 0) | |
b3b94faa | 202 | goto out_gunlock; |
c752666c | 203 | error = 0; |
b3b94faa DT |
204 | |
205 | if (alloc_required) { | |
206 | struct gfs2_alloc *al = gfs2_alloc_get(dip); | |
182fe5ab CG |
207 | if (!al) { |
208 | error = -ENOMEM; | |
209 | goto out_gunlock; | |
210 | } | |
b3b94faa | 211 | |
d82661d9 | 212 | error = gfs2_quota_lock_check(dip); |
b3b94faa DT |
213 | if (error) |
214 | goto out_alloc; | |
215 | ||
b3b94faa DT |
216 | al->al_requested = sdp->sd_max_dirres; |
217 | ||
218 | error = gfs2_inplace_reserve(dip); | |
219 | if (error) | |
220 | goto out_gunlock_q; | |
221 | ||
1b50259b | 222 | error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + |
bf97b673 | 223 | gfs2_rg_blocks(al) + |
b3b94faa DT |
224 | 2 * RES_DINODE + RES_STATFS + |
225 | RES_QUOTA, 0); | |
226 | if (error) | |
227 | goto out_ipres; | |
228 | } else { | |
229 | error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0); | |
230 | if (error) | |
231 | goto out_ipres; | |
232 | } | |
233 | ||
2baee03f | 234 | error = gfs2_meta_inode_buffer(ip, &dibh); |
b3b94faa DT |
235 | if (error) |
236 | goto out_end_trans; | |
237 | ||
2baee03f SW |
238 | error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode)); |
239 | if (error) | |
240 | goto out_brelse; | |
241 | ||
242 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); | |
243 | inc_nlink(&ip->i_inode); | |
244 | ip->i_inode.i_ctime = CURRENT_TIME; | |
245 | gfs2_dinode_out(ip, dibh->b_data); | |
246 | mark_inode_dirty(&ip->i_inode); | |
b3b94faa | 247 | |
2baee03f SW |
248 | out_brelse: |
249 | brelse(dibh); | |
feaa7bba | 250 | out_end_trans: |
b3b94faa | 251 | gfs2_trans_end(sdp); |
feaa7bba | 252 | out_ipres: |
b3b94faa DT |
253 | if (alloc_required) |
254 | gfs2_inplace_release(dip); | |
feaa7bba | 255 | out_gunlock_q: |
b3b94faa DT |
256 | if (alloc_required) |
257 | gfs2_quota_unlock(dip); | |
feaa7bba | 258 | out_alloc: |
b3b94faa DT |
259 | if (alloc_required) |
260 | gfs2_alloc_put(dip); | |
feaa7bba | 261 | out_gunlock: |
72dbf479 BP |
262 | gfs2_glock_dq(ghs + 1); |
263 | out_child: | |
264 | gfs2_glock_dq(ghs); | |
265 | out_parent: | |
b3b94faa DT |
266 | gfs2_holder_uninit(ghs); |
267 | gfs2_holder_uninit(ghs + 1); | |
b3b94faa | 268 | if (!error) { |
7de9c6ee | 269 | ihold(inode); |
b3b94faa DT |
270 | d_instantiate(dentry, inode); |
271 | mark_inode_dirty(inode); | |
272 | } | |
b3b94faa DT |
273 | return error; |
274 | } | |
275 | ||
87ec2174 SW |
276 | /* |
277 | * gfs2_unlink_ok - check to see that a inode is still in a directory | |
278 | * @dip: the directory | |
279 | * @name: the name of the file | |
280 | * @ip: the inode | |
281 | * | |
282 | * Assumes that the lock on (at least) @dip is held. | |
283 | * | |
284 | * Returns: 0 if the parent/child relationship is correct, errno if it isn't | |
285 | */ | |
286 | ||
287 | static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name, | |
288 | const struct gfs2_inode *ip) | |
289 | { | |
290 | int error; | |
291 | ||
292 | if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode)) | |
293 | return -EPERM; | |
294 | ||
295 | if ((dip->i_inode.i_mode & S_ISVTX) && | |
296 | dip->i_inode.i_uid != current_fsuid() && | |
297 | ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER)) | |
298 | return -EPERM; | |
299 | ||
300 | if (IS_APPEND(&dip->i_inode)) | |
301 | return -EPERM; | |
302 | ||
b74c79e9 | 303 | error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0); |
87ec2174 SW |
304 | if (error) |
305 | return error; | |
306 | ||
307 | error = gfs2_dir_check(&dip->i_inode, name, ip); | |
308 | if (error) | |
309 | return error; | |
310 | ||
311 | return 0; | |
312 | } | |
313 | ||
b3b94faa | 314 | /** |
855d23ce SW |
315 | * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it |
316 | * @dip: The parent directory | |
317 | * @name: The name of the entry in the parent directory | |
318 | * @bh: The inode buffer for the inode to be removed | |
319 | * @inode: The inode to be removed | |
320 | * | |
321 | * Called with all the locks and in a transaction. This will only be | |
322 | * called for a directory after it has been checked to ensure it is empty. | |
323 | * | |
324 | * Returns: 0 on success, or an error | |
325 | */ | |
326 | ||
327 | static int gfs2_unlink_inode(struct gfs2_inode *dip, | |
328 | const struct dentry *dentry, | |
329 | struct buffer_head *bh) | |
330 | { | |
331 | struct inode *inode = dentry->d_inode; | |
332 | struct gfs2_inode *ip = GFS2_I(inode); | |
333 | int error; | |
334 | ||
335 | error = gfs2_dir_del(dip, dentry); | |
336 | if (error) | |
337 | return error; | |
338 | ||
339 | ip->i_entries = 0; | |
340 | inode->i_ctime = CURRENT_TIME; | |
341 | if (S_ISDIR(inode->i_mode)) | |
342 | clear_nlink(inode); | |
343 | else | |
344 | drop_nlink(inode); | |
345 | gfs2_trans_add_bh(ip->i_gl, bh, 1); | |
346 | gfs2_dinode_out(ip, bh->b_data); | |
347 | mark_inode_dirty(inode); | |
348 | if (inode->i_nlink == 0) | |
349 | gfs2_unlink_di(inode); | |
350 | return 0; | |
351 | } | |
352 | ||
353 | ||
354 | /** | |
355 | * gfs2_unlink - Unlink an inode (this does rmdir as well) | |
356 | * @dir: The inode of the directory containing the inode to unlink | |
b3b94faa DT |
357 | * @dentry: The file itself |
358 | * | |
855d23ce SW |
359 | * This routine uses the type of the inode as a flag to figure out |
360 | * whether this is an unlink or an rmdir. | |
b3b94faa DT |
361 | * |
362 | * Returns: errno | |
363 | */ | |
364 | ||
365 | static int gfs2_unlink(struct inode *dir, struct dentry *dentry) | |
366 | { | |
feaa7bba SW |
367 | struct gfs2_inode *dip = GFS2_I(dir); |
368 | struct gfs2_sbd *sdp = GFS2_SB(dir); | |
855d23ce SW |
369 | struct inode *inode = dentry->d_inode; |
370 | struct gfs2_inode *ip = GFS2_I(inode); | |
371 | struct buffer_head *bh; | |
ddee7608 RC |
372 | struct gfs2_holder ghs[3]; |
373 | struct gfs2_rgrpd *rgd; | |
374 | struct gfs2_holder ri_gh; | |
b3b94faa DT |
375 | int error; |
376 | ||
ddee7608 RC |
377 | error = gfs2_rindex_hold(sdp, &ri_gh); |
378 | if (error) | |
379 | return error; | |
380 | ||
b3b94faa | 381 | gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
ddee7608 | 382 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); |
b3b94faa | 383 | |
dbb7cae2 | 384 | rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr); |
ddee7608 RC |
385 | gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2); |
386 | ||
387 | ||
8497a46e | 388 | error = gfs2_glock_nq(ghs); /* parent */ |
b3b94faa | 389 | if (error) |
8497a46e SW |
390 | goto out_parent; |
391 | ||
392 | error = gfs2_glock_nq(ghs + 1); /* child */ | |
393 | if (error) | |
394 | goto out_child; | |
395 | ||
d192a8e5 | 396 | error = -ENOENT; |
855d23ce | 397 | if (inode->i_nlink == 0) |
d192a8e5 SW |
398 | goto out_rgrp; |
399 | ||
855d23ce SW |
400 | if (S_ISDIR(inode->i_mode)) { |
401 | error = -ENOTEMPTY; | |
402 | if (ip->i_entries > 2 || inode->i_nlink > 2) | |
403 | goto out_rgrp; | |
404 | } | |
405 | ||
8497a46e SW |
406 | error = gfs2_glock_nq(ghs + 2); /* rgrp */ |
407 | if (error) | |
408 | goto out_rgrp; | |
b3b94faa DT |
409 | |
410 | error = gfs2_unlink_ok(dip, &dentry->d_name, ip); | |
411 | if (error) | |
72dbf479 | 412 | goto out_gunlock; |
b3b94faa | 413 | |
855d23ce | 414 | error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0); |
b3b94faa | 415 | if (error) |
cd012075 | 416 | goto out_gunlock; |
b3b94faa | 417 | |
855d23ce SW |
418 | error = gfs2_meta_inode_buffer(ip, &bh); |
419 | if (error) | |
420 | goto out_end_trans; | |
b3b94faa | 421 | |
855d23ce SW |
422 | error = gfs2_unlink_inode(dip, dentry, bh); |
423 | brelse(bh); | |
b3b94faa | 424 | |
feaa7bba SW |
425 | out_end_trans: |
426 | gfs2_trans_end(sdp); | |
72dbf479 | 427 | out_gunlock: |
8497a46e SW |
428 | gfs2_glock_dq(ghs + 2); |
429 | out_rgrp: | |
ddee7608 | 430 | gfs2_holder_uninit(ghs + 2); |
8497a46e SW |
431 | gfs2_glock_dq(ghs + 1); |
432 | out_child: | |
433 | gfs2_holder_uninit(ghs + 1); | |
434 | gfs2_glock_dq(ghs); | |
435 | out_parent: | |
436 | gfs2_holder_uninit(ghs); | |
ddee7608 | 437 | gfs2_glock_dq_uninit(&ri_gh); |
b3b94faa DT |
438 | return error; |
439 | } | |
440 | ||
441 | /** | |
442 | * gfs2_symlink - Create a symlink | |
443 | * @dir: The directory to create the symlink in | |
444 | * @dentry: The dentry to put the symlink in | |
445 | * @symname: The thing which the link points to | |
446 | * | |
447 | * Returns: errno | |
448 | */ | |
449 | ||
450 | static int gfs2_symlink(struct inode *dir, struct dentry *dentry, | |
451 | const char *symname) | |
452 | { | |
feaa7bba SW |
453 | struct gfs2_inode *dip = GFS2_I(dir), *ip; |
454 | struct gfs2_sbd *sdp = GFS2_SB(dir); | |
b3b94faa DT |
455 | struct gfs2_holder ghs[2]; |
456 | struct inode *inode; | |
457 | struct buffer_head *dibh; | |
458 | int size; | |
459 | int error; | |
460 | ||
b3b94faa DT |
461 | /* Must be stuffed with a null terminator for gfs2_follow_link() */ |
462 | size = strlen(symname); | |
463 | if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1) | |
464 | return -ENAMETOOLONG; | |
465 | ||
466 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); | |
467 | ||
e7f14f4d | 468 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0); |
7359a19c | 469 | if (IS_ERR(inode)) { |
b3b94faa | 470 | gfs2_holder_uninit(ghs); |
7359a19c | 471 | return PTR_ERR(inode); |
b3b94faa DT |
472 | } |
473 | ||
5c676f6d | 474 | ip = ghs[1].gh_gl->gl_object; |
b3b94faa | 475 | |
5cf32524 | 476 | i_size_write(inode, size); |
b3b94faa DT |
477 | |
478 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
479 | ||
480 | if (!gfs2_assert_withdraw(sdp, !error)) { | |
539e5d6b | 481 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa DT |
482 | memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, |
483 | size); | |
484 | brelse(dibh); | |
485 | } | |
486 | ||
487 | gfs2_trans_end(sdp); | |
6dbd8224 | 488 | if (dip->i_alloc->al_rgd) |
b3b94faa DT |
489 | gfs2_inplace_release(dip); |
490 | gfs2_quota_unlock(dip); | |
491 | gfs2_alloc_put(dip); | |
492 | ||
493 | gfs2_glock_dq_uninit_m(2, ghs); | |
494 | ||
b3b94faa DT |
495 | d_instantiate(dentry, inode); |
496 | mark_inode_dirty(inode); | |
497 | ||
498 | return 0; | |
499 | } | |
500 | ||
501 | /** | |
502 | * gfs2_mkdir - Make a directory | |
503 | * @dir: The parent directory of the new one | |
504 | * @dentry: The dentry of the new directory | |
505 | * @mode: The mode of the new directory | |
506 | * | |
507 | * Returns: errno | |
508 | */ | |
509 | ||
510 | static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
511 | { | |
feaa7bba SW |
512 | struct gfs2_inode *dip = GFS2_I(dir), *ip; |
513 | struct gfs2_sbd *sdp = GFS2_SB(dir); | |
b3b94faa DT |
514 | struct gfs2_holder ghs[2]; |
515 | struct inode *inode; | |
516 | struct buffer_head *dibh; | |
517 | int error; | |
518 | ||
b3b94faa DT |
519 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
520 | ||
e7f14f4d | 521 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0); |
7359a19c | 522 | if (IS_ERR(inode)) { |
b3b94faa | 523 | gfs2_holder_uninit(ghs); |
7359a19c | 524 | return PTR_ERR(inode); |
b3b94faa DT |
525 | } |
526 | ||
5c676f6d | 527 | ip = ghs[1].gh_gl->gl_object; |
b3b94faa | 528 | |
4f56110a | 529 | ip->i_inode.i_nlink = 2; |
a2e0f799 | 530 | i_size_write(inode, sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)); |
383f01fb | 531 | ip->i_diskflags |= GFS2_DIF_JDATA; |
ad6203f2 | 532 | ip->i_entries = 2; |
b3b94faa DT |
533 | |
534 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
535 | ||
536 | if (!gfs2_assert_withdraw(sdp, !error)) { | |
537 | struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data; | |
c752666c | 538 | struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1); |
b3b94faa | 539 | |
c752666c | 540 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
8d123585 | 541 | gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent); |
b3b94faa | 542 | dent->de_inum = di->di_num; /* already GFS2 endian */ |
7ecdb70a | 543 | dent->de_type = cpu_to_be16(DT_DIR); |
b3b94faa DT |
544 | di->di_entries = cpu_to_be32(1); |
545 | ||
c752666c | 546 | dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1)); |
8d123585 | 547 | gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent); |
b3b94faa | 548 | |
dbb7cae2 | 549 | gfs2_inum_out(dip, dent); |
7ecdb70a | 550 | dent->de_type = cpu_to_be16(DT_DIR); |
b3b94faa | 551 | |
539e5d6b | 552 | gfs2_dinode_out(ip, di); |
b3b94faa DT |
553 | |
554 | brelse(dibh); | |
555 | } | |
556 | ||
557 | error = gfs2_change_nlink(dip, +1); | |
558 | gfs2_assert_withdraw(sdp, !error); /* dip already pinned */ | |
559 | ||
560 | gfs2_trans_end(sdp); | |
6dbd8224 | 561 | if (dip->i_alloc->al_rgd) |
b3b94faa DT |
562 | gfs2_inplace_release(dip); |
563 | gfs2_quota_unlock(dip); | |
564 | gfs2_alloc_put(dip); | |
565 | ||
566 | gfs2_glock_dq_uninit_m(2, ghs); | |
567 | ||
b3b94faa DT |
568 | d_instantiate(dentry, inode); |
569 | mark_inode_dirty(inode); | |
570 | ||
571 | return 0; | |
572 | } | |
573 | ||
b3b94faa DT |
574 | /** |
575 | * gfs2_mknod - Make a special file | |
576 | * @dir: The directory in which the special file will reside | |
577 | * @dentry: The dentry of the special file | |
578 | * @mode: The mode of the special file | |
579 | * @rdev: The device specification of the special file | |
580 | * | |
581 | */ | |
582 | ||
583 | static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode, | |
584 | dev_t dev) | |
585 | { | |
e7f14f4d | 586 | struct gfs2_inode *dip = GFS2_I(dir); |
feaa7bba | 587 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
b3b94faa DT |
588 | struct gfs2_holder ghs[2]; |
589 | struct inode *inode; | |
b3b94faa DT |
590 | |
591 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); | |
592 | ||
e7f14f4d | 593 | inode = gfs2_createi(ghs, &dentry->d_name, mode, dev); |
7359a19c | 594 | if (IS_ERR(inode)) { |
b3b94faa | 595 | gfs2_holder_uninit(ghs); |
7359a19c | 596 | return PTR_ERR(inode); |
b3b94faa DT |
597 | } |
598 | ||
b3b94faa | 599 | gfs2_trans_end(sdp); |
6dbd8224 | 600 | if (dip->i_alloc->al_rgd) |
b3b94faa DT |
601 | gfs2_inplace_release(dip); |
602 | gfs2_quota_unlock(dip); | |
603 | gfs2_alloc_put(dip); | |
604 | ||
605 | gfs2_glock_dq_uninit_m(2, ghs); | |
606 | ||
b3b94faa DT |
607 | d_instantiate(dentry, inode); |
608 | mark_inode_dirty(inode); | |
609 | ||
610 | return 0; | |
611 | } | |
612 | ||
0188d6c5 SW |
613 | /* |
614 | * gfs2_ok_to_move - check if it's ok to move a directory to another directory | |
615 | * @this: move this | |
616 | * @to: to here | |
617 | * | |
618 | * Follow @to back to the root and make sure we don't encounter @this | |
619 | * Assumes we already hold the rename lock. | |
620 | * | |
621 | * Returns: errno | |
622 | */ | |
623 | ||
624 | static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to) | |
625 | { | |
626 | struct inode *dir = &to->i_inode; | |
627 | struct super_block *sb = dir->i_sb; | |
628 | struct inode *tmp; | |
0188d6c5 SW |
629 | int error = 0; |
630 | ||
0188d6c5 SW |
631 | igrab(dir); |
632 | ||
633 | for (;;) { | |
634 | if (dir == &this->i_inode) { | |
635 | error = -EINVAL; | |
636 | break; | |
637 | } | |
638 | if (dir == sb->s_root->d_inode) { | |
639 | error = 0; | |
640 | break; | |
641 | } | |
642 | ||
8d123585 | 643 | tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1); |
0188d6c5 SW |
644 | if (IS_ERR(tmp)) { |
645 | error = PTR_ERR(tmp); | |
646 | break; | |
647 | } | |
648 | ||
649 | iput(dir); | |
650 | dir = tmp; | |
651 | } | |
652 | ||
653 | iput(dir); | |
654 | ||
655 | return error; | |
656 | } | |
657 | ||
b3b94faa DT |
658 | /** |
659 | * gfs2_rename - Rename a file | |
660 | * @odir: Parent directory of old file name | |
661 | * @odentry: The old dentry of the file | |
662 | * @ndir: Parent directory of new file name | |
663 | * @ndentry: The new dentry of the file | |
664 | * | |
665 | * Returns: errno | |
666 | */ | |
667 | ||
668 | static int gfs2_rename(struct inode *odir, struct dentry *odentry, | |
669 | struct inode *ndir, struct dentry *ndentry) | |
670 | { | |
feaa7bba SW |
671 | struct gfs2_inode *odip = GFS2_I(odir); |
672 | struct gfs2_inode *ndip = GFS2_I(ndir); | |
673 | struct gfs2_inode *ip = GFS2_I(odentry->d_inode); | |
b3b94faa | 674 | struct gfs2_inode *nip = NULL; |
feaa7bba | 675 | struct gfs2_sbd *sdp = GFS2_SB(odir); |
46290341 | 676 | struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh; |
ddee7608 | 677 | struct gfs2_rgrpd *nrgd; |
b3b94faa DT |
678 | unsigned int num_gh; |
679 | int dir_rename = 0; | |
24b977b5 | 680 | int alloc_required = 0; |
b3b94faa DT |
681 | unsigned int x; |
682 | int error; | |
683 | ||
b3b94faa | 684 | if (ndentry->d_inode) { |
feaa7bba | 685 | nip = GFS2_I(ndentry->d_inode); |
b3b94faa DT |
686 | if (ip == nip) |
687 | return 0; | |
688 | } | |
689 | ||
46290341 BP |
690 | error = gfs2_rindex_hold(sdp, &ri_gh); |
691 | if (error) | |
692 | return error; | |
b3b94faa | 693 | |
0188d6c5 SW |
694 | if (odip != ndip) { |
695 | error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, | |
696 | 0, &r_gh); | |
b3b94faa DT |
697 | if (error) |
698 | goto out; | |
699 | ||
0188d6c5 SW |
700 | if (S_ISDIR(ip->i_inode.i_mode)) { |
701 | dir_rename = 1; | |
702 | /* don't move a dirctory into it's subdir */ | |
703 | error = gfs2_ok_to_move(ip, ndip); | |
704 | if (error) | |
705 | goto out_gunlock_r; | |
706 | } | |
b3b94faa DT |
707 | } |
708 | ||
d9d1ca30 | 709 | num_gh = 1; |
b3b94faa | 710 | gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
d9d1ca30 SW |
711 | if (odip != ndip) { |
712 | gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); | |
713 | num_gh++; | |
714 | } | |
715 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); | |
716 | num_gh++; | |
b3b94faa | 717 | |
d9d1ca30 SW |
718 | if (nip) { |
719 | gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); | |
720 | num_gh++; | |
ddee7608 RC |
721 | /* grab the resource lock for unlink flag twiddling |
722 | * this is the case of the target file already existing | |
723 | * so we unlink before doing the rename | |
724 | */ | |
dbb7cae2 | 725 | nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr); |
ddee7608 RC |
726 | if (nrgd) |
727 | gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++); | |
d9d1ca30 | 728 | } |
b3b94faa | 729 | |
72dbf479 BP |
730 | for (x = 0; x < num_gh; x++) { |
731 | error = gfs2_glock_nq(ghs + x); | |
732 | if (error) | |
733 | goto out_gunlock; | |
734 | } | |
b3b94faa | 735 | |
d192a8e5 SW |
736 | error = -ENOENT; |
737 | if (ip->i_inode.i_nlink == 0) | |
738 | goto out_gunlock; | |
739 | ||
b3b94faa DT |
740 | /* Check out the old directory */ |
741 | ||
742 | error = gfs2_unlink_ok(odip, &odentry->d_name, ip); | |
743 | if (error) | |
744 | goto out_gunlock; | |
745 | ||
746 | /* Check out the new directory */ | |
747 | ||
748 | if (nip) { | |
749 | error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip); | |
750 | if (error) | |
751 | goto out_gunlock; | |
752 | ||
d192a8e5 SW |
753 | if (nip->i_inode.i_nlink == 0) { |
754 | error = -EAGAIN; | |
755 | goto out_gunlock; | |
756 | } | |
757 | ||
b60623c2 | 758 | if (S_ISDIR(nip->i_inode.i_mode)) { |
ad6203f2 | 759 | if (nip->i_entries < 2) { |
b3b94faa | 760 | if (gfs2_consist_inode(nip)) |
4cc14f0b | 761 | gfs2_dinode_print(nip); |
b3b94faa DT |
762 | error = -EIO; |
763 | goto out_gunlock; | |
764 | } | |
ad6203f2 | 765 | if (nip->i_entries > 2) { |
b3b94faa DT |
766 | error = -ENOTEMPTY; |
767 | goto out_gunlock; | |
768 | } | |
769 | } | |
770 | } else { | |
b74c79e9 | 771 | error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC, 0); |
b3b94faa DT |
772 | if (error) |
773 | goto out_gunlock; | |
774 | ||
dbb7cae2 | 775 | error = gfs2_dir_check(ndir, &ndentry->d_name, NULL); |
b3b94faa DT |
776 | switch (error) { |
777 | case -ENOENT: | |
778 | error = 0; | |
779 | break; | |
780 | case 0: | |
781 | error = -EEXIST; | |
782 | default: | |
783 | goto out_gunlock; | |
784 | }; | |
785 | ||
786 | if (odip != ndip) { | |
4f56110a | 787 | if (!ndip->i_inode.i_nlink) { |
d192a8e5 | 788 | error = -ENOENT; |
b3b94faa DT |
789 | goto out_gunlock; |
790 | } | |
ad6203f2 | 791 | if (ndip->i_entries == (u32)-1) { |
b3b94faa DT |
792 | error = -EFBIG; |
793 | goto out_gunlock; | |
794 | } | |
b60623c2 | 795 | if (S_ISDIR(ip->i_inode.i_mode) && |
4f56110a | 796 | ndip->i_inode.i_nlink == (u32)-1) { |
b3b94faa DT |
797 | error = -EMLINK; |
798 | goto out_gunlock; | |
799 | } | |
800 | } | |
801 | } | |
802 | ||
803 | /* Check out the dir to be renamed */ | |
804 | ||
805 | if (dir_rename) { | |
b74c79e9 | 806 | error = gfs2_permission(odentry->d_inode, MAY_WRITE, 0); |
b3b94faa DT |
807 | if (error) |
808 | goto out_gunlock; | |
809 | } | |
810 | ||
24b977b5 SW |
811 | if (nip == NULL) |
812 | alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name); | |
813 | error = alloc_required; | |
c752666c | 814 | if (error < 0) |
b3b94faa | 815 | goto out_gunlock; |
c752666c | 816 | error = 0; |
b3b94faa DT |
817 | |
818 | if (alloc_required) { | |
819 | struct gfs2_alloc *al = gfs2_alloc_get(ndip); | |
182fe5ab CG |
820 | if (!al) { |
821 | error = -ENOMEM; | |
822 | goto out_gunlock; | |
823 | } | |
b3b94faa | 824 | |
d82661d9 | 825 | error = gfs2_quota_lock_check(ndip); |
b3b94faa DT |
826 | if (error) |
827 | goto out_alloc; | |
828 | ||
b3b94faa DT |
829 | al->al_requested = sdp->sd_max_dirres; |
830 | ||
46290341 | 831 | error = gfs2_inplace_reserve_ri(ndip); |
b3b94faa DT |
832 | if (error) |
833 | goto out_gunlock_q; | |
834 | ||
fe1bdedc | 835 | error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + |
bf97b673 | 836 | gfs2_rg_blocks(al) + |
b3b94faa | 837 | 4 * RES_DINODE + 4 * RES_LEAF + |
87d21e07 | 838 | RES_STATFS + RES_QUOTA + 4, 0); |
b3b94faa DT |
839 | if (error) |
840 | goto out_ipreserv; | |
841 | } else { | |
842 | error = gfs2_trans_begin(sdp, 4 * RES_DINODE + | |
87d21e07 | 843 | 5 * RES_LEAF + 4, 0); |
b3b94faa DT |
844 | if (error) |
845 | goto out_gunlock; | |
846 | } | |
847 | ||
848 | /* Remove the target file, if it exists */ | |
849 | ||
850 | if (nip) { | |
855d23ce SW |
851 | struct buffer_head *bh; |
852 | error = gfs2_meta_inode_buffer(nip, &bh); | |
b3b94faa DT |
853 | if (error) |
854 | goto out_end_trans; | |
855d23ce SW |
855 | error = gfs2_unlink_inode(ndip, ndentry, bh); |
856 | brelse(bh); | |
b3b94faa DT |
857 | } |
858 | ||
859 | if (dir_rename) { | |
b3b94faa | 860 | error = gfs2_change_nlink(ndip, +1); |
b3b94faa DT |
861 | if (error) |
862 | goto out_end_trans; | |
863 | ||
8d123585 | 864 | error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR); |
b3b94faa DT |
865 | if (error) |
866 | goto out_end_trans; | |
867 | } else { | |
868 | struct buffer_head *dibh; | |
869 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
870 | if (error) | |
871 | goto out_end_trans; | |
4bd91ba1 | 872 | ip->i_inode.i_ctime = CURRENT_TIME; |
d4e9c4c3 | 873 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
539e5d6b | 874 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa DT |
875 | brelse(dibh); |
876 | } | |
877 | ||
855d23ce | 878 | error = gfs2_dir_del(odip, odentry); |
b3b94faa DT |
879 | if (error) |
880 | goto out_end_trans; | |
881 | ||
dbb7cae2 | 882 | error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode)); |
b3b94faa DT |
883 | if (error) |
884 | goto out_end_trans; | |
885 | ||
feaa7bba | 886 | out_end_trans: |
b3b94faa | 887 | gfs2_trans_end(sdp); |
feaa7bba | 888 | out_ipreserv: |
b3b94faa DT |
889 | if (alloc_required) |
890 | gfs2_inplace_release(ndip); | |
feaa7bba | 891 | out_gunlock_q: |
b3b94faa DT |
892 | if (alloc_required) |
893 | gfs2_quota_unlock(ndip); | |
feaa7bba | 894 | out_alloc: |
b3b94faa DT |
895 | if (alloc_required) |
896 | gfs2_alloc_put(ndip); | |
feaa7bba | 897 | out_gunlock: |
72dbf479 BP |
898 | while (x--) { |
899 | gfs2_glock_dq(ghs + x); | |
b3b94faa | 900 | gfs2_holder_uninit(ghs + x); |
72dbf479 | 901 | } |
feaa7bba | 902 | out_gunlock_r: |
0188d6c5 | 903 | if (r_gh.gh_gl) |
b3b94faa | 904 | gfs2_glock_dq_uninit(&r_gh); |
feaa7bba | 905 | out: |
46290341 | 906 | gfs2_glock_dq_uninit(&ri_gh); |
b3b94faa DT |
907 | return error; |
908 | } | |
909 | ||
536baf02 | 910 | /** |
c177c2ac AV |
911 | * gfs2_follow_link - Follow a symbolic link |
912 | * @dentry: The dentry of the link | |
913 | * @nd: Data that we pass to vfs_follow_link() | |
536baf02 | 914 | * |
c177c2ac | 915 | * This can handle symlinks of any size. |
536baf02 | 916 | * |
c177c2ac | 917 | * Returns: 0 on success or error code |
536baf02 SW |
918 | */ |
919 | ||
c177c2ac | 920 | static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd) |
536baf02 | 921 | { |
c177c2ac | 922 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
536baf02 SW |
923 | struct gfs2_holder i_gh; |
924 | struct buffer_head *dibh; | |
a2e0f799 | 925 | unsigned int x, size; |
c177c2ac | 926 | char *buf; |
536baf02 SW |
927 | int error; |
928 | ||
929 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh); | |
930 | error = gfs2_glock_nq(&i_gh); | |
931 | if (error) { | |
932 | gfs2_holder_uninit(&i_gh); | |
c177c2ac AV |
933 | nd_set_link(nd, ERR_PTR(error)); |
934 | return NULL; | |
536baf02 SW |
935 | } |
936 | ||
a2e0f799 SW |
937 | size = (unsigned int)i_size_read(&ip->i_inode); |
938 | if (size == 0) { | |
536baf02 | 939 | gfs2_consist_inode(ip); |
c177c2ac | 940 | buf = ERR_PTR(-EIO); |
536baf02 SW |
941 | goto out; |
942 | } | |
943 | ||
944 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
c177c2ac AV |
945 | if (error) { |
946 | buf = ERR_PTR(error); | |
536baf02 | 947 | goto out; |
536baf02 SW |
948 | } |
949 | ||
a2e0f799 | 950 | x = size + 1; |
c177c2ac AV |
951 | buf = kmalloc(x, GFP_NOFS); |
952 | if (!buf) | |
953 | buf = ERR_PTR(-ENOMEM); | |
954 | else | |
955 | memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x); | |
536baf02 SW |
956 | brelse(dibh); |
957 | out: | |
958 | gfs2_glock_dq_uninit(&i_gh); | |
c177c2ac AV |
959 | nd_set_link(nd, buf); |
960 | return NULL; | |
b3b94faa DT |
961 | } |
962 | ||
c177c2ac | 963 | static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p) |
b3b94faa | 964 | { |
c177c2ac AV |
965 | char *s = nd_get_link(nd); |
966 | if (!IS_ERR(s)) | |
967 | kfree(s); | |
b3b94faa DT |
968 | } |
969 | ||
970 | /** | |
971 | * gfs2_permission - | |
75d5cfbe SW |
972 | * @inode: The inode |
973 | * @mask: The mask to be tested | |
974 | * @flags: Indicates whether this is an RCU path walk or not | |
b3b94faa | 975 | * |
300c7d75 SW |
976 | * This may be called from the VFS directly, or from within GFS2 with the |
977 | * inode locked, so we look to see if the glock is already locked and only | |
978 | * lock the glock if its not already been done. | |
979 | * | |
b3b94faa DT |
980 | * Returns: errno |
981 | */ | |
982 | ||
b74c79e9 | 983 | int gfs2_permission(struct inode *inode, int mask, unsigned int flags) |
b3b94faa | 984 | { |
b74c79e9 | 985 | struct gfs2_inode *ip; |
b3b94faa DT |
986 | struct gfs2_holder i_gh; |
987 | int error; | |
300c7d75 | 988 | int unlock = 0; |
b3b94faa | 989 | |
b74c79e9 NP |
990 | |
991 | ip = GFS2_I(inode); | |
7afd88d9 | 992 | if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) { |
75d5cfbe SW |
993 | if (flags & IPERM_FLAG_RCU) |
994 | return -ECHILD; | |
300c7d75 SW |
995 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); |
996 | if (error) | |
997 | return error; | |
998 | unlock = 1; | |
999 | } | |
b3b94faa | 1000 | |
f58ba889 MS |
1001 | if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode)) |
1002 | error = -EACCES; | |
1003 | else | |
b74c79e9 | 1004 | error = generic_permission(inode, mask, flags, gfs2_check_acl); |
300c7d75 | 1005 | if (unlock) |
b3b94faa | 1006 | gfs2_glock_dq_uninit(&i_gh); |
b3b94faa DT |
1007 | |
1008 | return error; | |
1009 | } | |
1010 | ||
b3b94faa DT |
1011 | static int setattr_chown(struct inode *inode, struct iattr *attr) |
1012 | { | |
feaa7bba SW |
1013 | struct gfs2_inode *ip = GFS2_I(inode); |
1014 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
cd915493 | 1015 | u32 ouid, ogid, nuid, ngid; |
b3b94faa DT |
1016 | int error; |
1017 | ||
2933f925 SW |
1018 | ouid = inode->i_uid; |
1019 | ogid = inode->i_gid; | |
b3b94faa DT |
1020 | nuid = attr->ia_uid; |
1021 | ngid = attr->ia_gid; | |
1022 | ||
1023 | if (!(attr->ia_valid & ATTR_UID) || ouid == nuid) | |
1024 | ouid = nuid = NO_QUOTA_CHANGE; | |
1025 | if (!(attr->ia_valid & ATTR_GID) || ogid == ngid) | |
1026 | ogid = ngid = NO_QUOTA_CHANGE; | |
1027 | ||
182fe5ab CG |
1028 | if (!gfs2_alloc_get(ip)) |
1029 | return -ENOMEM; | |
b3b94faa DT |
1030 | |
1031 | error = gfs2_quota_lock(ip, nuid, ngid); | |
1032 | if (error) | |
1033 | goto out_alloc; | |
1034 | ||
1035 | if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) { | |
1036 | error = gfs2_quota_check(ip, nuid, ngid); | |
1037 | if (error) | |
1038 | goto out_gunlock_q; | |
1039 | } | |
1040 | ||
1041 | error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0); | |
1042 | if (error) | |
1043 | goto out_gunlock_q; | |
1044 | ||
2ae51ed7 | 1045 | error = gfs2_setattr_simple(ip, attr); |
b3b94faa DT |
1046 | if (error) |
1047 | goto out_end_trans; | |
1048 | ||
b3b94faa | 1049 | if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) { |
77658aad SW |
1050 | u64 blocks = gfs2_get_inode_blocks(&ip->i_inode); |
1051 | gfs2_quota_change(ip, -blocks, ouid, ogid); | |
1052 | gfs2_quota_change(ip, blocks, nuid, ngid); | |
b3b94faa DT |
1053 | } |
1054 | ||
a91ea69f | 1055 | out_end_trans: |
b3b94faa | 1056 | gfs2_trans_end(sdp); |
a91ea69f | 1057 | out_gunlock_q: |
b3b94faa | 1058 | gfs2_quota_unlock(ip); |
a91ea69f | 1059 | out_alloc: |
b3b94faa | 1060 | gfs2_alloc_put(ip); |
b3b94faa DT |
1061 | return error; |
1062 | } | |
1063 | ||
1064 | /** | |
1065 | * gfs2_setattr - Change attributes on an inode | |
1066 | * @dentry: The dentry which is changing | |
1067 | * @attr: The structure describing the change | |
1068 | * | |
1069 | * The VFS layer wants to change one or more of an inodes attributes. Write | |
1070 | * that change out to disk. | |
1071 | * | |
1072 | * Returns: errno | |
1073 | */ | |
1074 | ||
1075 | static int gfs2_setattr(struct dentry *dentry, struct iattr *attr) | |
1076 | { | |
1077 | struct inode *inode = dentry->d_inode; | |
feaa7bba | 1078 | struct gfs2_inode *ip = GFS2_I(inode); |
b3b94faa DT |
1079 | struct gfs2_holder i_gh; |
1080 | int error; | |
1081 | ||
b3b94faa DT |
1082 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
1083 | if (error) | |
1084 | return error; | |
1085 | ||
1086 | error = -EPERM; | |
1087 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) | |
1088 | goto out; | |
1089 | ||
1090 | error = inode_change_ok(inode, attr); | |
1091 | if (error) | |
1092 | goto out; | |
1093 | ||
1094 | if (attr->ia_valid & ATTR_SIZE) | |
ff8f33c8 | 1095 | error = gfs2_setattr_size(inode, attr->ia_size); |
b3b94faa DT |
1096 | else if (attr->ia_valid & (ATTR_UID | ATTR_GID)) |
1097 | error = setattr_chown(inode, attr); | |
1098 | else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode)) | |
1099 | error = gfs2_acl_chmod(ip, attr); | |
1100 | else | |
1101 | error = gfs2_setattr_simple(ip, attr); | |
1102 | ||
a91ea69f | 1103 | out: |
b3b94faa | 1104 | gfs2_glock_dq_uninit(&i_gh); |
b3b94faa DT |
1105 | if (!error) |
1106 | mark_inode_dirty(inode); | |
b3b94faa DT |
1107 | return error; |
1108 | } | |
1109 | ||
1110 | /** | |
1111 | * gfs2_getattr - Read out an inode's attributes | |
26c1a574 | 1112 | * @mnt: The vfsmount the inode is being accessed from |
b3b94faa DT |
1113 | * @dentry: The dentry to stat |
1114 | * @stat: The inode's stats | |
1115 | * | |
dcf3dd85 SW |
1116 | * This may be called from the VFS directly, or from within GFS2 with the |
1117 | * inode locked, so we look to see if the glock is already locked and only | |
1118 | * lock the glock if its not already been done. Note that its the NFS | |
1119 | * readdirplus operation which causes this to be called (from filldir) | |
1120 | * with the glock already held. | |
1121 | * | |
b3b94faa DT |
1122 | * Returns: errno |
1123 | */ | |
1124 | ||
1125 | static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry, | |
1126 | struct kstat *stat) | |
1127 | { | |
1128 | struct inode *inode = dentry->d_inode; | |
feaa7bba | 1129 | struct gfs2_inode *ip = GFS2_I(inode); |
b3b94faa DT |
1130 | struct gfs2_holder gh; |
1131 | int error; | |
dcf3dd85 | 1132 | int unlock = 0; |
b3b94faa | 1133 | |
7afd88d9 | 1134 | if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) { |
dcf3dd85 SW |
1135 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); |
1136 | if (error) | |
1137 | return error; | |
1138 | unlock = 1; | |
b3b94faa DT |
1139 | } |
1140 | ||
dcf3dd85 | 1141 | generic_fillattr(inode, stat); |
d7c103d0 | 1142 | if (unlock) |
dcf3dd85 SW |
1143 | gfs2_glock_dq_uninit(&gh); |
1144 | ||
1145 | return 0; | |
b3b94faa DT |
1146 | } |
1147 | ||
1148 | static int gfs2_setxattr(struct dentry *dentry, const char *name, | |
1149 | const void *data, size_t size, int flags) | |
1150 | { | |
feaa7bba | 1151 | struct inode *inode = dentry->d_inode; |
40b78a32 SW |
1152 | struct gfs2_inode *ip = GFS2_I(inode); |
1153 | struct gfs2_holder gh; | |
1154 | int ret; | |
b3b94faa | 1155 | |
40b78a32 SW |
1156 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); |
1157 | ret = gfs2_glock_nq(&gh); | |
1158 | if (ret == 0) { | |
1159 | ret = generic_setxattr(dentry, name, data, size, flags); | |
1160 | gfs2_glock_dq(&gh); | |
1161 | } | |
1162 | gfs2_holder_uninit(&gh); | |
1163 | return ret; | |
b3b94faa DT |
1164 | } |
1165 | ||
1166 | static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name, | |
1167 | void *data, size_t size) | |
1168 | { | |
40b78a32 SW |
1169 | struct inode *inode = dentry->d_inode; |
1170 | struct gfs2_inode *ip = GFS2_I(inode); | |
1171 | struct gfs2_holder gh; | |
1172 | int ret; | |
b3b94faa | 1173 | |
40b78a32 SW |
1174 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); |
1175 | ret = gfs2_glock_nq(&gh); | |
1176 | if (ret == 0) { | |
1177 | ret = generic_getxattr(dentry, name, data, size); | |
1178 | gfs2_glock_dq(&gh); | |
1179 | } | |
1180 | gfs2_holder_uninit(&gh); | |
1181 | return ret; | |
b3b94faa DT |
1182 | } |
1183 | ||
1184 | static int gfs2_removexattr(struct dentry *dentry, const char *name) | |
1185 | { | |
40b78a32 SW |
1186 | struct inode *inode = dentry->d_inode; |
1187 | struct gfs2_inode *ip = GFS2_I(inode); | |
1188 | struct gfs2_holder gh; | |
1189 | int ret; | |
b3b94faa | 1190 | |
40b78a32 SW |
1191 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); |
1192 | ret = gfs2_glock_nq(&gh); | |
1193 | if (ret == 0) { | |
1194 | ret = generic_removexattr(dentry, name); | |
1195 | gfs2_glock_dq(&gh); | |
1196 | } | |
1197 | gfs2_holder_uninit(&gh); | |
1198 | return ret; | |
b3b94faa DT |
1199 | } |
1200 | ||
e9079cce SW |
1201 | static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
1202 | u64 start, u64 len) | |
1203 | { | |
1204 | struct gfs2_inode *ip = GFS2_I(inode); | |
1205 | struct gfs2_holder gh; | |
1206 | int ret; | |
1207 | ||
1208 | ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); | |
1209 | if (ret) | |
1210 | return ret; | |
1211 | ||
1212 | mutex_lock(&inode->i_mutex); | |
1213 | ||
1214 | ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh); | |
1215 | if (ret) | |
1216 | goto out; | |
1217 | ||
1218 | if (gfs2_is_stuffed(ip)) { | |
1219 | u64 phys = ip->i_no_addr << inode->i_blkbits; | |
1220 | u64 size = i_size_read(inode); | |
1221 | u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED| | |
1222 | FIEMAP_EXTENT_DATA_INLINE; | |
1223 | phys += sizeof(struct gfs2_dinode); | |
1224 | phys += start; | |
1225 | if (start + len > size) | |
1226 | len = size - start; | |
1227 | if (start < size) | |
1228 | ret = fiemap_fill_next_extent(fieinfo, start, phys, | |
1229 | len, flags); | |
1230 | if (ret == 1) | |
1231 | ret = 0; | |
1232 | } else { | |
1233 | ret = __generic_block_fiemap(inode, fieinfo, start, len, | |
1234 | gfs2_block_map); | |
1235 | } | |
1236 | ||
1237 | gfs2_glock_dq_uninit(&gh); | |
1238 | out: | |
1239 | mutex_unlock(&inode->i_mutex); | |
1240 | return ret; | |
1241 | } | |
1242 | ||
92e1d5be | 1243 | const struct inode_operations gfs2_file_iops = { |
e6305c43 | 1244 | .permission = gfs2_permission, |
b3b94faa DT |
1245 | .setattr = gfs2_setattr, |
1246 | .getattr = gfs2_getattr, | |
1247 | .setxattr = gfs2_setxattr, | |
1248 | .getxattr = gfs2_getxattr, | |
1249 | .listxattr = gfs2_listxattr, | |
1250 | .removexattr = gfs2_removexattr, | |
e9079cce | 1251 | .fiemap = gfs2_fiemap, |
b3b94faa DT |
1252 | }; |
1253 | ||
92e1d5be | 1254 | const struct inode_operations gfs2_dir_iops = { |
b3b94faa DT |
1255 | .create = gfs2_create, |
1256 | .lookup = gfs2_lookup, | |
1257 | .link = gfs2_link, | |
1258 | .unlink = gfs2_unlink, | |
1259 | .symlink = gfs2_symlink, | |
1260 | .mkdir = gfs2_mkdir, | |
855d23ce | 1261 | .rmdir = gfs2_unlink, |
b3b94faa DT |
1262 | .mknod = gfs2_mknod, |
1263 | .rename = gfs2_rename, | |
e6305c43 | 1264 | .permission = gfs2_permission, |
b3b94faa DT |
1265 | .setattr = gfs2_setattr, |
1266 | .getattr = gfs2_getattr, | |
1267 | .setxattr = gfs2_setxattr, | |
1268 | .getxattr = gfs2_getxattr, | |
1269 | .listxattr = gfs2_listxattr, | |
1270 | .removexattr = gfs2_removexattr, | |
e9079cce | 1271 | .fiemap = gfs2_fiemap, |
b3b94faa DT |
1272 | }; |
1273 | ||
92e1d5be | 1274 | const struct inode_operations gfs2_symlink_iops = { |
c177c2ac | 1275 | .readlink = generic_readlink, |
b3b94faa | 1276 | .follow_link = gfs2_follow_link, |
c177c2ac | 1277 | .put_link = gfs2_put_link, |
e6305c43 | 1278 | .permission = gfs2_permission, |
b3b94faa DT |
1279 | .setattr = gfs2_setattr, |
1280 | .getattr = gfs2_getattr, | |
1281 | .setxattr = gfs2_setxattr, | |
1282 | .getxattr = gfs2_getxattr, | |
1283 | .listxattr = gfs2_listxattr, | |
1284 | .removexattr = gfs2_removexattr, | |
e9079cce | 1285 | .fiemap = gfs2_fiemap, |
b3b94faa DT |
1286 | }; |
1287 |