]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/gfs2/ops_inode.c
[GFS2] Fix unlink deadlocks
[mirror_ubuntu-artful-kernel.git] / fs / gfs2 / ops_inode.c
1 /*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
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
7 * of the GNU General Public License version 2.
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/namei.h>
16 #include <linux/utsname.h>
17 #include <linux/mm.h>
18 #include <linux/xattr.h>
19 #include <linux/posix_acl.h>
20 #include <linux/gfs2_ondisk.h>
21 #include <linux/crc32.h>
22 #include <linux/lm_interface.h>
23 #include <asm/uaccess.h>
24
25 #include "gfs2.h"
26 #include "incore.h"
27 #include "acl.h"
28 #include "bmap.h"
29 #include "dir.h"
30 #include "eaops.h"
31 #include "eattr.h"
32 #include "glock.h"
33 #include "inode.h"
34 #include "meta_io.h"
35 #include "ops_dentry.h"
36 #include "ops_inode.h"
37 #include "quota.h"
38 #include "rgrp.h"
39 #include "trans.h"
40 #include "util.h"
41
42 /**
43 * gfs2_create - Create a file
44 * @dir: The directory in which to create the file
45 * @dentry: The dentry of the new file
46 * @mode: The mode of the new file
47 *
48 * Returns: errno
49 */
50
51 static int gfs2_create(struct inode *dir, struct dentry *dentry,
52 int mode, struct nameidata *nd)
53 {
54 struct gfs2_inode *dip = GFS2_I(dir);
55 struct gfs2_sbd *sdp = GFS2_SB(dir);
56 struct gfs2_holder ghs[2];
57 struct inode *inode;
58
59 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
60
61 for (;;) {
62 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
63 if (!IS_ERR(inode)) {
64 gfs2_trans_end(sdp);
65 if (dip->i_alloc.al_rgd)
66 gfs2_inplace_release(dip);
67 gfs2_quota_unlock(dip);
68 gfs2_alloc_put(dip);
69 gfs2_glock_dq_uninit_m(2, ghs);
70 mark_inode_dirty(inode);
71 break;
72 } else if (PTR_ERR(inode) != -EEXIST ||
73 (nd->intent.open.flags & O_EXCL)) {
74 gfs2_holder_uninit(ghs);
75 return PTR_ERR(inode);
76 }
77
78 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
79 if (inode) {
80 if (!IS_ERR(inode)) {
81 gfs2_holder_uninit(ghs);
82 break;
83 } else {
84 gfs2_holder_uninit(ghs);
85 return PTR_ERR(inode);
86 }
87 }
88 }
89
90 d_instantiate(dentry, inode);
91
92 return 0;
93 }
94
95 /**
96 * gfs2_lookup - Look up a filename in a directory and return its inode
97 * @dir: The directory inode
98 * @dentry: The dentry of the new inode
99 * @nd: passed from Linux VFS, ignored by us
100 *
101 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
102 *
103 * Returns: errno
104 */
105
106 static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
107 struct nameidata *nd)
108 {
109 struct inode *inode = NULL;
110
111 dentry->d_op = &gfs2_dops;
112
113 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
114 if (inode && IS_ERR(inode))
115 return ERR_PTR(PTR_ERR(inode));
116
117 if (inode)
118 return d_splice_alias(inode, dentry);
119 d_add(dentry, inode);
120
121 return NULL;
122 }
123
124 /**
125 * gfs2_link - Link to a file
126 * @old_dentry: The inode to link
127 * @dir: Add link to this directory
128 * @dentry: The name of the link
129 *
130 * Link the inode in "old_dentry" into the directory "dir" with the
131 * name in "dentry".
132 *
133 * Returns: errno
134 */
135
136 static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
137 struct dentry *dentry)
138 {
139 struct gfs2_inode *dip = GFS2_I(dir);
140 struct gfs2_sbd *sdp = GFS2_SB(dir);
141 struct inode *inode = old_dentry->d_inode;
142 struct gfs2_inode *ip = GFS2_I(inode);
143 struct gfs2_holder ghs[2];
144 int alloc_required;
145 int error;
146
147 if (S_ISDIR(inode->i_mode))
148 return -EPERM;
149
150 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
151 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
152
153 error = gfs2_glock_nq_m(2, ghs);
154 if (error)
155 goto out;
156
157 error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
158 if (error)
159 goto out_gunlock;
160
161 error = gfs2_dir_search(dir, &dentry->d_name, NULL, NULL);
162 switch (error) {
163 case -ENOENT:
164 break;
165 case 0:
166 error = -EEXIST;
167 default:
168 goto out_gunlock;
169 }
170
171 error = -EINVAL;
172 if (!dip->i_inode.i_nlink)
173 goto out_gunlock;
174 error = -EFBIG;
175 if (dip->i_di.di_entries == (u32)-1)
176 goto out_gunlock;
177 error = -EPERM;
178 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
179 goto out_gunlock;
180 error = -EINVAL;
181 if (!ip->i_inode.i_nlink)
182 goto out_gunlock;
183 error = -EMLINK;
184 if (ip->i_inode.i_nlink == (u32)-1)
185 goto out_gunlock;
186
187 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
188 if (error < 0)
189 goto out_gunlock;
190 error = 0;
191
192 if (alloc_required) {
193 struct gfs2_alloc *al = gfs2_alloc_get(dip);
194
195 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
196 if (error)
197 goto out_alloc;
198
199 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
200 if (error)
201 goto out_gunlock_q;
202
203 al->al_requested = sdp->sd_max_dirres;
204
205 error = gfs2_inplace_reserve(dip);
206 if (error)
207 goto out_gunlock_q;
208
209 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
210 al->al_rgd->rd_ri.ri_length +
211 2 * RES_DINODE + RES_STATFS +
212 RES_QUOTA, 0);
213 if (error)
214 goto out_ipres;
215 } else {
216 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
217 if (error)
218 goto out_ipres;
219 }
220
221 error = gfs2_dir_add(dir, &dentry->d_name, &ip->i_num,
222 IF2DT(inode->i_mode));
223 if (error)
224 goto out_end_trans;
225
226 error = gfs2_change_nlink(ip, +1);
227
228 out_end_trans:
229 gfs2_trans_end(sdp);
230 out_ipres:
231 if (alloc_required)
232 gfs2_inplace_release(dip);
233 out_gunlock_q:
234 if (alloc_required)
235 gfs2_quota_unlock(dip);
236 out_alloc:
237 if (alloc_required)
238 gfs2_alloc_put(dip);
239 out_gunlock:
240 gfs2_glock_dq_m(2, ghs);
241 out:
242 gfs2_holder_uninit(ghs);
243 gfs2_holder_uninit(ghs + 1);
244 if (!error) {
245 atomic_inc(&inode->i_count);
246 d_instantiate(dentry, inode);
247 mark_inode_dirty(inode);
248 }
249 return error;
250 }
251
252 /**
253 * gfs2_unlink - Unlink a file
254 * @dir: The inode of the directory containing the file to unlink
255 * @dentry: The file itself
256 *
257 * Unlink a file. Call gfs2_unlinki()
258 *
259 * Returns: errno
260 */
261
262 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
263 {
264 struct gfs2_inode *dip = GFS2_I(dir);
265 struct gfs2_sbd *sdp = GFS2_SB(dir);
266 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
267 struct gfs2_holder ghs[3];
268 struct gfs2_rgrpd *rgd;
269 struct gfs2_holder ri_gh;
270 int error;
271
272 error = gfs2_rindex_hold(sdp, &ri_gh);
273 if (error)
274 return error;
275
276 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
277 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
278
279 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
280 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
281
282
283 error = gfs2_glock_nq_m(3, ghs);
284 if (error)
285 goto out;
286
287 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
288 if (error)
289 goto out_gunlock;
290
291 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
292 if (error)
293 goto out_gunlock;
294
295 error = gfs2_dir_del(dip, &dentry->d_name);
296 if (error)
297 goto out_end_trans;
298
299 error = gfs2_change_nlink(ip, -1);
300
301 out_end_trans:
302 gfs2_trans_end(sdp);
303 out_gunlock:
304 gfs2_glock_dq_m(3, ghs);
305 out:
306 gfs2_holder_uninit(ghs);
307 gfs2_holder_uninit(ghs + 1);
308 gfs2_holder_uninit(ghs + 2);
309 gfs2_glock_dq_uninit(&ri_gh);
310 return error;
311 }
312
313 /**
314 * gfs2_symlink - Create a symlink
315 * @dir: The directory to create the symlink in
316 * @dentry: The dentry to put the symlink in
317 * @symname: The thing which the link points to
318 *
319 * Returns: errno
320 */
321
322 static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
323 const char *symname)
324 {
325 struct gfs2_inode *dip = GFS2_I(dir), *ip;
326 struct gfs2_sbd *sdp = GFS2_SB(dir);
327 struct gfs2_holder ghs[2];
328 struct inode *inode;
329 struct buffer_head *dibh;
330 int size;
331 int error;
332
333 /* Must be stuffed with a null terminator for gfs2_follow_link() */
334 size = strlen(symname);
335 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
336 return -ENAMETOOLONG;
337
338 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
339
340 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
341 if (IS_ERR(inode)) {
342 gfs2_holder_uninit(ghs);
343 return PTR_ERR(inode);
344 }
345
346 ip = ghs[1].gh_gl->gl_object;
347
348 ip->i_di.di_size = size;
349
350 error = gfs2_meta_inode_buffer(ip, &dibh);
351
352 if (!gfs2_assert_withdraw(sdp, !error)) {
353 gfs2_dinode_out(ip, dibh->b_data);
354 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
355 size);
356 brelse(dibh);
357 }
358
359 gfs2_trans_end(sdp);
360 if (dip->i_alloc.al_rgd)
361 gfs2_inplace_release(dip);
362 gfs2_quota_unlock(dip);
363 gfs2_alloc_put(dip);
364
365 gfs2_glock_dq_uninit_m(2, ghs);
366
367 d_instantiate(dentry, inode);
368 mark_inode_dirty(inode);
369
370 return 0;
371 }
372
373 /**
374 * gfs2_mkdir - Make a directory
375 * @dir: The parent directory of the new one
376 * @dentry: The dentry of the new directory
377 * @mode: The mode of the new directory
378 *
379 * Returns: errno
380 */
381
382 static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
383 {
384 struct gfs2_inode *dip = GFS2_I(dir), *ip;
385 struct gfs2_sbd *sdp = GFS2_SB(dir);
386 struct gfs2_holder ghs[2];
387 struct inode *inode;
388 struct buffer_head *dibh;
389 int error;
390
391 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
392
393 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
394 if (IS_ERR(inode)) {
395 gfs2_holder_uninit(ghs);
396 return PTR_ERR(inode);
397 }
398
399 ip = ghs[1].gh_gl->gl_object;
400
401 ip->i_inode.i_nlink = 2;
402 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
403 ip->i_di.di_flags |= GFS2_DIF_JDATA;
404 ip->i_di.di_entries = 2;
405
406 error = gfs2_meta_inode_buffer(ip, &dibh);
407
408 if (!gfs2_assert_withdraw(sdp, !error)) {
409 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
410 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
411 struct qstr str;
412
413 gfs2_str2qstr(&str, ".");
414 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
415 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
416 dent->de_inum = di->di_num; /* already GFS2 endian */
417 dent->de_type = cpu_to_be16(DT_DIR);
418 di->di_entries = cpu_to_be32(1);
419
420 gfs2_str2qstr(&str, "..");
421 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
422 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
423
424 gfs2_inum_out(&dip->i_num, &dent->de_inum);
425 dent->de_type = cpu_to_be16(DT_DIR);
426
427 gfs2_dinode_out(ip, di);
428
429 brelse(dibh);
430 }
431
432 error = gfs2_change_nlink(dip, +1);
433 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
434
435 gfs2_trans_end(sdp);
436 if (dip->i_alloc.al_rgd)
437 gfs2_inplace_release(dip);
438 gfs2_quota_unlock(dip);
439 gfs2_alloc_put(dip);
440
441 gfs2_glock_dq_uninit_m(2, ghs);
442
443 d_instantiate(dentry, inode);
444 mark_inode_dirty(inode);
445
446 return 0;
447 }
448
449 /**
450 * gfs2_rmdir - Remove a directory
451 * @dir: The parent directory of the directory to be removed
452 * @dentry: The dentry of the directory to remove
453 *
454 * Remove a directory. Call gfs2_rmdiri()
455 *
456 * Returns: errno
457 */
458
459 static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
460 {
461 struct gfs2_inode *dip = GFS2_I(dir);
462 struct gfs2_sbd *sdp = GFS2_SB(dir);
463 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
464 struct gfs2_holder ghs[3];
465 struct gfs2_rgrpd *rgd;
466 struct gfs2_holder ri_gh;
467 int error;
468
469
470 error = gfs2_rindex_hold(sdp, &ri_gh);
471 if (error)
472 return error;
473 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
474 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
475
476 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
477 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
478
479 error = gfs2_glock_nq_m(3, ghs);
480 if (error)
481 goto out;
482
483 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
484 if (error)
485 goto out_gunlock;
486
487 if (ip->i_di.di_entries < 2) {
488 if (gfs2_consist_inode(ip))
489 gfs2_dinode_print(ip);
490 error = -EIO;
491 goto out_gunlock;
492 }
493 if (ip->i_di.di_entries > 2) {
494 error = -ENOTEMPTY;
495 goto out_gunlock;
496 }
497
498 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
499 if (error)
500 goto out_gunlock;
501
502 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
503
504 gfs2_trans_end(sdp);
505
506 out_gunlock:
507 gfs2_glock_dq_m(3, ghs);
508 out:
509 gfs2_holder_uninit(ghs);
510 gfs2_holder_uninit(ghs + 1);
511 gfs2_holder_uninit(ghs + 2);
512 gfs2_glock_dq_uninit(&ri_gh);
513 return error;
514 }
515
516 /**
517 * gfs2_mknod - Make a special file
518 * @dir: The directory in which the special file will reside
519 * @dentry: The dentry of the special file
520 * @mode: The mode of the special file
521 * @rdev: The device specification of the special file
522 *
523 */
524
525 static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
526 dev_t dev)
527 {
528 struct gfs2_inode *dip = GFS2_I(dir);
529 struct gfs2_sbd *sdp = GFS2_SB(dir);
530 struct gfs2_holder ghs[2];
531 struct inode *inode;
532
533 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
534
535 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
536 if (IS_ERR(inode)) {
537 gfs2_holder_uninit(ghs);
538 return PTR_ERR(inode);
539 }
540
541 gfs2_trans_end(sdp);
542 if (dip->i_alloc.al_rgd)
543 gfs2_inplace_release(dip);
544 gfs2_quota_unlock(dip);
545 gfs2_alloc_put(dip);
546
547 gfs2_glock_dq_uninit_m(2, ghs);
548
549 d_instantiate(dentry, inode);
550 mark_inode_dirty(inode);
551
552 return 0;
553 }
554
555 /**
556 * gfs2_rename - Rename a file
557 * @odir: Parent directory of old file name
558 * @odentry: The old dentry of the file
559 * @ndir: Parent directory of new file name
560 * @ndentry: The new dentry of the file
561 *
562 * Returns: errno
563 */
564
565 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
566 struct inode *ndir, struct dentry *ndentry)
567 {
568 struct gfs2_inode *odip = GFS2_I(odir);
569 struct gfs2_inode *ndip = GFS2_I(ndir);
570 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
571 struct gfs2_inode *nip = NULL;
572 struct gfs2_sbd *sdp = GFS2_SB(odir);
573 struct gfs2_holder ghs[5], r_gh;
574 struct gfs2_rgrpd *nrgd;
575 unsigned int num_gh;
576 int dir_rename = 0;
577 int alloc_required;
578 unsigned int x;
579 int error;
580
581 if (ndentry->d_inode) {
582 nip = GFS2_I(ndentry->d_inode);
583 if (ip == nip)
584 return 0;
585 }
586
587 /* Make sure we aren't trying to move a dirctory into it's subdir */
588
589 if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
590 dir_rename = 1;
591
592 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
593 &r_gh);
594 if (error)
595 goto out;
596
597 error = gfs2_ok_to_move(ip, ndip);
598 if (error)
599 goto out_gunlock_r;
600 }
601
602 num_gh = 1;
603 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
604 if (odip != ndip) {
605 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
606 num_gh++;
607 }
608 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
609 num_gh++;
610
611 if (nip) {
612 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
613 num_gh++;
614 /* grab the resource lock for unlink flag twiddling
615 * this is the case of the target file already existing
616 * so we unlink before doing the rename
617 */
618 nrgd = gfs2_blk2rgrpd(sdp, nip->i_num.no_addr);
619 if (nrgd)
620 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
621 }
622
623 error = gfs2_glock_nq_m(num_gh, ghs);
624 if (error)
625 goto out_uninit;
626
627 /* Check out the old directory */
628
629 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
630 if (error)
631 goto out_gunlock;
632
633 /* Check out the new directory */
634
635 if (nip) {
636 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
637 if (error)
638 goto out_gunlock;
639
640 if (S_ISDIR(nip->i_inode.i_mode)) {
641 if (nip->i_di.di_entries < 2) {
642 if (gfs2_consist_inode(nip))
643 gfs2_dinode_print(nip);
644 error = -EIO;
645 goto out_gunlock;
646 }
647 if (nip->i_di.di_entries > 2) {
648 error = -ENOTEMPTY;
649 goto out_gunlock;
650 }
651 }
652 } else {
653 error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
654 if (error)
655 goto out_gunlock;
656
657 error = gfs2_dir_search(ndir, &ndentry->d_name, NULL, NULL);
658 switch (error) {
659 case -ENOENT:
660 error = 0;
661 break;
662 case 0:
663 error = -EEXIST;
664 default:
665 goto out_gunlock;
666 };
667
668 if (odip != ndip) {
669 if (!ndip->i_inode.i_nlink) {
670 error = -EINVAL;
671 goto out_gunlock;
672 }
673 if (ndip->i_di.di_entries == (u32)-1) {
674 error = -EFBIG;
675 goto out_gunlock;
676 }
677 if (S_ISDIR(ip->i_inode.i_mode) &&
678 ndip->i_inode.i_nlink == (u32)-1) {
679 error = -EMLINK;
680 goto out_gunlock;
681 }
682 }
683 }
684
685 /* Check out the dir to be renamed */
686
687 if (dir_rename) {
688 error = permission(odentry->d_inode, MAY_WRITE, NULL);
689 if (error)
690 goto out_gunlock;
691 }
692
693 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
694 if (error < 0)
695 goto out_gunlock;
696 error = 0;
697
698 if (alloc_required) {
699 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
700
701 error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
702 if (error)
703 goto out_alloc;
704
705 error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid);
706 if (error)
707 goto out_gunlock_q;
708
709 al->al_requested = sdp->sd_max_dirres;
710
711 error = gfs2_inplace_reserve(ndip);
712 if (error)
713 goto out_gunlock_q;
714
715 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
716 al->al_rgd->rd_ri.ri_length +
717 4 * RES_DINODE + 4 * RES_LEAF +
718 RES_STATFS + RES_QUOTA + 4, 0);
719 if (error)
720 goto out_ipreserv;
721 } else {
722 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
723 5 * RES_LEAF + 4, 0);
724 if (error)
725 goto out_gunlock;
726 }
727
728 /* Remove the target file, if it exists */
729
730 if (nip) {
731 if (S_ISDIR(nip->i_inode.i_mode))
732 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
733 else {
734 error = gfs2_dir_del(ndip, &ndentry->d_name);
735 if (error)
736 goto out_end_trans;
737 error = gfs2_change_nlink(nip, -1);
738 }
739 if (error)
740 goto out_end_trans;
741 }
742
743 if (dir_rename) {
744 struct qstr name;
745 gfs2_str2qstr(&name, "..");
746
747 error = gfs2_change_nlink(ndip, +1);
748 if (error)
749 goto out_end_trans;
750 error = gfs2_change_nlink(odip, -1);
751 if (error)
752 goto out_end_trans;
753
754 error = gfs2_dir_mvino(ip, &name, &ndip->i_num, DT_DIR);
755 if (error)
756 goto out_end_trans;
757 } else {
758 struct buffer_head *dibh;
759 error = gfs2_meta_inode_buffer(ip, &dibh);
760 if (error)
761 goto out_end_trans;
762 ip->i_inode.i_ctime = CURRENT_TIME_SEC;
763 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
764 gfs2_dinode_out(ip, dibh->b_data);
765 brelse(dibh);
766 }
767
768 error = gfs2_dir_del(odip, &odentry->d_name);
769 if (error)
770 goto out_end_trans;
771
772 error = gfs2_dir_add(ndir, &ndentry->d_name, &ip->i_num,
773 IF2DT(ip->i_inode.i_mode));
774 if (error)
775 goto out_end_trans;
776
777 out_end_trans:
778 gfs2_trans_end(sdp);
779 out_ipreserv:
780 if (alloc_required)
781 gfs2_inplace_release(ndip);
782 out_gunlock_q:
783 if (alloc_required)
784 gfs2_quota_unlock(ndip);
785 out_alloc:
786 if (alloc_required)
787 gfs2_alloc_put(ndip);
788 out_gunlock:
789 gfs2_glock_dq_m(num_gh, ghs);
790 out_uninit:
791 for (x = 0; x < num_gh; x++)
792 gfs2_holder_uninit(ghs + x);
793 out_gunlock_r:
794 if (dir_rename)
795 gfs2_glock_dq_uninit(&r_gh);
796 out:
797 return error;
798 }
799
800 /**
801 * gfs2_readlink - Read the value of a symlink
802 * @dentry: the symlink
803 * @buf: the buffer to read the symlink data into
804 * @size: the size of the buffer
805 *
806 * Returns: errno
807 */
808
809 static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
810 int user_size)
811 {
812 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
813 char array[GFS2_FAST_NAME_SIZE], *buf = array;
814 unsigned int len = GFS2_FAST_NAME_SIZE;
815 int error;
816
817 error = gfs2_readlinki(ip, &buf, &len);
818 if (error)
819 return error;
820
821 if (user_size > len - 1)
822 user_size = len - 1;
823
824 if (copy_to_user(user_buf, buf, user_size))
825 error = -EFAULT;
826 else
827 error = user_size;
828
829 if (buf != array)
830 kfree(buf);
831
832 return error;
833 }
834
835 /**
836 * gfs2_follow_link - Follow a symbolic link
837 * @dentry: The dentry of the link
838 * @nd: Data that we pass to vfs_follow_link()
839 *
840 * This can handle symlinks of any size. It is optimised for symlinks
841 * under GFS2_FAST_NAME_SIZE.
842 *
843 * Returns: 0 on success or error code
844 */
845
846 static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
847 {
848 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
849 char array[GFS2_FAST_NAME_SIZE], *buf = array;
850 unsigned int len = GFS2_FAST_NAME_SIZE;
851 int error;
852
853 error = gfs2_readlinki(ip, &buf, &len);
854 if (!error) {
855 error = vfs_follow_link(nd, buf);
856 if (buf != array)
857 kfree(buf);
858 }
859
860 return ERR_PTR(error);
861 }
862
863 /**
864 * gfs2_permission -
865 * @inode:
866 * @mask:
867 * @nd: passed from Linux VFS, ignored by us
868 *
869 * This may be called from the VFS directly, or from within GFS2 with the
870 * inode locked, so we look to see if the glock is already locked and only
871 * lock the glock if its not already been done.
872 *
873 * Returns: errno
874 */
875
876 static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
877 {
878 struct gfs2_inode *ip = GFS2_I(inode);
879 struct gfs2_holder i_gh;
880 int error;
881 int unlock = 0;
882
883 if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
884 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
885 if (error)
886 return error;
887 unlock = 1;
888 }
889
890 error = generic_permission(inode, mask, gfs2_check_acl);
891 if (unlock)
892 gfs2_glock_dq_uninit(&i_gh);
893
894 return error;
895 }
896
897 static int setattr_size(struct inode *inode, struct iattr *attr)
898 {
899 struct gfs2_inode *ip = GFS2_I(inode);
900 int error;
901
902 if (attr->ia_size != ip->i_di.di_size) {
903 error = vmtruncate(inode, attr->ia_size);
904 if (error)
905 return error;
906 }
907
908 error = gfs2_truncatei(ip, attr->ia_size);
909 if (error)
910 return error;
911
912 return error;
913 }
914
915 static int setattr_chown(struct inode *inode, struct iattr *attr)
916 {
917 struct gfs2_inode *ip = GFS2_I(inode);
918 struct gfs2_sbd *sdp = GFS2_SB(inode);
919 struct buffer_head *dibh;
920 u32 ouid, ogid, nuid, ngid;
921 int error;
922
923 ouid = inode->i_uid;
924 ogid = inode->i_gid;
925 nuid = attr->ia_uid;
926 ngid = attr->ia_gid;
927
928 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
929 ouid = nuid = NO_QUOTA_CHANGE;
930 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
931 ogid = ngid = NO_QUOTA_CHANGE;
932
933 gfs2_alloc_get(ip);
934
935 error = gfs2_quota_lock(ip, nuid, ngid);
936 if (error)
937 goto out_alloc;
938
939 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
940 error = gfs2_quota_check(ip, nuid, ngid);
941 if (error)
942 goto out_gunlock_q;
943 }
944
945 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
946 if (error)
947 goto out_gunlock_q;
948
949 error = gfs2_meta_inode_buffer(ip, &dibh);
950 if (error)
951 goto out_end_trans;
952
953 error = inode_setattr(inode, attr);
954 gfs2_assert_warn(sdp, !error);
955
956 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
957 gfs2_dinode_out(ip, dibh->b_data);
958 brelse(dibh);
959
960 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
961 gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid);
962 gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid);
963 }
964
965 out_end_trans:
966 gfs2_trans_end(sdp);
967 out_gunlock_q:
968 gfs2_quota_unlock(ip);
969 out_alloc:
970 gfs2_alloc_put(ip);
971 return error;
972 }
973
974 /**
975 * gfs2_setattr - Change attributes on an inode
976 * @dentry: The dentry which is changing
977 * @attr: The structure describing the change
978 *
979 * The VFS layer wants to change one or more of an inodes attributes. Write
980 * that change out to disk.
981 *
982 * Returns: errno
983 */
984
985 static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
986 {
987 struct inode *inode = dentry->d_inode;
988 struct gfs2_inode *ip = GFS2_I(inode);
989 struct gfs2_holder i_gh;
990 int error;
991
992 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
993 if (error)
994 return error;
995
996 error = -EPERM;
997 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
998 goto out;
999
1000 error = inode_change_ok(inode, attr);
1001 if (error)
1002 goto out;
1003
1004 if (attr->ia_valid & ATTR_SIZE)
1005 error = setattr_size(inode, attr);
1006 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1007 error = setattr_chown(inode, attr);
1008 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1009 error = gfs2_acl_chmod(ip, attr);
1010 else
1011 error = gfs2_setattr_simple(ip, attr);
1012
1013 out:
1014 gfs2_glock_dq_uninit(&i_gh);
1015 if (!error)
1016 mark_inode_dirty(inode);
1017 return error;
1018 }
1019
1020 /**
1021 * gfs2_getattr - Read out an inode's attributes
1022 * @mnt: The vfsmount the inode is being accessed from
1023 * @dentry: The dentry to stat
1024 * @stat: The inode's stats
1025 *
1026 * This may be called from the VFS directly, or from within GFS2 with the
1027 * inode locked, so we look to see if the glock is already locked and only
1028 * lock the glock if its not already been done. Note that its the NFS
1029 * readdirplus operation which causes this to be called (from filldir)
1030 * with the glock already held.
1031 *
1032 * Returns: errno
1033 */
1034
1035 static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1036 struct kstat *stat)
1037 {
1038 struct inode *inode = dentry->d_inode;
1039 struct gfs2_inode *ip = GFS2_I(inode);
1040 struct gfs2_holder gh;
1041 int error;
1042 int unlock = 0;
1043
1044 if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
1045 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1046 if (error)
1047 return error;
1048 unlock = 1;
1049 }
1050
1051 generic_fillattr(inode, stat);
1052 if (unlock)
1053 gfs2_glock_dq_uninit(&gh);
1054
1055 return 0;
1056 }
1057
1058 static int gfs2_setxattr(struct dentry *dentry, const char *name,
1059 const void *data, size_t size, int flags)
1060 {
1061 struct inode *inode = dentry->d_inode;
1062 struct gfs2_ea_request er;
1063
1064 memset(&er, 0, sizeof(struct gfs2_ea_request));
1065 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1066 if (er.er_type == GFS2_EATYPE_UNUSED)
1067 return -EOPNOTSUPP;
1068 er.er_data = (char *)data;
1069 er.er_name_len = strlen(er.er_name);
1070 er.er_data_len = size;
1071 er.er_flags = flags;
1072
1073 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
1074
1075 return gfs2_ea_set(GFS2_I(inode), &er);
1076 }
1077
1078 static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1079 void *data, size_t size)
1080 {
1081 struct gfs2_ea_request er;
1082
1083 memset(&er, 0, sizeof(struct gfs2_ea_request));
1084 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1085 if (er.er_type == GFS2_EATYPE_UNUSED)
1086 return -EOPNOTSUPP;
1087 er.er_data = data;
1088 er.er_name_len = strlen(er.er_name);
1089 er.er_data_len = size;
1090
1091 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
1092 }
1093
1094 static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1095 {
1096 struct gfs2_ea_request er;
1097
1098 memset(&er, 0, sizeof(struct gfs2_ea_request));
1099 er.er_data = (size) ? buffer : NULL;
1100 er.er_data_len = size;
1101
1102 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
1103 }
1104
1105 static int gfs2_removexattr(struct dentry *dentry, const char *name)
1106 {
1107 struct gfs2_ea_request er;
1108
1109 memset(&er, 0, sizeof(struct gfs2_ea_request));
1110 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1111 if (er.er_type == GFS2_EATYPE_UNUSED)
1112 return -EOPNOTSUPP;
1113 er.er_name_len = strlen(er.er_name);
1114
1115 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
1116 }
1117
1118 struct inode_operations gfs2_file_iops = {
1119 .permission = gfs2_permission,
1120 .setattr = gfs2_setattr,
1121 .getattr = gfs2_getattr,
1122 .setxattr = gfs2_setxattr,
1123 .getxattr = gfs2_getxattr,
1124 .listxattr = gfs2_listxattr,
1125 .removexattr = gfs2_removexattr,
1126 };
1127
1128 struct inode_operations gfs2_dev_iops = {
1129 .permission = gfs2_permission,
1130 .setattr = gfs2_setattr,
1131 .getattr = gfs2_getattr,
1132 .setxattr = gfs2_setxattr,
1133 .getxattr = gfs2_getxattr,
1134 .listxattr = gfs2_listxattr,
1135 .removexattr = gfs2_removexattr,
1136 };
1137
1138 struct inode_operations gfs2_dir_iops = {
1139 .create = gfs2_create,
1140 .lookup = gfs2_lookup,
1141 .link = gfs2_link,
1142 .unlink = gfs2_unlink,
1143 .symlink = gfs2_symlink,
1144 .mkdir = gfs2_mkdir,
1145 .rmdir = gfs2_rmdir,
1146 .mknod = gfs2_mknod,
1147 .rename = gfs2_rename,
1148 .permission = gfs2_permission,
1149 .setattr = gfs2_setattr,
1150 .getattr = gfs2_getattr,
1151 .setxattr = gfs2_setxattr,
1152 .getxattr = gfs2_getxattr,
1153 .listxattr = gfs2_listxattr,
1154 .removexattr = gfs2_removexattr,
1155 };
1156
1157 struct inode_operations gfs2_symlink_iops = {
1158 .readlink = gfs2_readlink,
1159 .follow_link = gfs2_follow_link,
1160 .permission = gfs2_permission,
1161 .setattr = gfs2_setattr,
1162 .getattr = gfs2_getattr,
1163 .setxattr = gfs2_setxattr,
1164 .getxattr = gfs2_getxattr,
1165 .listxattr = gfs2_listxattr,
1166 .removexattr = gfs2_removexattr,
1167 };
1168