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