[GFS2] Only set inode flags when required
[pandora-kernel.git] / fs / gfs2 / inode.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4  *
5  * This copyrighted material is made available to anyone wishing to use,
6  * modify, copy, or redistribute it subject to the terms and conditions
7  * of the GNU General Public License version 2.
8  */
9
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/posix_acl.h>
16 #include <linux/sort.h>
17 #include <linux/gfs2_ondisk.h>
18 #include <linux/crc32.h>
19 #include <linux/lm_interface.h>
20 #include <linux/security.h>
21
22 #include "gfs2.h"
23 #include "incore.h"
24 #include "acl.h"
25 #include "bmap.h"
26 #include "dir.h"
27 #include "eattr.h"
28 #include "glock.h"
29 #include "glops.h"
30 #include "inode.h"
31 #include "log.h"
32 #include "meta_io.h"
33 #include "ops_address.h"
34 #include "ops_file.h"
35 #include "ops_inode.h"
36 #include "quota.h"
37 #include "rgrp.h"
38 #include "trans.h"
39 #include "util.h"
40
41 /**
42  * gfs2_inode_attr_in - Copy attributes from the dinode into the VFS inode
43  * @ip: The GFS2 inode (with embedded disk inode data)
44  * @inode:  The Linux VFS inode
45  *
46  */
47
48 void gfs2_inode_attr_in(struct gfs2_inode *ip)
49 {
50         struct inode *inode = &ip->i_inode;
51         struct gfs2_dinode_host *di = &ip->i_di;
52
53         inode->i_ino = ip->i_num.no_addr;
54         i_size_write(inode, di->di_size);
55         inode->i_blocks = di->di_blocks <<
56                 (GFS2_SB(inode)->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT);
57 }
58
59 static int iget_test(struct inode *inode, void *opaque)
60 {
61         struct gfs2_inode *ip = GFS2_I(inode);
62         struct gfs2_inum_host *inum = opaque;
63
64         if (ip && ip->i_num.no_addr == inum->no_addr)
65                 return 1;
66
67         return 0;
68 }
69
70 static int iget_set(struct inode *inode, void *opaque)
71 {
72         struct gfs2_inode *ip = GFS2_I(inode);
73         struct gfs2_inum_host *inum = opaque;
74
75         ip->i_num = *inum;
76         return 0;
77 }
78
79 struct inode *gfs2_ilookup(struct super_block *sb, struct gfs2_inum_host *inum)
80 {
81         return ilookup5(sb, (unsigned long)inum->no_formal_ino,
82                         iget_test, inum);
83 }
84
85 static struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum_host *inum)
86 {
87         return iget5_locked(sb, (unsigned long)inum->no_formal_ino,
88                      iget_test, iget_set, inum);
89 }
90
91 /**
92  * gfs2_inode_lookup - Lookup an inode
93  * @sb: The super block
94  * @inum: The inode number
95  * @type: The type of the inode
96  *
97  * Returns: A VFS inode, or an error
98  */
99
100 struct inode *gfs2_inode_lookup(struct super_block *sb, struct gfs2_inum_host *inum, unsigned int type)
101 {
102         struct inode *inode = gfs2_iget(sb, inum);
103         struct gfs2_inode *ip = GFS2_I(inode);
104         struct gfs2_glock *io_gl;
105         int error;
106
107         if (!inode)
108                 return ERR_PTR(-ENOBUFS);
109
110         if (inode->i_state & I_NEW) {
111                 struct gfs2_sbd *sdp = GFS2_SB(inode);
112                 umode_t mode = DT2IF(type);
113                 inode->i_private = ip;
114                 inode->i_mode = mode;
115
116                 if (S_ISREG(mode)) {
117                         inode->i_op = &gfs2_file_iops;
118                         inode->i_fop = &gfs2_file_fops;
119                         inode->i_mapping->a_ops = &gfs2_file_aops;
120                 } else if (S_ISDIR(mode)) {
121                         inode->i_op = &gfs2_dir_iops;
122                         inode->i_fop = &gfs2_dir_fops;
123                 } else if (S_ISLNK(mode)) {
124                         inode->i_op = &gfs2_symlink_iops;
125                 } else {
126                         inode->i_op = &gfs2_dev_iops;
127                 }
128
129                 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
130                 if (unlikely(error))
131                         goto fail;
132                 ip->i_gl->gl_object = ip;
133
134                 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
135                 if (unlikely(error))
136                         goto fail_put;
137
138                 set_bit(GIF_INVALID, &ip->i_flags);
139                 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
140                 if (unlikely(error))
141                         goto fail_iopen;
142
143                 gfs2_glock_put(io_gl);
144                 unlock_new_inode(inode);
145         }
146
147         return inode;
148 fail_iopen:
149         gfs2_glock_put(io_gl);
150 fail_put:
151         ip->i_gl->gl_object = NULL;
152         gfs2_glock_put(ip->i_gl);
153 fail:
154         iput(inode);
155         return ERR_PTR(error);
156 }
157
158 static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
159 {
160         struct gfs2_dinode_host *di = &ip->i_di;
161         const struct gfs2_dinode *str = buf;
162
163         if (ip->i_num.no_addr != be64_to_cpu(str->di_num.no_addr)) {
164                 if (gfs2_consist_inode(ip))
165                         gfs2_dinode_print(ip);
166                 return -EIO;
167         }
168         if (ip->i_num.no_formal_ino != be64_to_cpu(str->di_num.no_formal_ino))
169                 return -ESTALE;
170
171         ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
172         ip->i_inode.i_rdev = 0;
173         switch (ip->i_inode.i_mode & S_IFMT) {
174         case S_IFBLK:
175         case S_IFCHR:
176                 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
177                                            be32_to_cpu(str->di_minor));
178                 break;
179         };
180
181         ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
182         ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
183         /*
184          * We will need to review setting the nlink count here in the
185          * light of the forthcoming ro bind mount work. This is a reminder
186          * to do that.
187          */
188         ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
189         di->di_size = be64_to_cpu(str->di_size);
190         di->di_blocks = be64_to_cpu(str->di_blocks);
191         ip->i_inode.i_atime.tv_sec = be64_to_cpu(str->di_atime);
192         ip->i_inode.i_atime.tv_nsec = 0;
193         ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
194         ip->i_inode.i_mtime.tv_nsec = 0;
195         ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
196         ip->i_inode.i_ctime.tv_nsec = 0;
197
198         di->di_goal_meta = be64_to_cpu(str->di_goal_meta);
199         di->di_goal_data = be64_to_cpu(str->di_goal_data);
200         di->di_generation = be64_to_cpu(str->di_generation);
201
202         di->di_flags = be32_to_cpu(str->di_flags);
203         gfs2_set_inode_flags(&ip->i_inode);
204         di->di_height = be16_to_cpu(str->di_height);
205
206         di->di_depth = be16_to_cpu(str->di_depth);
207         di->di_entries = be32_to_cpu(str->di_entries);
208
209         di->di_eattr = be64_to_cpu(str->di_eattr);
210         return 0;
211 }
212
213 /**
214  * gfs2_inode_refresh - Refresh the incore copy of the dinode
215  * @ip: The GFS2 inode
216  *
217  * Returns: errno
218  */
219
220 int gfs2_inode_refresh(struct gfs2_inode *ip)
221 {
222         struct buffer_head *dibh;
223         int error;
224
225         error = gfs2_meta_inode_buffer(ip, &dibh);
226         if (error)
227                 return error;
228
229         if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
230                 brelse(dibh);
231                 return -EIO;
232         }
233
234         error = gfs2_dinode_in(ip, dibh->b_data);
235         brelse(dibh);
236         clear_bit(GIF_INVALID, &ip->i_flags);
237
238         return error;
239 }
240
241 int gfs2_dinode_dealloc(struct gfs2_inode *ip)
242 {
243         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
244         struct gfs2_alloc *al;
245         struct gfs2_rgrpd *rgd;
246         int error;
247
248         if (ip->i_di.di_blocks != 1) {
249                 if (gfs2_consist_inode(ip))
250                         gfs2_dinode_print(ip);
251                 return -EIO;
252         }
253
254         al = gfs2_alloc_get(ip);
255
256         error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
257         if (error)
258                 goto out;
259
260         error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
261         if (error)
262                 goto out_qs;
263
264         rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
265         if (!rgd) {
266                 gfs2_consist_inode(ip);
267                 error = -EIO;
268                 goto out_rindex_relse;
269         }
270
271         error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
272                                    &al->al_rgd_gh);
273         if (error)
274                 goto out_rindex_relse;
275
276         error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
277         if (error)
278                 goto out_rg_gunlock;
279
280         gfs2_trans_add_gl(ip->i_gl);
281
282         gfs2_free_di(rgd, ip);
283
284         gfs2_trans_end(sdp);
285         clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
286
287 out_rg_gunlock:
288         gfs2_glock_dq_uninit(&al->al_rgd_gh);
289 out_rindex_relse:
290         gfs2_glock_dq_uninit(&al->al_ri_gh);
291 out_qs:
292         gfs2_quota_unhold(ip);
293 out:
294         gfs2_alloc_put(ip);
295         return error;
296 }
297
298 /**
299  * gfs2_change_nlink - Change nlink count on inode
300  * @ip: The GFS2 inode
301  * @diff: The change in the nlink count required
302  *
303  * Returns: errno
304  */
305
306 int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
307 {
308         struct gfs2_sbd *sdp = ip->i_inode.i_sb->s_fs_info;
309         struct buffer_head *dibh;
310         u32 nlink;
311         int error;
312
313         BUG_ON(diff != 1 && diff != -1);
314         nlink = ip->i_inode.i_nlink + diff;
315
316         /* If we are reducing the nlink count, but the new value ends up being
317            bigger than the old one, we must have underflowed. */
318         if (diff < 0 && nlink > ip->i_inode.i_nlink) {
319                 if (gfs2_consist_inode(ip))
320                         gfs2_dinode_print(ip);
321                 return -EIO;
322         }
323
324         error = gfs2_meta_inode_buffer(ip, &dibh);
325         if (error)
326                 return error;
327
328         if (diff > 0)
329                 inc_nlink(&ip->i_inode);
330         else
331                 drop_nlink(&ip->i_inode);
332
333         ip->i_inode.i_ctime.tv_sec = get_seconds();
334
335         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
336         gfs2_dinode_out(ip, dibh->b_data);
337         brelse(dibh);
338         mark_inode_dirty(&ip->i_inode);
339
340         if (ip->i_inode.i_nlink == 0) {
341                 struct gfs2_rgrpd *rgd;
342                 struct gfs2_holder ri_gh, rg_gh;
343
344                 error = gfs2_rindex_hold(sdp, &ri_gh);
345                 if (error)
346                         goto out;
347                 error = -EIO;
348                 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
349                 if (!rgd)
350                         goto out_norgrp;
351                 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
352                 if (error)
353                         goto out_norgrp;
354
355                 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
356                 gfs2_glock_dq_uninit(&rg_gh);
357 out_norgrp:
358                 gfs2_glock_dq_uninit(&ri_gh);
359         }
360 out:
361         return error;
362 }
363
364 struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
365 {
366         struct qstr qstr;
367         gfs2_str2qstr(&qstr, name);
368         return gfs2_lookupi(dip, &qstr, 1, NULL);
369 }
370
371
372 /**
373  * gfs2_lookupi - Look up a filename in a directory and return its inode
374  * @d_gh: An initialized holder for the directory glock
375  * @name: The name of the inode to look for
376  * @is_root: If 1, ignore the caller's permissions
377  * @i_gh: An uninitialized holder for the new inode glock
378  *
379  * There will always be a vnode (Linux VFS inode) for the d_gh inode unless
380  * @is_root is true.
381  *
382  * Returns: errno
383  */
384
385 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
386                            int is_root, struct nameidata *nd)
387 {
388         struct super_block *sb = dir->i_sb;
389         struct gfs2_inode *dip = GFS2_I(dir);
390         struct gfs2_holder d_gh;
391         struct gfs2_inum_host inum;
392         unsigned int type;
393         int error = 0;
394         struct inode *inode = NULL;
395
396         if (!name->len || name->len > GFS2_FNAMESIZE)
397                 return ERR_PTR(-ENAMETOOLONG);
398
399         if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
400             (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
401              dir == sb->s_root->d_inode)) {
402                 igrab(dir);
403                 return dir;
404         }
405
406         error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
407         if (error)
408                 return ERR_PTR(error);
409
410         if (!is_root) {
411                 error = permission(dir, MAY_EXEC, NULL);
412                 if (error)
413                         goto out;
414         }
415
416         error = gfs2_dir_search(dir, name, &inum, &type);
417         if (error)
418                 goto out;
419
420         inode = gfs2_inode_lookup(sb, &inum, type);
421
422 out:
423         gfs2_glock_dq_uninit(&d_gh);
424         if (error == -ENOENT)
425                 return NULL;
426         return inode;
427 }
428
429 static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
430 {
431         struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
432         struct buffer_head *bh;
433         struct gfs2_inum_range_host ir;
434         int error;
435
436         error = gfs2_trans_begin(sdp, RES_DINODE, 0);
437         if (error)
438                 return error;
439         mutex_lock(&sdp->sd_inum_mutex);
440
441         error = gfs2_meta_inode_buffer(ip, &bh);
442         if (error) {
443                 mutex_unlock(&sdp->sd_inum_mutex);
444                 gfs2_trans_end(sdp);
445                 return error;
446         }
447
448         gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
449
450         if (ir.ir_length) {
451                 *formal_ino = ir.ir_start++;
452                 ir.ir_length--;
453                 gfs2_trans_add_bh(ip->i_gl, bh, 1);
454                 gfs2_inum_range_out(&ir,
455                                     bh->b_data + sizeof(struct gfs2_dinode));
456                 brelse(bh);
457                 mutex_unlock(&sdp->sd_inum_mutex);
458                 gfs2_trans_end(sdp);
459                 return 0;
460         }
461
462         brelse(bh);
463
464         mutex_unlock(&sdp->sd_inum_mutex);
465         gfs2_trans_end(sdp);
466
467         return 1;
468 }
469
470 static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
471 {
472         struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
473         struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
474         struct gfs2_holder gh;
475         struct buffer_head *bh;
476         struct gfs2_inum_range_host ir;
477         int error;
478
479         error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
480         if (error)
481                 return error;
482
483         error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
484         if (error)
485                 goto out;
486         mutex_lock(&sdp->sd_inum_mutex);
487
488         error = gfs2_meta_inode_buffer(ip, &bh);
489         if (error)
490                 goto out_end_trans;
491
492         gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
493
494         if (!ir.ir_length) {
495                 struct buffer_head *m_bh;
496                 u64 x, y;
497                 __be64 z;
498
499                 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
500                 if (error)
501                         goto out_brelse;
502
503                 z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
504                 x = y = be64_to_cpu(z);
505                 ir.ir_start = x;
506                 ir.ir_length = GFS2_INUM_QUANTUM;
507                 x += GFS2_INUM_QUANTUM;
508                 if (x < y)
509                         gfs2_consist_inode(m_ip);
510                 z = cpu_to_be64(x);
511                 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
512                 *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
513
514                 brelse(m_bh);
515         }
516
517         *formal_ino = ir.ir_start++;
518         ir.ir_length--;
519
520         gfs2_trans_add_bh(ip->i_gl, bh, 1);
521         gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
522
523 out_brelse:
524         brelse(bh);
525 out_end_trans:
526         mutex_unlock(&sdp->sd_inum_mutex);
527         gfs2_trans_end(sdp);
528 out:
529         gfs2_glock_dq_uninit(&gh);
530         return error;
531 }
532
533 static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
534 {
535         int error;
536
537         error = pick_formal_ino_1(sdp, inum);
538         if (error <= 0)
539                 return error;
540
541         error = pick_formal_ino_2(sdp, inum);
542
543         return error;
544 }
545
546 /**
547  * create_ok - OK to create a new on-disk inode here?
548  * @dip:  Directory in which dinode is to be created
549  * @name:  Name of new dinode
550  * @mode:
551  *
552  * Returns: errno
553  */
554
555 static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
556                      unsigned int mode)
557 {
558         int error;
559
560         error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
561         if (error)
562                 return error;
563
564         /*  Don't create entries in an unlinked directory  */
565         if (!dip->i_inode.i_nlink)
566                 return -EPERM;
567
568         error = gfs2_dir_search(&dip->i_inode, name, NULL, NULL);
569         switch (error) {
570         case -ENOENT:
571                 error = 0;
572                 break;
573         case 0:
574                 return -EEXIST;
575         default:
576                 return error;
577         }
578
579         if (dip->i_di.di_entries == (u32)-1)
580                 return -EFBIG;
581         if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
582                 return -EMLINK;
583
584         return 0;
585 }
586
587 static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
588                                unsigned int *uid, unsigned int *gid)
589 {
590         if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
591             (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
592                 if (S_ISDIR(*mode))
593                         *mode |= S_ISUID;
594                 else if (dip->i_inode.i_uid != current->fsuid)
595                         *mode &= ~07111;
596                 *uid = dip->i_inode.i_uid;
597         } else
598                 *uid = current->fsuid;
599
600         if (dip->i_inode.i_mode & S_ISGID) {
601                 if (S_ISDIR(*mode))
602                         *mode |= S_ISGID;
603                 *gid = dip->i_inode.i_gid;
604         } else
605                 *gid = current->fsgid;
606 }
607
608 static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_inum_host *inum,
609                         u64 *generation)
610 {
611         struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
612         int error;
613
614         gfs2_alloc_get(dip);
615
616         dip->i_alloc.al_requested = RES_DINODE;
617         error = gfs2_inplace_reserve(dip);
618         if (error)
619                 goto out;
620
621         error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
622         if (error)
623                 goto out_ipreserv;
624
625         inum->no_addr = gfs2_alloc_di(dip, generation);
626
627         gfs2_trans_end(sdp);
628
629 out_ipreserv:
630         gfs2_inplace_release(dip);
631 out:
632         gfs2_alloc_put(dip);
633         return error;
634 }
635
636 /**
637  * init_dinode - Fill in a new dinode structure
638  * @dip: the directory this inode is being created in
639  * @gl: The glock covering the new inode
640  * @inum: the inode number
641  * @mode: the file permissions
642  * @uid:
643  * @gid:
644  *
645  */
646
647 static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
648                         const struct gfs2_inum_host *inum, unsigned int mode,
649                         unsigned int uid, unsigned int gid,
650                         const u64 *generation, dev_t dev)
651 {
652         struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
653         struct gfs2_dinode *di;
654         struct buffer_head *dibh;
655
656         dibh = gfs2_meta_new(gl, inum->no_addr);
657         gfs2_trans_add_bh(gl, dibh, 1);
658         gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
659         gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
660         di = (struct gfs2_dinode *)dibh->b_data;
661
662         di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
663         di->di_num.no_addr = cpu_to_be64(inum->no_addr);
664         di->di_mode = cpu_to_be32(mode);
665         di->di_uid = cpu_to_be32(uid);
666         di->di_gid = cpu_to_be32(gid);
667         di->di_nlink = 0;
668         di->di_size = 0;
669         di->di_blocks = cpu_to_be64(1);
670         di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
671         di->di_major = cpu_to_be32(MAJOR(dev));
672         di->di_minor = cpu_to_be32(MINOR(dev));
673         di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
674         di->di_generation = cpu_to_be64(*generation);
675         di->di_flags = 0;
676
677         if (S_ISREG(mode)) {
678                 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
679                     gfs2_tune_get(sdp, gt_new_files_jdata))
680                         di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
681                 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
682                     gfs2_tune_get(sdp, gt_new_files_directio))
683                         di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
684         } else if (S_ISDIR(mode)) {
685                 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
686                                             GFS2_DIF_INHERIT_DIRECTIO);
687                 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
688                                             GFS2_DIF_INHERIT_JDATA);
689         }
690
691         di->__pad1 = 0;
692         di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
693         di->di_height = 0;
694         di->__pad2 = 0;
695         di->__pad3 = 0;
696         di->di_depth = 0;
697         di->di_entries = 0;
698         memset(&di->__pad4, 0, sizeof(di->__pad4));
699         di->di_eattr = 0;
700         memset(&di->di_reserved, 0, sizeof(di->di_reserved));
701
702         brelse(dibh);
703 }
704
705 static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
706                        unsigned int mode, const struct gfs2_inum_host *inum,
707                        const u64 *generation, dev_t dev)
708 {
709         struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
710         unsigned int uid, gid;
711         int error;
712
713         munge_mode_uid_gid(dip, &mode, &uid, &gid);
714         gfs2_alloc_get(dip);
715
716         error = gfs2_quota_lock(dip, uid, gid);
717         if (error)
718                 goto out;
719
720         error = gfs2_quota_check(dip, uid, gid);
721         if (error)
722                 goto out_quota;
723
724         error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
725         if (error)
726                 goto out_quota;
727
728         init_dinode(dip, gl, inum, mode, uid, gid, generation, dev);
729         gfs2_quota_change(dip, +1, uid, gid);
730         gfs2_trans_end(sdp);
731
732 out_quota:
733         gfs2_quota_unlock(dip);
734 out:
735         gfs2_alloc_put(dip);
736         return error;
737 }
738
739 static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
740                        struct gfs2_inode *ip)
741 {
742         struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
743         struct gfs2_alloc *al;
744         int alloc_required;
745         struct buffer_head *dibh;
746         int error;
747
748         al = gfs2_alloc_get(dip);
749
750         error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
751         if (error)
752                 goto fail;
753
754         error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
755         if (alloc_required < 0)
756                 goto fail;
757         if (alloc_required) {
758                 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
759                 if (error)
760                         goto fail_quota_locks;
761
762                 al->al_requested = sdp->sd_max_dirres;
763
764                 error = gfs2_inplace_reserve(dip);
765                 if (error)
766                         goto fail_quota_locks;
767
768                 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
769                                          al->al_rgd->rd_ri.ri_length +
770                                          2 * RES_DINODE +
771                                          RES_STATFS + RES_QUOTA, 0);
772                 if (error)
773                         goto fail_ipreserv;
774         } else {
775                 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
776                 if (error)
777                         goto fail_quota_locks;
778         }
779
780         error = gfs2_dir_add(&dip->i_inode, name, &ip->i_num, IF2DT(ip->i_inode.i_mode));
781         if (error)
782                 goto fail_end_trans;
783
784         error = gfs2_meta_inode_buffer(ip, &dibh);
785         if (error)
786                 goto fail_end_trans;
787         ip->i_inode.i_nlink = 1;
788         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
789         gfs2_dinode_out(ip, dibh->b_data);
790         brelse(dibh);
791         return 0;
792
793 fail_end_trans:
794         gfs2_trans_end(sdp);
795
796 fail_ipreserv:
797         if (dip->i_alloc.al_rgd)
798                 gfs2_inplace_release(dip);
799
800 fail_quota_locks:
801         gfs2_quota_unlock(dip);
802
803 fail:
804         gfs2_alloc_put(dip);
805         return error;
806 }
807
808 static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
809 {
810         int err;
811         size_t len;
812         void *value;
813         char *name;
814         struct gfs2_ea_request er;
815
816         err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
817                                            &name, &value, &len);
818
819         if (err) {
820                 if (err == -EOPNOTSUPP)
821                         return 0;
822                 return err;
823         }
824
825         memset(&er, 0, sizeof(struct gfs2_ea_request));
826
827         er.er_type = GFS2_EATYPE_SECURITY;
828         er.er_name = name;
829         er.er_data = value;
830         er.er_name_len = strlen(name);
831         er.er_data_len = len;
832
833         err = gfs2_ea_set_i(ip, &er);
834
835         kfree(value);
836         kfree(name);
837
838         return err;
839 }
840
841 /**
842  * gfs2_createi - Create a new inode
843  * @ghs: An array of two holders
844  * @name: The name of the new file
845  * @mode: the permissions on the new inode
846  *
847  * @ghs[0] is an initialized holder for the directory
848  * @ghs[1] is the holder for the inode lock
849  *
850  * If the return value is not NULL, the glocks on both the directory and the new
851  * file are held.  A transaction has been started and an inplace reservation
852  * is held, as well.
853  *
854  * Returns: An inode
855  */
856
857 struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
858                            unsigned int mode, dev_t dev)
859 {
860         struct inode *inode;
861         struct gfs2_inode *dip = ghs->gh_gl->gl_object;
862         struct inode *dir = &dip->i_inode;
863         struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
864         struct gfs2_inum_host inum;
865         int error;
866         u64 generation;
867
868         if (!name->len || name->len > GFS2_FNAMESIZE)
869                 return ERR_PTR(-ENAMETOOLONG);
870
871         gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
872         error = gfs2_glock_nq(ghs);
873         if (error)
874                 goto fail;
875
876         error = create_ok(dip, name, mode);
877         if (error)
878                 goto fail_gunlock;
879
880         error = pick_formal_ino(sdp, &inum.no_formal_ino);
881         if (error)
882                 goto fail_gunlock;
883
884         error = alloc_dinode(dip, &inum, &generation);
885         if (error)
886                 goto fail_gunlock;
887
888         if (inum.no_addr < dip->i_num.no_addr) {
889                 gfs2_glock_dq(ghs);
890
891                 error = gfs2_glock_nq_num(sdp, inum.no_addr,
892                                           &gfs2_inode_glops, LM_ST_EXCLUSIVE,
893                                           GL_SKIP, ghs + 1);
894                 if (error) {
895                         return ERR_PTR(error);
896                 }
897
898                 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
899                 error = gfs2_glock_nq(ghs);
900                 if (error) {
901                         gfs2_glock_dq_uninit(ghs + 1);
902                         return ERR_PTR(error);
903                 }
904
905                 error = create_ok(dip, name, mode);
906                 if (error)
907                         goto fail_gunlock2;
908         } else {
909                 error = gfs2_glock_nq_num(sdp, inum.no_addr,
910                                           &gfs2_inode_glops, LM_ST_EXCLUSIVE,
911                                           GL_SKIP, ghs + 1);
912                 if (error)
913                         goto fail_gunlock;
914         }
915
916         error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev);
917         if (error)
918                 goto fail_gunlock2;
919
920         inode = gfs2_inode_lookup(dir->i_sb, &inum, IF2DT(mode));
921         if (IS_ERR(inode))
922                 goto fail_gunlock2;
923
924         error = gfs2_inode_refresh(GFS2_I(inode));
925         if (error)
926                 goto fail_iput;
927
928         error = gfs2_acl_create(dip, GFS2_I(inode));
929         if (error)
930                 goto fail_iput;
931
932         error = gfs2_security_init(dip, GFS2_I(inode));
933         if (error)
934                 goto fail_iput;
935
936         error = link_dinode(dip, name, GFS2_I(inode));
937         if (error)
938                 goto fail_iput;
939
940         if (!inode)
941                 return ERR_PTR(-ENOMEM);
942         return inode;
943
944 fail_iput:
945         iput(inode);
946 fail_gunlock2:
947         gfs2_glock_dq_uninit(ghs + 1);
948 fail_gunlock:
949         gfs2_glock_dq(ghs);
950 fail:
951         return ERR_PTR(error);
952 }
953
954 /**
955  * gfs2_rmdiri - Remove a directory
956  * @dip: The parent directory of the directory to be removed
957  * @name: The name of the directory to be removed
958  * @ip: The GFS2 inode of the directory to be removed
959  *
960  * Assumes Glocks on dip and ip are held
961  *
962  * Returns: errno
963  */
964
965 int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
966                 struct gfs2_inode *ip)
967 {
968         struct qstr dotname;
969         int error;
970
971         if (ip->i_di.di_entries != 2) {
972                 if (gfs2_consist_inode(ip))
973                         gfs2_dinode_print(ip);
974                 return -EIO;
975         }
976
977         error = gfs2_dir_del(dip, name);
978         if (error)
979                 return error;
980
981         error = gfs2_change_nlink(dip, -1);
982         if (error)
983                 return error;
984
985         gfs2_str2qstr(&dotname, ".");
986         error = gfs2_dir_del(ip, &dotname);
987         if (error)
988                 return error;
989
990         gfs2_str2qstr(&dotname, "..");
991         error = gfs2_dir_del(ip, &dotname);
992         if (error)
993                 return error;
994
995         /* It looks odd, but it really should be done twice */
996         error = gfs2_change_nlink(ip, -1);
997         if (error)
998                 return error;
999
1000         error = gfs2_change_nlink(ip, -1);
1001         if (error)
1002                 return error;
1003
1004         return error;
1005 }
1006
1007 /*
1008  * gfs2_unlink_ok - check to see that a inode is still in a directory
1009  * @dip: the directory
1010  * @name: the name of the file
1011  * @ip: the inode
1012  *
1013  * Assumes that the lock on (at least) @dip is held.
1014  *
1015  * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1016  */
1017
1018 int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
1019                    struct gfs2_inode *ip)
1020 {
1021         struct gfs2_inum_host inum;
1022         unsigned int type;
1023         int error;
1024
1025         if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1026                 return -EPERM;
1027
1028         if ((dip->i_inode.i_mode & S_ISVTX) &&
1029             dip->i_inode.i_uid != current->fsuid &&
1030             ip->i_inode.i_uid != current->fsuid && !capable(CAP_FOWNER))
1031                 return -EPERM;
1032
1033         if (IS_APPEND(&dip->i_inode))
1034                 return -EPERM;
1035
1036         error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
1037         if (error)
1038                 return error;
1039
1040         error = gfs2_dir_search(&dip->i_inode, name, &inum, &type);
1041         if (error)
1042                 return error;
1043
1044         if (!gfs2_inum_equal(&inum, &ip->i_num))
1045                 return -ENOENT;
1046
1047         if (IF2DT(ip->i_inode.i_mode) != type) {
1048                 gfs2_consist_inode(dip);
1049                 return -EIO;
1050         }
1051
1052         return 0;
1053 }
1054
1055 /*
1056  * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1057  * @this: move this
1058  * @to: to here
1059  *
1060  * Follow @to back to the root and make sure we don't encounter @this
1061  * Assumes we already hold the rename lock.
1062  *
1063  * Returns: errno
1064  */
1065
1066 int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1067 {
1068         struct inode *dir = &to->i_inode;
1069         struct super_block *sb = dir->i_sb;
1070         struct inode *tmp;
1071         struct qstr dotdot;
1072         int error = 0;
1073
1074         gfs2_str2qstr(&dotdot, "..");
1075
1076         igrab(dir);
1077
1078         for (;;) {
1079                 if (dir == &this->i_inode) {
1080                         error = -EINVAL;
1081                         break;
1082                 }
1083                 if (dir == sb->s_root->d_inode) {
1084                         error = 0;
1085                         break;
1086                 }
1087
1088                 tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
1089                 if (IS_ERR(tmp)) {
1090                         error = PTR_ERR(tmp);
1091                         break;
1092                 }
1093
1094                 iput(dir);
1095                 dir = tmp;
1096         }
1097
1098         iput(dir);
1099
1100         return error;
1101 }
1102
1103 /**
1104  * gfs2_readlinki - return the contents of a symlink
1105  * @ip: the symlink's inode
1106  * @buf: a pointer to the buffer to be filled
1107  * @len: a pointer to the length of @buf
1108  *
1109  * If @buf is too small, a piece of memory is kmalloc()ed and needs
1110  * to be freed by the caller.
1111  *
1112  * Returns: errno
1113  */
1114
1115 int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1116 {
1117         struct gfs2_holder i_gh;
1118         struct buffer_head *dibh;
1119         unsigned int x;
1120         int error;
1121
1122         gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1123         error = gfs2_glock_nq_atime(&i_gh);
1124         if (error) {
1125                 gfs2_holder_uninit(&i_gh);
1126                 return error;
1127         }
1128
1129         if (!ip->i_di.di_size) {
1130                 gfs2_consist_inode(ip);
1131                 error = -EIO;
1132                 goto out;
1133         }
1134
1135         error = gfs2_meta_inode_buffer(ip, &dibh);
1136         if (error)
1137                 goto out;
1138
1139         x = ip->i_di.di_size + 1;
1140         if (x > *len) {
1141                 *buf = kmalloc(x, GFP_KERNEL);
1142                 if (!*buf) {
1143                         error = -ENOMEM;
1144                         goto out_brelse;
1145                 }
1146         }
1147
1148         memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1149         *len = x;
1150
1151 out_brelse:
1152         brelse(dibh);
1153 out:
1154         gfs2_glock_dq_uninit(&i_gh);
1155         return error;
1156 }
1157
1158 /**
1159  * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
1160  *       conditionally update the inode's atime
1161  * @gh: the holder to acquire
1162  *
1163  * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
1164  * Update if the difference between the current time and the inode's current
1165  * atime is greater than an interval specified at mount.
1166  *
1167  * Returns: errno
1168  */
1169
1170 int gfs2_glock_nq_atime(struct gfs2_holder *gh)
1171 {
1172         struct gfs2_glock *gl = gh->gh_gl;
1173         struct gfs2_sbd *sdp = gl->gl_sbd;
1174         struct gfs2_inode *ip = gl->gl_object;
1175         s64 curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
1176         unsigned int state;
1177         int flags;
1178         int error;
1179
1180         if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1181             gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1182             gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1183                 return -EINVAL;
1184
1185         state = gh->gh_state;
1186         flags = gh->gh_flags;
1187
1188         error = gfs2_glock_nq(gh);
1189         if (error)
1190                 return error;
1191
1192         if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1193             (sdp->sd_vfs->s_flags & MS_RDONLY))
1194                 return 0;
1195
1196         curtime = get_seconds();
1197         if (curtime - ip->i_inode.i_atime.tv_sec >= quantum) {
1198                 gfs2_glock_dq(gh);
1199                 gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
1200                                    gh);
1201                 error = gfs2_glock_nq(gh);
1202                 if (error)
1203                         return error;
1204
1205                 /* Verify that atime hasn't been updated while we were
1206                    trying to get exclusive lock. */
1207
1208                 curtime = get_seconds();
1209                 if (curtime - ip->i_inode.i_atime.tv_sec >= quantum) {
1210                         struct buffer_head *dibh;
1211                         struct gfs2_dinode *di;
1212
1213                         error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1214                         if (error == -EROFS)
1215                                 return 0;
1216                         if (error)
1217                                 goto fail;
1218
1219                         error = gfs2_meta_inode_buffer(ip, &dibh);
1220                         if (error)
1221                                 goto fail_end_trans;
1222
1223                         ip->i_inode.i_atime.tv_sec = curtime;
1224
1225                         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1226                         di = (struct gfs2_dinode *)dibh->b_data;
1227                         di->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
1228                         brelse(dibh);
1229
1230                         gfs2_trans_end(sdp);
1231                 }
1232
1233                 /* If someone else has asked for the glock,
1234                    unlock and let them have it. Then reacquire
1235                    in the original state. */
1236                 if (gfs2_glock_is_blocking(gl)) {
1237                         gfs2_glock_dq(gh);
1238                         gfs2_holder_reinit(state, flags, gh);
1239                         return gfs2_glock_nq(gh);
1240                 }
1241         }
1242
1243         return 0;
1244
1245 fail_end_trans:
1246         gfs2_trans_end(sdp);
1247 fail:
1248         gfs2_glock_dq(gh);
1249         return error;
1250 }
1251
1252 /**
1253  * glock_compare_atime - Compare two struct gfs2_glock structures for sort
1254  * @arg_a: the first structure
1255  * @arg_b: the second structure
1256  *
1257  * Returns: 1 if A > B
1258  *         -1 if A < B
1259  *          0 if A == B
1260  */
1261
1262 static int glock_compare_atime(const void *arg_a, const void *arg_b)
1263 {
1264         const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1265         const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1266         const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1267         const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1268
1269         if (a->ln_number > b->ln_number)
1270                 return 1;
1271         if (a->ln_number < b->ln_number)
1272                 return -1;
1273         if (gh_a->gh_state == LM_ST_SHARED && gh_b->gh_state == LM_ST_EXCLUSIVE)
1274                 return 1;
1275         if (gh_a->gh_state == LM_ST_SHARED && (gh_b->gh_flags & GL_ATIME))
1276                 return 1;
1277
1278         return 0;
1279 }
1280
1281 /**
1282  * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an
1283  *      atime update
1284  * @num_gh: the number of structures
1285  * @ghs: an array of struct gfs2_holder structures
1286  *
1287  * Returns: 0 on success (all glocks acquired),
1288  *          errno on failure (no glocks acquired)
1289  */
1290
1291 int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs)
1292 {
1293         struct gfs2_holder **p;
1294         unsigned int x;
1295         int error = 0;
1296
1297         if (!num_gh)
1298                 return 0;
1299
1300         if (num_gh == 1) {
1301                 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1302                 if (ghs->gh_flags & GL_ATIME)
1303                         error = gfs2_glock_nq_atime(ghs);
1304                 else
1305                         error = gfs2_glock_nq(ghs);
1306                 return error;
1307         }
1308
1309         p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1310         if (!p)
1311                 return -ENOMEM;
1312
1313         for (x = 0; x < num_gh; x++)
1314                 p[x] = &ghs[x];
1315
1316         sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL);
1317
1318         for (x = 0; x < num_gh; x++) {
1319                 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1320
1321                 if (p[x]->gh_flags & GL_ATIME)
1322                         error = gfs2_glock_nq_atime(p[x]);
1323                 else
1324                         error = gfs2_glock_nq(p[x]);
1325
1326                 if (error) {
1327                         while (x--)
1328                                 gfs2_glock_dq(p[x]);
1329                         break;
1330                 }
1331         }
1332
1333         kfree(p);
1334         return error;
1335 }
1336
1337
1338 static int
1339 __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1340 {
1341         struct buffer_head *dibh;
1342         int error;
1343
1344         error = gfs2_meta_inode_buffer(ip, &dibh);
1345         if (!error) {
1346                 error = inode_setattr(&ip->i_inode, attr);
1347                 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
1348                 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1349                 gfs2_dinode_out(ip, dibh->b_data);
1350                 brelse(dibh);
1351         }
1352         return error;
1353 }
1354
1355 /**
1356  * gfs2_setattr_simple -
1357  * @ip:
1358  * @attr:
1359  *
1360  * Called with a reference on the vnode.
1361  *
1362  * Returns: errno
1363  */
1364
1365 int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1366 {
1367         int error;
1368
1369         if (current->journal_info)
1370                 return __gfs2_setattr_simple(ip, attr);
1371
1372         error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
1373         if (error)
1374                 return error;
1375
1376         error = __gfs2_setattr_simple(ip, attr);
1377         gfs2_trans_end(GFS2_SB(&ip->i_inode));
1378         return error;
1379 }
1380