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