1
2
3
4
5
6
7
8
9
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>
15#include <linux/mm.h>
16#include <linux/xattr.h>
17#include <linux/posix_acl.h>
18#include <linux/gfs2_ondisk.h>
19#include <linux/crc32.h>
20#include <linux/fiemap.h>
21#include <asm/uaccess.h>
22
23#include "gfs2.h"
24#include "incore.h"
25#include "acl.h"
26#include "bmap.h"
27#include "dir.h"
28#include "xattr.h"
29#include "glock.h"
30#include "inode.h"
31#include "meta_io.h"
32#include "quota.h"
33#include "rgrp.h"
34#include "trans.h"
35#include "util.h"
36#include "super.h"
37
38
39
40
41
42
43
44
45
46
47static int gfs2_create(struct inode *dir, struct dentry *dentry,
48 int mode, struct nameidata *nd)
49{
50 struct gfs2_inode *dip = GFS2_I(dir);
51 struct gfs2_sbd *sdp = GFS2_SB(dir);
52 struct gfs2_holder ghs[2];
53 struct inode *inode;
54
55 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
56
57 for (;;) {
58 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
59 if (!IS_ERR(inode)) {
60 gfs2_trans_end(sdp);
61 if (dip->i_alloc->al_rgd)
62 gfs2_inplace_release(dip);
63 gfs2_quota_unlock(dip);
64 gfs2_alloc_put(dip);
65 gfs2_glock_dq_uninit_m(2, ghs);
66 mark_inode_dirty(inode);
67 break;
68 } else if (PTR_ERR(inode) != -EEXIST ||
69 (nd && nd->flags & LOOKUP_EXCL)) {
70 gfs2_holder_uninit(ghs);
71 return PTR_ERR(inode);
72 }
73
74 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
75 if (inode) {
76 if (!IS_ERR(inode)) {
77 gfs2_holder_uninit(ghs);
78 break;
79 } else {
80 gfs2_holder_uninit(ghs);
81 return PTR_ERR(inode);
82 }
83 }
84 }
85
86 d_instantiate(dentry, inode);
87
88 return 0;
89}
90
91
92
93
94
95
96
97
98
99
100
101
102static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
103 struct nameidata *nd)
104{
105 struct inode *inode = NULL;
106
107 dentry->d_op = &gfs2_dops;
108
109 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
110 if (inode && IS_ERR(inode))
111 return ERR_CAST(inode);
112
113 if (inode) {
114 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
115 struct gfs2_holder gh;
116 int error;
117 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
118 if (error) {
119 iput(inode);
120 return ERR_PTR(error);
121 }
122 gfs2_glock_dq_uninit(&gh);
123 return d_splice_alias(inode, dentry);
124 }
125 d_add(dentry, inode);
126
127 return NULL;
128}
129
130
131
132
133
134
135
136
137
138
139
140
141
142static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
143 struct dentry *dentry)
144{
145 struct gfs2_inode *dip = GFS2_I(dir);
146 struct gfs2_sbd *sdp = GFS2_SB(dir);
147 struct inode *inode = old_dentry->d_inode;
148 struct gfs2_inode *ip = GFS2_I(inode);
149 struct gfs2_holder ghs[2];
150 int alloc_required;
151 int error;
152
153 if (S_ISDIR(inode->i_mode))
154 return -EPERM;
155
156 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
157 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
158
159 error = gfs2_glock_nq(ghs);
160 if (error)
161 goto out_parent;
162
163 error = gfs2_glock_nq(ghs + 1);
164 if (error)
165 goto out_child;
166
167 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
168 if (error)
169 goto out_gunlock;
170
171 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
172 switch (error) {
173 case -ENOENT:
174 break;
175 case 0:
176 error = -EEXIST;
177 default:
178 goto out_gunlock;
179 }
180
181 error = -EINVAL;
182 if (!dip->i_inode.i_nlink)
183 goto out_gunlock;
184 error = -EFBIG;
185 if (dip->i_entries == (u32)-1)
186 goto out_gunlock;
187 error = -EPERM;
188 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
189 goto out_gunlock;
190 error = -EINVAL;
191 if (!ip->i_inode.i_nlink)
192 goto out_gunlock;
193 error = -EMLINK;
194 if (ip->i_inode.i_nlink == (u32)-1)
195 goto out_gunlock;
196
197 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
198 if (error < 0)
199 goto out_gunlock;
200 error = 0;
201
202 if (alloc_required) {
203 struct gfs2_alloc *al = gfs2_alloc_get(dip);
204 if (!al) {
205 error = -ENOMEM;
206 goto out_gunlock;
207 }
208
209 error = gfs2_quota_lock_check(dip);
210 if (error)
211 goto out_alloc;
212
213 al->al_requested = sdp->sd_max_dirres;
214
215 error = gfs2_inplace_reserve(dip);
216 if (error)
217 goto out_gunlock_q;
218
219 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
220 al->al_rgd->rd_length +
221 2 * RES_DINODE + RES_STATFS +
222 RES_QUOTA, 0);
223 if (error)
224 goto out_ipres;
225 } else {
226 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
227 if (error)
228 goto out_ipres;
229 }
230
231 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
232 if (error)
233 goto out_end_trans;
234
235 error = gfs2_change_nlink(ip, +1);
236
237out_end_trans:
238 gfs2_trans_end(sdp);
239out_ipres:
240 if (alloc_required)
241 gfs2_inplace_release(dip);
242out_gunlock_q:
243 if (alloc_required)
244 gfs2_quota_unlock(dip);
245out_alloc:
246 if (alloc_required)
247 gfs2_alloc_put(dip);
248out_gunlock:
249 gfs2_glock_dq(ghs + 1);
250out_child:
251 gfs2_glock_dq(ghs);
252out_parent:
253 gfs2_holder_uninit(ghs);
254 gfs2_holder_uninit(ghs + 1);
255 if (!error) {
256 atomic_inc(&inode->i_count);
257 d_instantiate(dentry, inode);
258 mark_inode_dirty(inode);
259 }
260 return error;
261}
262
263
264
265
266
267
268
269
270
271
272
273
274static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
275 const struct gfs2_inode *ip)
276{
277 int error;
278
279 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
280 return -EPERM;
281
282 if ((dip->i_inode.i_mode & S_ISVTX) &&
283 dip->i_inode.i_uid != current_fsuid() &&
284 ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
285 return -EPERM;
286
287 if (IS_APPEND(&dip->i_inode))
288 return -EPERM;
289
290 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
291 if (error)
292 return error;
293
294 error = gfs2_dir_check(&dip->i_inode, name, ip);
295 if (error)
296 return error;
297
298 return 0;
299}
300
301
302
303
304
305
306
307
308
309
310
311static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
312{
313 struct gfs2_inode *dip = GFS2_I(dir);
314 struct gfs2_sbd *sdp = GFS2_SB(dir);
315 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
316 struct gfs2_holder ghs[3];
317 struct gfs2_rgrpd *rgd;
318 struct gfs2_holder ri_gh;
319 int error;
320
321 error = gfs2_rindex_hold(sdp, &ri_gh);
322 if (error)
323 return error;
324
325 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
326 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
327
328 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
329 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
330
331
332 error = gfs2_glock_nq(ghs);
333 if (error)
334 goto out_parent;
335
336 error = gfs2_glock_nq(ghs + 1);
337 if (error)
338 goto out_child;
339
340 error = gfs2_glock_nq(ghs + 2);
341 if (error)
342 goto out_rgrp;
343
344 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
345 if (error)
346 goto out_gunlock;
347
348 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
349 if (error)
350 goto out_gunlock;
351
352 error = gfs2_dir_del(dip, &dentry->d_name);
353 if (error)
354 goto out_end_trans;
355
356 error = gfs2_change_nlink(ip, -1);
357
358out_end_trans:
359 gfs2_trans_end(sdp);
360out_gunlock:
361 gfs2_glock_dq(ghs + 2);
362out_rgrp:
363 gfs2_holder_uninit(ghs + 2);
364 gfs2_glock_dq(ghs + 1);
365out_child:
366 gfs2_holder_uninit(ghs + 1);
367 gfs2_glock_dq(ghs);
368out_parent:
369 gfs2_holder_uninit(ghs);
370 gfs2_glock_dq_uninit(&ri_gh);
371 return error;
372}
373
374
375
376
377
378
379
380
381
382
383static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
384 const char *symname)
385{
386 struct gfs2_inode *dip = GFS2_I(dir), *ip;
387 struct gfs2_sbd *sdp = GFS2_SB(dir);
388 struct gfs2_holder ghs[2];
389 struct inode *inode;
390 struct buffer_head *dibh;
391 int size;
392 int error;
393
394
395 size = strlen(symname);
396 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
397 return -ENAMETOOLONG;
398
399 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
400
401 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
402 if (IS_ERR(inode)) {
403 gfs2_holder_uninit(ghs);
404 return PTR_ERR(inode);
405 }
406
407 ip = ghs[1].gh_gl->gl_object;
408
409 ip->i_disksize = size;
410 i_size_write(inode, size);
411
412 error = gfs2_meta_inode_buffer(ip, &dibh);
413
414 if (!gfs2_assert_withdraw(sdp, !error)) {
415 gfs2_dinode_out(ip, dibh->b_data);
416 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
417 size);
418 brelse(dibh);
419 }
420
421 gfs2_trans_end(sdp);
422 if (dip->i_alloc->al_rgd)
423 gfs2_inplace_release(dip);
424 gfs2_quota_unlock(dip);
425 gfs2_alloc_put(dip);
426
427 gfs2_glock_dq_uninit_m(2, ghs);
428
429 d_instantiate(dentry, inode);
430 mark_inode_dirty(inode);
431
432 return 0;
433}
434
435
436
437
438
439
440
441
442
443
444static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
445{
446 struct gfs2_inode *dip = GFS2_I(dir), *ip;
447 struct gfs2_sbd *sdp = GFS2_SB(dir);
448 struct gfs2_holder ghs[2];
449 struct inode *inode;
450 struct buffer_head *dibh;
451 int error;
452
453 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
454
455 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
456 if (IS_ERR(inode)) {
457 gfs2_holder_uninit(ghs);
458 return PTR_ERR(inode);
459 }
460
461 ip = ghs[1].gh_gl->gl_object;
462
463 ip->i_inode.i_nlink = 2;
464 ip->i_disksize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
465 ip->i_diskflags |= GFS2_DIF_JDATA;
466 ip->i_entries = 2;
467
468 error = gfs2_meta_inode_buffer(ip, &dibh);
469
470 if (!gfs2_assert_withdraw(sdp, !error)) {
471 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
472 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
473 struct qstr str;
474
475 gfs2_str2qstr(&str, ".");
476 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
477 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
478 dent->de_inum = di->di_num;
479 dent->de_type = cpu_to_be16(DT_DIR);
480 di->di_entries = cpu_to_be32(1);
481
482 gfs2_str2qstr(&str, "..");
483 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
484 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
485
486 gfs2_inum_out(dip, dent);
487 dent->de_type = cpu_to_be16(DT_DIR);
488
489 gfs2_dinode_out(ip, di);
490
491 brelse(dibh);
492 }
493
494 error = gfs2_change_nlink(dip, +1);
495 gfs2_assert_withdraw(sdp, !error);
496
497 gfs2_trans_end(sdp);
498 if (dip->i_alloc->al_rgd)
499 gfs2_inplace_release(dip);
500 gfs2_quota_unlock(dip);
501 gfs2_alloc_put(dip);
502
503 gfs2_glock_dq_uninit_m(2, ghs);
504
505 d_instantiate(dentry, inode);
506 mark_inode_dirty(inode);
507
508 return 0;
509}
510
511
512
513
514
515
516
517
518
519
520
521
522static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
523 struct gfs2_inode *ip)
524{
525 struct qstr dotname;
526 int error;
527
528 if (ip->i_entries != 2) {
529 if (gfs2_consist_inode(ip))
530 gfs2_dinode_print(ip);
531 return -EIO;
532 }
533
534 error = gfs2_dir_del(dip, name);
535 if (error)
536 return error;
537
538 error = gfs2_change_nlink(dip, -1);
539 if (error)
540 return error;
541
542 gfs2_str2qstr(&dotname, ".");
543 error = gfs2_dir_del(ip, &dotname);
544 if (error)
545 return error;
546
547 gfs2_str2qstr(&dotname, "..");
548 error = gfs2_dir_del(ip, &dotname);
549 if (error)
550 return error;
551
552
553 error = gfs2_change_nlink(ip, -1);
554 if (error)
555 return error;
556
557 error = gfs2_change_nlink(ip, -1);
558 if (error)
559 return error;
560
561 return error;
562}
563
564
565
566
567
568
569
570
571
572
573
574static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
575{
576 struct gfs2_inode *dip = GFS2_I(dir);
577 struct gfs2_sbd *sdp = GFS2_SB(dir);
578 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
579 struct gfs2_holder ghs[3];
580 struct gfs2_rgrpd *rgd;
581 struct gfs2_holder ri_gh;
582 int error;
583
584 error = gfs2_rindex_hold(sdp, &ri_gh);
585 if (error)
586 return error;
587 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
588 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
589
590 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
591 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
592
593 error = gfs2_glock_nq(ghs);
594 if (error)
595 goto out_parent;
596
597 error = gfs2_glock_nq(ghs + 1);
598 if (error)
599 goto out_child;
600
601 error = gfs2_glock_nq(ghs + 2);
602 if (error)
603 goto out_rgrp;
604
605 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
606 if (error)
607 goto out_gunlock;
608
609 if (ip->i_entries < 2) {
610 if (gfs2_consist_inode(ip))
611 gfs2_dinode_print(ip);
612 error = -EIO;
613 goto out_gunlock;
614 }
615 if (ip->i_entries > 2) {
616 error = -ENOTEMPTY;
617 goto out_gunlock;
618 }
619
620 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
621 if (error)
622 goto out_gunlock;
623
624 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
625
626 gfs2_trans_end(sdp);
627
628out_gunlock:
629 gfs2_glock_dq(ghs + 2);
630out_rgrp:
631 gfs2_holder_uninit(ghs + 2);
632 gfs2_glock_dq(ghs + 1);
633out_child:
634 gfs2_holder_uninit(ghs + 1);
635 gfs2_glock_dq(ghs);
636out_parent:
637 gfs2_holder_uninit(ghs);
638 gfs2_glock_dq_uninit(&ri_gh);
639 return error;
640}
641
642
643
644
645
646
647
648
649
650
651static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
652 dev_t dev)
653{
654 struct gfs2_inode *dip = GFS2_I(dir);
655 struct gfs2_sbd *sdp = GFS2_SB(dir);
656 struct gfs2_holder ghs[2];
657 struct inode *inode;
658
659 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
660
661 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
662 if (IS_ERR(inode)) {
663 gfs2_holder_uninit(ghs);
664 return PTR_ERR(inode);
665 }
666
667 gfs2_trans_end(sdp);
668 if (dip->i_alloc->al_rgd)
669 gfs2_inplace_release(dip);
670 gfs2_quota_unlock(dip);
671 gfs2_alloc_put(dip);
672
673 gfs2_glock_dq_uninit_m(2, ghs);
674
675 d_instantiate(dentry, inode);
676 mark_inode_dirty(inode);
677
678 return 0;
679}
680
681
682
683
684
685
686
687
688
689
690
691
692static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
693{
694 struct inode *dir = &to->i_inode;
695 struct super_block *sb = dir->i_sb;
696 struct inode *tmp;
697 struct qstr dotdot;
698 int error = 0;
699
700 gfs2_str2qstr(&dotdot, "..");
701
702 igrab(dir);
703
704 for (;;) {
705 if (dir == &this->i_inode) {
706 error = -EINVAL;
707 break;
708 }
709 if (dir == sb->s_root->d_inode) {
710 error = 0;
711 break;
712 }
713
714 tmp = gfs2_lookupi(dir, &dotdot, 1);
715 if (IS_ERR(tmp)) {
716 error = PTR_ERR(tmp);
717 break;
718 }
719
720 iput(dir);
721 dir = tmp;
722 }
723
724 iput(dir);
725
726 return error;
727}
728
729
730
731
732
733
734
735
736
737
738
739static int gfs2_rename(struct inode *odir, struct dentry *odentry,
740 struct inode *ndir, struct dentry *ndentry)
741{
742 struct gfs2_inode *odip = GFS2_I(odir);
743 struct gfs2_inode *ndip = GFS2_I(ndir);
744 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
745 struct gfs2_inode *nip = NULL;
746 struct gfs2_sbd *sdp = GFS2_SB(odir);
747 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
748 struct gfs2_rgrpd *nrgd;
749 unsigned int num_gh;
750 int dir_rename = 0;
751 int alloc_required;
752 unsigned int x;
753 int error;
754
755 if (ndentry->d_inode) {
756 nip = GFS2_I(ndentry->d_inode);
757 if (ip == nip)
758 return 0;
759 }
760
761
762 if (odip != ndip) {
763 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
764 0, &r_gh);
765 if (error)
766 goto out;
767
768 if (S_ISDIR(ip->i_inode.i_mode)) {
769 dir_rename = 1;
770
771 error = gfs2_ok_to_move(ip, ndip);
772 if (error)
773 goto out_gunlock_r;
774 }
775 }
776
777 num_gh = 1;
778 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
779 if (odip != ndip) {
780 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
781 num_gh++;
782 }
783 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
784 num_gh++;
785
786 if (nip) {
787 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
788 num_gh++;
789
790
791
792
793 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
794 if (nrgd)
795 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
796 }
797
798 for (x = 0; x < num_gh; x++) {
799 error = gfs2_glock_nq(ghs + x);
800 if (error)
801 goto out_gunlock;
802 }
803
804
805
806 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
807 if (error)
808 goto out_gunlock;
809
810
811
812 if (nip) {
813 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
814 if (error)
815 goto out_gunlock;
816
817 if (S_ISDIR(nip->i_inode.i_mode)) {
818 if (nip->i_entries < 2) {
819 if (gfs2_consist_inode(nip))
820 gfs2_dinode_print(nip);
821 error = -EIO;
822 goto out_gunlock;
823 }
824 if (nip->i_entries > 2) {
825 error = -ENOTEMPTY;
826 goto out_gunlock;
827 }
828 }
829 } else {
830 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
831 if (error)
832 goto out_gunlock;
833
834 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
835 switch (error) {
836 case -ENOENT:
837 error = 0;
838 break;
839 case 0:
840 error = -EEXIST;
841 default:
842 goto out_gunlock;
843 };
844
845 if (odip != ndip) {
846 if (!ndip->i_inode.i_nlink) {
847 error = -EINVAL;
848 goto out_gunlock;
849 }
850 if (ndip->i_entries == (u32)-1) {
851 error = -EFBIG;
852 goto out_gunlock;
853 }
854 if (S_ISDIR(ip->i_inode.i_mode) &&
855 ndip->i_inode.i_nlink == (u32)-1) {
856 error = -EMLINK;
857 goto out_gunlock;
858 }
859 }
860 }
861
862
863
864 if (dir_rename) {
865 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
866 if (error)
867 goto out_gunlock;
868 }
869
870 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
871 if (error < 0)
872 goto out_gunlock;
873 error = 0;
874
875 if (alloc_required) {
876 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
877 if (!al) {
878 error = -ENOMEM;
879 goto out_gunlock;
880 }
881
882 error = gfs2_quota_lock_check(ndip);
883 if (error)
884 goto out_alloc;
885
886 al->al_requested = sdp->sd_max_dirres;
887
888 error = gfs2_inplace_reserve(ndip);
889 if (error)
890 goto out_gunlock_q;
891
892 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
893 al->al_rgd->rd_length +
894 4 * RES_DINODE + 4 * RES_LEAF +
895 RES_STATFS + RES_QUOTA + 4, 0);
896 if (error)
897 goto out_ipreserv;
898 } else {
899 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
900 5 * RES_LEAF + 4, 0);
901 if (error)
902 goto out_gunlock;
903 }
904
905
906
907 if (nip) {
908 if (S_ISDIR(nip->i_inode.i_mode))
909 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
910 else {
911 error = gfs2_dir_del(ndip, &ndentry->d_name);
912 if (error)
913 goto out_end_trans;
914 error = gfs2_change_nlink(nip, -1);
915 }
916 if (error)
917 goto out_end_trans;
918 }
919
920 if (dir_rename) {
921 struct qstr name;
922 gfs2_str2qstr(&name, "..");
923
924 error = gfs2_change_nlink(ndip, +1);
925 if (error)
926 goto out_end_trans;
927 error = gfs2_change_nlink(odip, -1);
928 if (error)
929 goto out_end_trans;
930
931 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
932 if (error)
933 goto out_end_trans;
934 } else {
935 struct buffer_head *dibh;
936 error = gfs2_meta_inode_buffer(ip, &dibh);
937 if (error)
938 goto out_end_trans;
939 ip->i_inode.i_ctime = CURRENT_TIME;
940 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
941 gfs2_dinode_out(ip, dibh->b_data);
942 brelse(dibh);
943 }
944
945 error = gfs2_dir_del(odip, &odentry->d_name);
946 if (error)
947 goto out_end_trans;
948
949 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
950 if (error)
951 goto out_end_trans;
952
953out_end_trans:
954 gfs2_trans_end(sdp);
955out_ipreserv:
956 if (alloc_required)
957 gfs2_inplace_release(ndip);
958out_gunlock_q:
959 if (alloc_required)
960 gfs2_quota_unlock(ndip);
961out_alloc:
962 if (alloc_required)
963 gfs2_alloc_put(ndip);
964out_gunlock:
965 while (x--) {
966 gfs2_glock_dq(ghs + x);
967 gfs2_holder_uninit(ghs + x);
968 }
969out_gunlock_r:
970 if (r_gh.gh_gl)
971 gfs2_glock_dq_uninit(&r_gh);
972out:
973 return error;
974}
975
976
977
978
979
980
981
982
983
984
985
986
987
988static int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
989{
990 struct gfs2_holder i_gh;
991 struct buffer_head *dibh;
992 unsigned int x;
993 int error;
994
995 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
996 error = gfs2_glock_nq(&i_gh);
997 if (error) {
998 gfs2_holder_uninit(&i_gh);
999 return error;
1000 }
1001
1002 if (!ip->i_disksize) {
1003 gfs2_consist_inode(ip);
1004 error = -EIO;
1005 goto out;
1006 }
1007
1008 error = gfs2_meta_inode_buffer(ip, &dibh);
1009 if (error)
1010 goto out;
1011
1012 x = ip->i_disksize + 1;
1013 if (x > *len) {
1014 *buf = kmalloc(x, GFP_NOFS);
1015 if (!*buf) {
1016 error = -ENOMEM;
1017 goto out_brelse;
1018 }
1019 }
1020
1021 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1022 *len = x;
1023
1024out_brelse:
1025 brelse(dibh);
1026out:
1027 gfs2_glock_dq_uninit(&i_gh);
1028 return error;
1029}
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
1041 int user_size)
1042{
1043 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
1044 char array[GFS2_FAST_NAME_SIZE], *buf = array;
1045 unsigned int len = GFS2_FAST_NAME_SIZE;
1046 int error;
1047
1048 error = gfs2_readlinki(ip, &buf, &len);
1049 if (error)
1050 return error;
1051
1052 if (user_size > len - 1)
1053 user_size = len - 1;
1054
1055 if (copy_to_user(user_buf, buf, user_size))
1056 error = -EFAULT;
1057 else
1058 error = user_size;
1059
1060 if (buf != array)
1061 kfree(buf);
1062
1063 return error;
1064}
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
1078{
1079 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
1080 char array[GFS2_FAST_NAME_SIZE], *buf = array;
1081 unsigned int len = GFS2_FAST_NAME_SIZE;
1082 int error;
1083
1084 error = gfs2_readlinki(ip, &buf, &len);
1085 if (!error) {
1086 error = vfs_follow_link(nd, buf);
1087 if (buf != array)
1088 kfree(buf);
1089 }
1090
1091 return ERR_PTR(error);
1092}
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107int gfs2_permission(struct inode *inode, int mask)
1108{
1109 struct gfs2_inode *ip = GFS2_I(inode);
1110 struct gfs2_holder i_gh;
1111 int error;
1112 int unlock = 0;
1113
1114 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1115 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1116 if (error)
1117 return error;
1118 unlock = 1;
1119 }
1120
1121 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1122 error = -EACCES;
1123 else
1124 error = generic_permission(inode, mask, gfs2_check_acl);
1125 if (unlock)
1126 gfs2_glock_dq_uninit(&i_gh);
1127
1128 return error;
1129}
1130
1131static int setattr_size(struct inode *inode, struct iattr *attr)
1132{
1133 struct gfs2_inode *ip = GFS2_I(inode);
1134 struct gfs2_sbd *sdp = GFS2_SB(inode);
1135 int error;
1136
1137 if (attr->ia_size != ip->i_disksize) {
1138 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1139 if (error)
1140 return error;
1141 error = vmtruncate(inode, attr->ia_size);
1142 gfs2_trans_end(sdp);
1143 if (error)
1144 return error;
1145 }
1146
1147 error = gfs2_truncatei(ip, attr->ia_size);
1148 if (error && (inode->i_size != ip->i_disksize))
1149 i_size_write(inode, ip->i_disksize);
1150
1151 return error;
1152}
1153
1154static int setattr_chown(struct inode *inode, struct iattr *attr)
1155{
1156 struct gfs2_inode *ip = GFS2_I(inode);
1157 struct gfs2_sbd *sdp = GFS2_SB(inode);
1158 struct buffer_head *dibh;
1159 u32 ouid, ogid, nuid, ngid;
1160 int error;
1161
1162 ouid = inode->i_uid;
1163 ogid = inode->i_gid;
1164 nuid = attr->ia_uid;
1165 ngid = attr->ia_gid;
1166
1167 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1168 ouid = nuid = NO_QUOTA_CHANGE;
1169 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1170 ogid = ngid = NO_QUOTA_CHANGE;
1171
1172 if (!gfs2_alloc_get(ip))
1173 return -ENOMEM;
1174
1175 error = gfs2_quota_lock(ip, nuid, ngid);
1176 if (error)
1177 goto out_alloc;
1178
1179 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1180 error = gfs2_quota_check(ip, nuid, ngid);
1181 if (error)
1182 goto out_gunlock_q;
1183 }
1184
1185 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1186 if (error)
1187 goto out_gunlock_q;
1188
1189 error = gfs2_meta_inode_buffer(ip, &dibh);
1190 if (error)
1191 goto out_end_trans;
1192
1193 error = inode_setattr(inode, attr);
1194 gfs2_assert_warn(sdp, !error);
1195
1196 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1197 gfs2_dinode_out(ip, dibh->b_data);
1198 brelse(dibh);
1199
1200 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1201 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1202 gfs2_quota_change(ip, -blocks, ouid, ogid);
1203 gfs2_quota_change(ip, blocks, nuid, ngid);
1204 }
1205
1206out_end_trans:
1207 gfs2_trans_end(sdp);
1208out_gunlock_q:
1209 gfs2_quota_unlock(ip);
1210out_alloc:
1211 gfs2_alloc_put(ip);
1212 return error;
1213}
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1227{
1228 struct inode *inode = dentry->d_inode;
1229 struct gfs2_inode *ip = GFS2_I(inode);
1230 struct gfs2_holder i_gh;
1231 int error;
1232
1233 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1234 if (error)
1235 return error;
1236
1237 error = -EPERM;
1238 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1239 goto out;
1240
1241 error = inode_change_ok(inode, attr);
1242 if (error)
1243 goto out;
1244
1245 if (attr->ia_valid & ATTR_SIZE)
1246 error = setattr_size(inode, attr);
1247 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1248 error = setattr_chown(inode, attr);
1249 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1250 error = gfs2_acl_chmod(ip, attr);
1251 else
1252 error = gfs2_setattr_simple(ip, attr);
1253
1254out:
1255 gfs2_glock_dq_uninit(&i_gh);
1256 if (!error)
1257 mark_inode_dirty(inode);
1258 return error;
1259}
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1277 struct kstat *stat)
1278{
1279 struct inode *inode = dentry->d_inode;
1280 struct gfs2_inode *ip = GFS2_I(inode);
1281 struct gfs2_holder gh;
1282 int error;
1283 int unlock = 0;
1284
1285 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1286 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1287 if (error)
1288 return error;
1289 unlock = 1;
1290 }
1291
1292 generic_fillattr(inode, stat);
1293 if (unlock)
1294 gfs2_glock_dq_uninit(&gh);
1295
1296 return 0;
1297}
1298
1299static int gfs2_setxattr(struct dentry *dentry, const char *name,
1300 const void *data, size_t size, int flags)
1301{
1302 struct inode *inode = dentry->d_inode;
1303 struct gfs2_inode *ip = GFS2_I(inode);
1304 struct gfs2_holder gh;
1305 int ret;
1306
1307 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1308 ret = gfs2_glock_nq(&gh);
1309 if (ret == 0) {
1310 ret = generic_setxattr(dentry, name, data, size, flags);
1311 gfs2_glock_dq(&gh);
1312 }
1313 gfs2_holder_uninit(&gh);
1314 return ret;
1315}
1316
1317static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1318 void *data, size_t size)
1319{
1320 struct inode *inode = dentry->d_inode;
1321 struct gfs2_inode *ip = GFS2_I(inode);
1322 struct gfs2_holder gh;
1323 int ret;
1324
1325 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1326 ret = gfs2_glock_nq(&gh);
1327 if (ret == 0) {
1328 ret = generic_getxattr(dentry, name, data, size);
1329 gfs2_glock_dq(&gh);
1330 }
1331 gfs2_holder_uninit(&gh);
1332 return ret;
1333}
1334
1335static int gfs2_removexattr(struct dentry *dentry, const char *name)
1336{
1337 struct inode *inode = dentry->d_inode;
1338 struct gfs2_inode *ip = GFS2_I(inode);
1339 struct gfs2_holder gh;
1340 int ret;
1341
1342 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1343 ret = gfs2_glock_nq(&gh);
1344 if (ret == 0) {
1345 ret = generic_removexattr(dentry, name);
1346 gfs2_glock_dq(&gh);
1347 }
1348 gfs2_holder_uninit(&gh);
1349 return ret;
1350}
1351
1352static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1353 u64 start, u64 len)
1354{
1355 struct gfs2_inode *ip = GFS2_I(inode);
1356 struct gfs2_holder gh;
1357 int ret;
1358
1359 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1360 if (ret)
1361 return ret;
1362
1363 mutex_lock(&inode->i_mutex);
1364
1365 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1366 if (ret)
1367 goto out;
1368
1369 if (gfs2_is_stuffed(ip)) {
1370 u64 phys = ip->i_no_addr << inode->i_blkbits;
1371 u64 size = i_size_read(inode);
1372 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1373 FIEMAP_EXTENT_DATA_INLINE;
1374 phys += sizeof(struct gfs2_dinode);
1375 phys += start;
1376 if (start + len > size)
1377 len = size - start;
1378 if (start < size)
1379 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1380 len, flags);
1381 if (ret == 1)
1382 ret = 0;
1383 } else {
1384 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1385 gfs2_block_map);
1386 }
1387
1388 gfs2_glock_dq_uninit(&gh);
1389out:
1390 mutex_unlock(&inode->i_mutex);
1391 return ret;
1392}
1393
1394const struct inode_operations gfs2_file_iops = {
1395 .permission = gfs2_permission,
1396 .setattr = gfs2_setattr,
1397 .getattr = gfs2_getattr,
1398 .setxattr = gfs2_setxattr,
1399 .getxattr = gfs2_getxattr,
1400 .listxattr = gfs2_listxattr,
1401 .removexattr = gfs2_removexattr,
1402 .fiemap = gfs2_fiemap,
1403};
1404
1405const struct inode_operations gfs2_dir_iops = {
1406 .create = gfs2_create,
1407 .lookup = gfs2_lookup,
1408 .link = gfs2_link,
1409 .unlink = gfs2_unlink,
1410 .symlink = gfs2_symlink,
1411 .mkdir = gfs2_mkdir,
1412 .rmdir = gfs2_rmdir,
1413 .mknod = gfs2_mknod,
1414 .rename = gfs2_rename,
1415 .permission = gfs2_permission,
1416 .setattr = gfs2_setattr,
1417 .getattr = gfs2_getattr,
1418 .setxattr = gfs2_setxattr,
1419 .getxattr = gfs2_getxattr,
1420 .listxattr = gfs2_listxattr,
1421 .removexattr = gfs2_removexattr,
1422 .fiemap = gfs2_fiemap,
1423};
1424
1425const struct inode_operations gfs2_symlink_iops = {
1426 .readlink = gfs2_readlink,
1427 .follow_link = gfs2_follow_link,
1428 .permission = gfs2_permission,
1429 .setattr = gfs2_setattr,
1430 .getattr = gfs2_getattr,
1431 .setxattr = gfs2_setxattr,
1432 .getxattr = gfs2_getxattr,
1433 .listxattr = gfs2_listxattr,
1434 .removexattr = gfs2_removexattr,
1435 .fiemap = gfs2_fiemap,
1436};
1437
1438