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