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