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