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