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