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