e1213f7f92179aa2472304ff0db4294be66040d8
[pandora-kernel.git] / fs / gfs2 / inode.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2008 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/security.h>
20 #include <linux/time.h>
21
22 #include "gfs2.h"
23 #include "incore.h"
24 #include "acl.h"
25 #include "bmap.h"
26 #include "dir.h"
27 #include "xattr.h"
28 #include "glock.h"
29 #include "glops.h"
30 #include "inode.h"
31 #include "log.h"
32 #include "meta_io.h"
33 #include "quota.h"
34 #include "rgrp.h"
35 #include "trans.h"
36 #include "util.h"
37
38 struct gfs2_inum_range_host {
39         u64 ir_start;
40         u64 ir_length;
41 };
42
43 static int iget_test(struct inode *inode, void *opaque)
44 {
45         struct gfs2_inode *ip = GFS2_I(inode);
46         u64 *no_addr = opaque;
47
48         if (ip->i_no_addr == *no_addr)
49                 return 1;
50
51         return 0;
52 }
53
54 static int iget_set(struct inode *inode, void *opaque)
55 {
56         struct gfs2_inode *ip = GFS2_I(inode);
57         u64 *no_addr = opaque;
58
59         inode->i_ino = (unsigned long)*no_addr;
60         ip->i_no_addr = *no_addr;
61         return 0;
62 }
63
64 struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
65 {
66         unsigned long hash = (unsigned long)no_addr;
67         return ilookup5(sb, hash, iget_test, &no_addr);
68 }
69
70 static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
71 {
72         unsigned long hash = (unsigned long)no_addr;
73         return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
74 }
75
76 /**
77  * GFS2 lookup code fills in vfs inode contents based on info obtained
78  * from directory entry inside gfs2_inode_lookup(). This has caused issues
79  * with NFS code path since its get_dentry routine doesn't have the relevant
80  * directory entry when gfs2_inode_lookup() is invoked. Part of the code
81  * segment inside gfs2_inode_lookup code needs to get moved around.
82  *
83  * Clears I_NEW as well.
84  **/
85
86 void gfs2_set_iop(struct inode *inode)
87 {
88         struct gfs2_sbd *sdp = GFS2_SB(inode);
89         umode_t mode = inode->i_mode;
90
91         if (S_ISREG(mode)) {
92                 inode->i_op = &gfs2_file_iops;
93                 if (gfs2_localflocks(sdp))
94                         inode->i_fop = &gfs2_file_fops_nolock;
95                 else
96                         inode->i_fop = &gfs2_file_fops;
97         } else if (S_ISDIR(mode)) {
98                 inode->i_op = &gfs2_dir_iops;
99                 if (gfs2_localflocks(sdp))
100                         inode->i_fop = &gfs2_dir_fops_nolock;
101                 else
102                         inode->i_fop = &gfs2_dir_fops;
103         } else if (S_ISLNK(mode)) {
104                 inode->i_op = &gfs2_symlink_iops;
105         } else {
106                 inode->i_op = &gfs2_file_iops;
107                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
108         }
109
110         unlock_new_inode(inode);
111 }
112
113 /**
114  * gfs2_inode_lookup - Lookup an inode
115  * @sb: The super block
116  * @no_addr: The inode number
117  * @type: The type of the inode
118  *
119  * Returns: A VFS inode, or an error
120  */
121
122 struct inode *gfs2_inode_lookup(struct super_block *sb,
123                                 unsigned int type,
124                                 u64 no_addr,
125                                 u64 no_formal_ino)
126 {
127         struct inode *inode;
128         struct gfs2_inode *ip;
129         struct gfs2_glock *io_gl = NULL;
130         int error;
131
132         inode = gfs2_iget(sb, no_addr);
133         ip = GFS2_I(inode);
134
135         if (!inode)
136                 return ERR_PTR(-ENOBUFS);
137
138         if (inode->i_state & I_NEW) {
139                 struct gfs2_sbd *sdp = GFS2_SB(inode);
140                 ip->i_no_formal_ino = no_formal_ino;
141
142                 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
143                 if (unlikely(error))
144                         goto fail;
145                 ip->i_gl->gl_object = ip;
146
147                 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
148                 if (unlikely(error))
149                         goto fail_put;
150
151                 set_bit(GIF_INVALID, &ip->i_flags);
152                 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
153                 if (unlikely(error))
154                         goto fail_iopen;
155                 ip->i_iopen_gh.gh_gl->gl_object = ip;
156
157                 gfs2_glock_put(io_gl);
158                 io_gl = NULL;
159
160                 if ((type == DT_UNKNOWN) && (no_formal_ino == 0))
161                         goto gfs2_nfsbypass;
162
163                 inode->i_mode = DT2IF(type);
164
165                 /*
166                  * We must read the inode in order to work out its type in
167                  * this case. Note that this doesn't happen often as we normally
168                  * know the type beforehand. This code path only occurs during
169                  * unlinked inode recovery (where it is safe to do this glock,
170                  * which is not true in the general case).
171                  */
172                 if (type == DT_UNKNOWN) {
173                         struct gfs2_holder gh;
174                         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
175                         if (unlikely(error))
176                                 goto fail_glock;
177                         /* Inode is now uptodate */
178                         gfs2_glock_dq_uninit(&gh);
179                 }
180
181                 gfs2_set_iop(inode);
182         }
183
184 gfs2_nfsbypass:
185         return inode;
186 fail_glock:
187         gfs2_glock_dq(&ip->i_iopen_gh);
188 fail_iopen:
189         if (io_gl)
190                 gfs2_glock_put(io_gl);
191 fail_put:
192         if (inode->i_state & I_NEW)
193                 ip->i_gl->gl_object = NULL;
194         gfs2_glock_put(ip->i_gl);
195 fail:
196         if (inode->i_state & I_NEW)
197                 iget_failed(inode);
198         else
199                 iput(inode);
200         return ERR_PTR(error);
201 }
202
203 struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
204                                   u64 *no_formal_ino, unsigned int blktype)
205 {
206         struct super_block *sb = sdp->sd_vfs;
207         struct gfs2_holder i_gh;
208         struct inode *inode;
209         int error;
210
211         error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
212                                   LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
213         if (error)
214                 return ERR_PTR(error);
215
216         error = gfs2_check_blk_type(sdp, no_addr, blktype);
217         if (error)
218                 goto fail;
219
220         inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0);
221         if (IS_ERR(inode))
222                 goto fail;
223
224         error = gfs2_inode_refresh(GFS2_I(inode));
225         if (error)
226                 goto fail_iput;
227
228         /* Pick up the works we bypass in gfs2_inode_lookup */
229         if (inode->i_state & I_NEW) 
230                 gfs2_set_iop(inode);
231
232         /* Two extra checks for NFS only */
233         if (no_formal_ino) {
234                 error = -ESTALE;
235                 if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
236                         goto fail_iput;
237
238                 error = -EIO;
239                 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
240                         goto fail_iput;
241
242                 error = 0;
243         }
244
245 fail:
246         gfs2_glock_dq_uninit(&i_gh);
247         return error ? ERR_PTR(error) : inode;
248 fail_iput:
249         iput(inode);
250         goto fail;
251 }
252
253 static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
254 {
255         const struct gfs2_dinode *str = buf;
256         struct timespec atime;
257         u16 height, depth;
258
259         if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
260                 goto corrupt;
261         ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
262         ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
263         ip->i_inode.i_rdev = 0;
264         switch (ip->i_inode.i_mode & S_IFMT) {
265         case S_IFBLK:
266         case S_IFCHR:
267                 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
268                                            be32_to_cpu(str->di_minor));
269                 break;
270         };
271
272         ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
273         ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
274         /*
275          * We will need to review setting the nlink count here in the
276          * light of the forthcoming ro bind mount work. This is a reminder
277          * to do that.
278          */
279         ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
280         i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
281         gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
282         atime.tv_sec = be64_to_cpu(str->di_atime);
283         atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
284         if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
285                 ip->i_inode.i_atime = atime;
286         ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
287         ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
288         ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
289         ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
290
291         ip->i_goal = be64_to_cpu(str->di_goal_meta);
292         ip->i_generation = be64_to_cpu(str->di_generation);
293
294         ip->i_diskflags = be32_to_cpu(str->di_flags);
295         gfs2_set_inode_flags(&ip->i_inode);
296         height = be16_to_cpu(str->di_height);
297         if (unlikely(height > GFS2_MAX_META_HEIGHT))
298                 goto corrupt;
299         ip->i_height = (u8)height;
300
301         depth = be16_to_cpu(str->di_depth);
302         if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
303                 goto corrupt;
304         ip->i_depth = (u8)depth;
305         ip->i_entries = be32_to_cpu(str->di_entries);
306
307         ip->i_eattr = be64_to_cpu(str->di_eattr);
308         if (S_ISREG(ip->i_inode.i_mode))
309                 gfs2_set_aops(&ip->i_inode);
310
311         return 0;
312 corrupt:
313         if (gfs2_consist_inode(ip))
314                 gfs2_dinode_print(ip);
315         return -EIO;
316 }
317
318 /**
319  * gfs2_inode_refresh - Refresh the incore copy of the dinode
320  * @ip: The GFS2 inode
321  *
322  * Returns: errno
323  */
324
325 int gfs2_inode_refresh(struct gfs2_inode *ip)
326 {
327         struct buffer_head *dibh;
328         int error;
329
330         error = gfs2_meta_inode_buffer(ip, &dibh);
331         if (error)
332                 return error;
333
334         if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
335                 brelse(dibh);
336                 return -EIO;
337         }
338
339         error = gfs2_dinode_in(ip, dibh->b_data);
340         brelse(dibh);
341         clear_bit(GIF_INVALID, &ip->i_flags);
342
343         return error;
344 }
345
346 int gfs2_dinode_dealloc(struct gfs2_inode *ip)
347 {
348         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
349         struct gfs2_alloc *al;
350         struct gfs2_rgrpd *rgd;
351         int error;
352
353         if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
354                 if (gfs2_consist_inode(ip))
355                         gfs2_dinode_print(ip);
356                 return -EIO;
357         }
358
359         al = gfs2_alloc_get(ip);
360         if (!al)
361                 return -ENOMEM;
362
363         error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
364         if (error)
365                 goto out;
366
367         error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
368         if (error)
369                 goto out_qs;
370
371         rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
372         if (!rgd) {
373                 gfs2_consist_inode(ip);
374                 error = -EIO;
375                 goto out_rindex_relse;
376         }
377
378         error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
379                                    &al->al_rgd_gh);
380         if (error)
381                 goto out_rindex_relse;
382
383         error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
384         if (error)
385                 goto out_rg_gunlock;
386
387         set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
388         set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
389
390         gfs2_free_di(rgd, ip);
391
392         gfs2_trans_end(sdp);
393
394 out_rg_gunlock:
395         gfs2_glock_dq_uninit(&al->al_rgd_gh);
396 out_rindex_relse:
397         gfs2_glock_dq_uninit(&al->al_ri_gh);
398 out_qs:
399         gfs2_quota_unhold(ip);
400 out:
401         gfs2_alloc_put(ip);
402         return error;
403 }
404
405 /**
406  * gfs2_change_nlink - Change nlink count on inode
407  * @ip: The GFS2 inode
408  * @diff: The change in the nlink count required
409  *
410  * Returns: errno
411  */
412 int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
413 {
414         struct buffer_head *dibh;
415         u32 nlink;
416         int error;
417
418         BUG_ON(diff != 1 && diff != -1);
419         nlink = ip->i_inode.i_nlink + diff;
420
421         /* If we are reducing the nlink count, but the new value ends up being
422            bigger than the old one, we must have underflowed. */
423         if (diff < 0 && nlink > ip->i_inode.i_nlink) {
424                 if (gfs2_consist_inode(ip))
425                         gfs2_dinode_print(ip);
426                 return -EIO;
427         }
428
429         error = gfs2_meta_inode_buffer(ip, &dibh);
430         if (error)
431                 return error;
432
433         if (diff > 0)
434                 inc_nlink(&ip->i_inode);
435         else
436                 drop_nlink(&ip->i_inode);
437
438         ip->i_inode.i_ctime = CURRENT_TIME;
439
440         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
441         gfs2_dinode_out(ip, dibh->b_data);
442         brelse(dibh);
443         mark_inode_dirty(&ip->i_inode);
444
445         if (ip->i_inode.i_nlink == 0)
446                 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
447
448         return error;
449 }
450
451 struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
452 {
453         struct qstr qstr;
454         struct inode *inode;
455         gfs2_str2qstr(&qstr, name);
456         inode = gfs2_lookupi(dip, &qstr, 1);
457         /* gfs2_lookupi has inconsistent callers: vfs
458          * related routines expect NULL for no entry found,
459          * gfs2_lookup_simple callers expect ENOENT
460          * and do not check for NULL.
461          */
462         if (inode == NULL)
463                 return ERR_PTR(-ENOENT);
464         else
465                 return inode;
466 }
467
468
469 /**
470  * gfs2_lookupi - Look up a filename in a directory and return its inode
471  * @d_gh: An initialized holder for the directory glock
472  * @name: The name of the inode to look for
473  * @is_root: If 1, ignore the caller's permissions
474  * @i_gh: An uninitialized holder for the new inode glock
475  *
476  * This can be called via the VFS filldir function when NFS is doing
477  * a readdirplus and the inode which its intending to stat isn't
478  * already in cache. In this case we must not take the directory glock
479  * again, since the readdir call will have already taken that lock.
480  *
481  * Returns: errno
482  */
483
484 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
485                            int is_root)
486 {
487         struct super_block *sb = dir->i_sb;
488         struct gfs2_inode *dip = GFS2_I(dir);
489         struct gfs2_holder d_gh;
490         int error = 0;
491         struct inode *inode = NULL;
492         int unlock = 0;
493
494         if (!name->len || name->len > GFS2_FNAMESIZE)
495                 return ERR_PTR(-ENAMETOOLONG);
496
497         if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
498             (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
499              dir == sb->s_root->d_inode)) {
500                 igrab(dir);
501                 return dir;
502         }
503
504         if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
505                 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
506                 if (error)
507                         return ERR_PTR(error);
508                 unlock = 1;
509         }
510
511         if (!is_root) {
512                 error = gfs2_permission(dir, MAY_EXEC);
513                 if (error)
514                         goto out;
515         }
516
517         inode = gfs2_dir_search(dir, name);
518         if (IS_ERR(inode))
519                 error = PTR_ERR(inode);
520 out:
521         if (unlock)
522                 gfs2_glock_dq_uninit(&d_gh);
523         if (error == -ENOENT)
524                 return NULL;
525         return inode ? inode : ERR_PTR(error);
526 }
527
528 /**
529  * create_ok - OK to create a new on-disk inode here?
530  * @dip:  Directory in which dinode is to be created
531  * @name:  Name of new dinode
532  * @mode:
533  *
534  * Returns: errno
535  */
536
537 static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
538                      unsigned int mode)
539 {
540         int error;
541
542         error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
543         if (error)
544                 return error;
545
546         /*  Don't create entries in an unlinked directory  */
547         if (!dip->i_inode.i_nlink)
548                 return -EPERM;
549
550         error = gfs2_dir_check(&dip->i_inode, name, NULL);
551         switch (error) {
552         case -ENOENT:
553                 error = 0;
554                 break;
555         case 0:
556                 return -EEXIST;
557         default:
558                 return error;
559         }
560
561         if (dip->i_entries == (u32)-1)
562                 return -EFBIG;
563         if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
564                 return -EMLINK;
565
566         return 0;
567 }
568
569 static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
570                                unsigned int *uid, unsigned int *gid)
571 {
572         if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
573             (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
574                 if (S_ISDIR(*mode))
575                         *mode |= S_ISUID;
576                 else if (dip->i_inode.i_uid != current_fsuid())
577                         *mode &= ~07111;
578                 *uid = dip->i_inode.i_uid;
579         } else
580                 *uid = current_fsuid();
581
582         if (dip->i_inode.i_mode & S_ISGID) {
583                 if (S_ISDIR(*mode))
584                         *mode |= S_ISGID;
585                 *gid = dip->i_inode.i_gid;
586         } else
587                 *gid = current_fsgid();
588 }
589
590 static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
591 {
592         struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
593         int error;
594
595         if (gfs2_alloc_get(dip) == NULL)
596                 return -ENOMEM;
597
598         dip->i_alloc->al_requested = RES_DINODE;
599         error = gfs2_inplace_reserve(dip);
600         if (error)
601                 goto out;
602
603         error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
604         if (error)
605                 goto out_ipreserv;
606
607         error = gfs2_alloc_di(dip, no_addr, generation);
608
609         gfs2_trans_end(sdp);
610
611 out_ipreserv:
612         gfs2_inplace_release(dip);
613 out:
614         gfs2_alloc_put(dip);
615         return error;
616 }
617
618 /**
619  * init_dinode - Fill in a new dinode structure
620  * @dip: the directory this inode is being created in
621  * @gl: The glock covering the new inode
622  * @inum: the inode number
623  * @mode: the file permissions
624  * @uid:
625  * @gid:
626  *
627  */
628
629 static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
630                         const struct gfs2_inum_host *inum, unsigned int mode,
631                         unsigned int uid, unsigned int gid,
632                         const u64 *generation, dev_t dev, struct buffer_head **bhp)
633 {
634         struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
635         struct gfs2_dinode *di;
636         struct buffer_head *dibh;
637         struct timespec tv = CURRENT_TIME;
638
639         dibh = gfs2_meta_new(gl, inum->no_addr);
640         gfs2_trans_add_bh(gl, dibh, 1);
641         gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
642         gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
643         di = (struct gfs2_dinode *)dibh->b_data;
644
645         di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
646         di->di_num.no_addr = cpu_to_be64(inum->no_addr);
647         di->di_mode = cpu_to_be32(mode);
648         di->di_uid = cpu_to_be32(uid);
649         di->di_gid = cpu_to_be32(gid);
650         di->di_nlink = 0;
651         di->di_size = 0;
652         di->di_blocks = cpu_to_be64(1);
653         di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
654         di->di_major = cpu_to_be32(MAJOR(dev));
655         di->di_minor = cpu_to_be32(MINOR(dev));
656         di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
657         di->di_generation = cpu_to_be64(*generation);
658         di->di_flags = 0;
659
660         if (S_ISREG(mode)) {
661                 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
662                     gfs2_tune_get(sdp, gt_new_files_jdata))
663                         di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
664         } else if (S_ISDIR(mode)) {
665                 di->di_flags |= cpu_to_be32(dip->i_diskflags &
666                                             GFS2_DIF_INHERIT_JDATA);
667         }
668
669         di->__pad1 = 0;
670         di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
671         di->di_height = 0;
672         di->__pad2 = 0;
673         di->__pad3 = 0;
674         di->di_depth = 0;
675         di->di_entries = 0;
676         memset(&di->__pad4, 0, sizeof(di->__pad4));
677         di->di_eattr = 0;
678         di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
679         di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
680         di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
681         memset(&di->di_reserved, 0, sizeof(di->di_reserved));
682         
683         set_buffer_uptodate(dibh);
684
685         *bhp = dibh;
686 }
687
688 static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
689                        unsigned int mode, const struct gfs2_inum_host *inum,
690                        const u64 *generation, dev_t dev, struct buffer_head **bhp)
691 {
692         struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
693         unsigned int uid, gid;
694         int error;
695
696         munge_mode_uid_gid(dip, &mode, &uid, &gid);
697         if (!gfs2_alloc_get(dip))
698                 return -ENOMEM;
699
700         error = gfs2_quota_lock(dip, uid, gid);
701         if (error)
702                 goto out;
703
704         error = gfs2_quota_check(dip, uid, gid);
705         if (error)
706                 goto out_quota;
707
708         error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
709         if (error)
710                 goto out_quota;
711
712         init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
713         gfs2_quota_change(dip, +1, uid, gid);
714         gfs2_trans_end(sdp);
715
716 out_quota:
717         gfs2_quota_unlock(dip);
718 out:
719         gfs2_alloc_put(dip);
720         return error;
721 }
722
723 static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
724                        struct gfs2_inode *ip)
725 {
726         struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
727         struct gfs2_alloc *al;
728         int alloc_required;
729         struct buffer_head *dibh;
730         int error;
731
732         al = gfs2_alloc_get(dip);
733         if (!al)
734                 return -ENOMEM;
735
736         error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
737         if (error)
738                 goto fail;
739
740         error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
741         if (alloc_required < 0)
742                 goto fail_quota_locks;
743         if (alloc_required) {
744                 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
745                 if (error)
746                         goto fail_quota_locks;
747
748                 al->al_requested = sdp->sd_max_dirres;
749
750                 error = gfs2_inplace_reserve(dip);
751                 if (error)
752                         goto fail_quota_locks;
753
754                 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
755                                          al->al_rgd->rd_length +
756                                          2 * RES_DINODE +
757                                          RES_STATFS + RES_QUOTA, 0);
758                 if (error)
759                         goto fail_ipreserv;
760         } else {
761                 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
762                 if (error)
763                         goto fail_quota_locks;
764         }
765
766         error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
767         if (error)
768                 goto fail_end_trans;
769
770         error = gfs2_meta_inode_buffer(ip, &dibh);
771         if (error)
772                 goto fail_end_trans;
773         ip->i_inode.i_nlink = 1;
774         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
775         gfs2_dinode_out(ip, dibh->b_data);
776         brelse(dibh);
777         return 0;
778
779 fail_end_trans:
780         gfs2_trans_end(sdp);
781
782 fail_ipreserv:
783         if (dip->i_alloc->al_rgd)
784                 gfs2_inplace_release(dip);
785
786 fail_quota_locks:
787         gfs2_quota_unlock(dip);
788
789 fail:
790         gfs2_alloc_put(dip);
791         return error;
792 }
793
794 static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
795 {
796         int err;
797         size_t len;
798         void *value;
799         char *name;
800
801         err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
802                                            &name, &value, &len);
803
804         if (err) {
805                 if (err == -EOPNOTSUPP)
806                         return 0;
807                 return err;
808         }
809
810         err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
811                                GFS2_EATYPE_SECURITY);
812         kfree(value);
813         kfree(name);
814
815         return err;
816 }
817
818 /**
819  * gfs2_createi - Create a new inode
820  * @ghs: An array of two holders
821  * @name: The name of the new file
822  * @mode: the permissions on the new inode
823  *
824  * @ghs[0] is an initialized holder for the directory
825  * @ghs[1] is the holder for the inode lock
826  *
827  * If the return value is not NULL, the glocks on both the directory and the new
828  * file are held.  A transaction has been started and an inplace reservation
829  * is held, as well.
830  *
831  * Returns: An inode
832  */
833
834 struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
835                            unsigned int mode, dev_t dev)
836 {
837         struct inode *inode = NULL;
838         struct gfs2_inode *dip = ghs->gh_gl->gl_object;
839         struct inode *dir = &dip->i_inode;
840         struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
841         struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
842         int error;
843         u64 generation;
844         struct buffer_head *bh = NULL;
845
846         if (!name->len || name->len > GFS2_FNAMESIZE)
847                 return ERR_PTR(-ENAMETOOLONG);
848
849         gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
850         error = gfs2_glock_nq(ghs);
851         if (error)
852                 goto fail;
853
854         error = create_ok(dip, name, mode);
855         if (error)
856                 goto fail_gunlock;
857
858         error = alloc_dinode(dip, &inum.no_addr, &generation);
859         if (error)
860                 goto fail_gunlock;
861         inum.no_formal_ino = generation;
862
863         error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
864                                   LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
865         if (error)
866                 goto fail_gunlock;
867
868         error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
869         if (error)
870                 goto fail_gunlock2;
871
872         inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
873                                   inum.no_formal_ino);
874         if (IS_ERR(inode))
875                 goto fail_gunlock2;
876
877         error = gfs2_inode_refresh(GFS2_I(inode));
878         if (error)
879                 goto fail_gunlock2;
880
881         error = gfs2_acl_create(dip, inode);
882         if (error)
883                 goto fail_gunlock2;
884
885         error = gfs2_security_init(dip, GFS2_I(inode));
886         if (error)
887                 goto fail_gunlock2;
888
889         error = link_dinode(dip, name, GFS2_I(inode));
890         if (error)
891                 goto fail_gunlock2;
892
893         if (bh)
894                 brelse(bh);
895         return inode;
896
897 fail_gunlock2:
898         gfs2_glock_dq_uninit(ghs + 1);
899         if (inode && !IS_ERR(inode))
900                 iput(inode);
901 fail_gunlock:
902         gfs2_glock_dq(ghs);
903 fail:
904         if (bh)
905                 brelse(bh);
906         return ERR_PTR(error);
907 }
908
909 static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
910 {
911         struct inode *inode = &ip->i_inode;
912         struct buffer_head *dibh;
913         int error;
914
915         error = gfs2_meta_inode_buffer(ip, &dibh);
916         if (error)
917                 return error;
918
919         if ((attr->ia_valid & ATTR_SIZE) &&
920             attr->ia_size != i_size_read(inode)) {
921                 error = vmtruncate(inode, attr->ia_size);
922                 if (error)
923                         return error;
924         }
925
926         setattr_copy(inode, attr);
927         mark_inode_dirty(inode);
928
929         gfs2_assert_warn(GFS2_SB(inode), !error);
930         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
931         gfs2_dinode_out(ip, dibh->b_data);
932         brelse(dibh);
933         return 0;
934 }
935
936 /**
937  * gfs2_setattr_simple -
938  * @ip:
939  * @attr:
940  *
941  * Called with a reference on the vnode.
942  *
943  * Returns: errno
944  */
945
946 int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
947 {
948         int error;
949
950         if (current->journal_info)
951                 return __gfs2_setattr_simple(ip, attr);
952
953         error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
954         if (error)
955                 return error;
956
957         error = __gfs2_setattr_simple(ip, attr);
958         gfs2_trans_end(GFS2_SB(&ip->i_inode));
959         return error;
960 }
961
962 void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
963 {
964         struct gfs2_dinode *str = buf;
965
966         str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
967         str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
968         str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
969         str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
970         str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
971         str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
972         str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
973         str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
974         str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
975         str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
976         str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
977         str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
978         str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
979         str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
980
981         str->di_goal_meta = cpu_to_be64(ip->i_goal);
982         str->di_goal_data = cpu_to_be64(ip->i_goal);
983         str->di_generation = cpu_to_be64(ip->i_generation);
984
985         str->di_flags = cpu_to_be32(ip->i_diskflags);
986         str->di_height = cpu_to_be16(ip->i_height);
987         str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
988                                              !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
989                                              GFS2_FORMAT_DE : 0);
990         str->di_depth = cpu_to_be16(ip->i_depth);
991         str->di_entries = cpu_to_be32(ip->i_entries);
992
993         str->di_eattr = cpu_to_be64(ip->i_eattr);
994         str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
995         str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
996         str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
997 }
998
999 void gfs2_dinode_print(const struct gfs2_inode *ip)
1000 {
1001         printk(KERN_INFO "  no_formal_ino = %llu\n",
1002                (unsigned long long)ip->i_no_formal_ino);
1003         printk(KERN_INFO "  no_addr = %llu\n",
1004                (unsigned long long)ip->i_no_addr);
1005         printk(KERN_INFO "  i_size = %llu\n",
1006                (unsigned long long)i_size_read(&ip->i_inode));
1007         printk(KERN_INFO "  blocks = %llu\n",
1008                (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
1009         printk(KERN_INFO "  i_goal = %llu\n",
1010                (unsigned long long)ip->i_goal);
1011         printk(KERN_INFO "  i_diskflags = 0x%.8X\n", ip->i_diskflags);
1012         printk(KERN_INFO "  i_height = %u\n", ip->i_height);
1013         printk(KERN_INFO "  i_depth = %u\n", ip->i_depth);
1014         printk(KERN_INFO "  i_entries = %u\n", ip->i_entries);
1015         printk(KERN_INFO "  i_eattr = %llu\n",
1016                (unsigned long long)ip->i_eattr);
1017 }
1018