blob: c2c7d2b63a577f850164a8684b688282a440f9a5 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
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 v.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>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050017#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050018#include <linux/crc32.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000019
20#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include "lm_interface.h"
22#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000023#include "acl.h"
24#include "bmap.h"
25#include "dir.h"
26#include "eattr.h"
27#include "glock.h"
28#include "glops.h"
29#include "inode.h"
30#include "log.h"
31#include "meta_io.h"
32#include "ops_address.h"
33#include "ops_file.h"
34#include "ops_inode.h"
35#include "quota.h"
36#include "rgrp.h"
37#include "trans.h"
38#include "unlinked.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050039#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000040
41/**
42 * inode_attr_in - Copy attributes from the dinode into the VFS inode
43 * @ip: The GFS2 inode (with embedded disk inode data)
44 * @inode: The Linux VFS inode
45 *
46 */
47
48static void inode_attr_in(struct gfs2_inode *ip, struct inode *inode)
49{
50 inode->i_ino = ip->i_num.no_formal_ino;
51
52 switch (ip->i_di.di_mode & S_IFMT) {
53 case S_IFBLK:
54 case S_IFCHR:
55 inode->i_rdev = MKDEV(ip->i_di.di_major, ip->i_di.di_minor);
56 break;
57 default:
58 inode->i_rdev = 0;
59 break;
60 };
61
62 inode->i_mode = ip->i_di.di_mode;
63 inode->i_nlink = ip->i_di.di_nlink;
64 inode->i_uid = ip->i_di.di_uid;
65 inode->i_gid = ip->i_di.di_gid;
66 i_size_write(inode, ip->i_di.di_size);
67 inode->i_atime.tv_sec = ip->i_di.di_atime;
68 inode->i_mtime.tv_sec = ip->i_di.di_mtime;
69 inode->i_ctime.tv_sec = ip->i_di.di_ctime;
70 inode->i_atime.tv_nsec = 0;
71 inode->i_mtime.tv_nsec = 0;
72 inode->i_ctime.tv_nsec = 0;
73 inode->i_blksize = PAGE_SIZE;
74 inode->i_blocks = ip->i_di.di_blocks <<
75 (ip->i_sbd->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT);
76
77 if (ip->i_di.di_flags & GFS2_DIF_IMMUTABLE)
78 inode->i_flags |= S_IMMUTABLE;
79 else
80 inode->i_flags &= ~S_IMMUTABLE;
81
82 if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY)
83 inode->i_flags |= S_APPEND;
84 else
85 inode->i_flags &= ~S_APPEND;
86}
87
88/**
89 * gfs2_inode_attr_in - Copy attributes from the dinode into the VFS inode
90 * @ip: The GFS2 inode (with embedded disk inode data)
91 *
92 */
93
94void gfs2_inode_attr_in(struct gfs2_inode *ip)
95{
96 struct inode *inode;
97
98 inode = gfs2_ip2v_lookup(ip);
99 if (inode) {
100 inode_attr_in(ip, inode);
101 iput(inode);
102 }
103}
104
105/**
106 * gfs2_inode_attr_out - Copy attributes from VFS inode into the dinode
107 * @ip: The GFS2 inode
108 *
109 * Only copy out the attributes that we want the VFS layer
110 * to be able to modify.
111 */
112
113void gfs2_inode_attr_out(struct gfs2_inode *ip)
114{
115 struct inode *inode = ip->i_vnode;
116
117 gfs2_assert_withdraw(ip->i_sbd,
118 (ip->i_di.di_mode & S_IFMT) == (inode->i_mode & S_IFMT));
119 ip->i_di.di_mode = inode->i_mode;
120 ip->i_di.di_uid = inode->i_uid;
121 ip->i_di.di_gid = inode->i_gid;
122 ip->i_di.di_atime = inode->i_atime.tv_sec;
123 ip->i_di.di_mtime = inode->i_mtime.tv_sec;
124 ip->i_di.di_ctime = inode->i_ctime.tv_sec;
125}
126
127/**
128 * gfs2_ip2v_lookup - Get the struct inode for a struct gfs2_inode
129 * @ip: the struct gfs2_inode to get the struct inode for
130 *
131 * Returns: A VFS inode, or NULL if none
132 */
133
134struct inode *gfs2_ip2v_lookup(struct gfs2_inode *ip)
135{
136 struct inode *inode = NULL;
137
138 gfs2_assert_warn(ip->i_sbd, test_bit(GIF_MIN_INIT, &ip->i_flags));
139
140 spin_lock(&ip->i_spin);
141 if (ip->i_vnode)
142 inode = igrab(ip->i_vnode);
143 spin_unlock(&ip->i_spin);
144
145 return inode;
146}
147
148/**
149 * gfs2_ip2v - Get/Create a struct inode for a struct gfs2_inode
150 * @ip: the struct gfs2_inode to get the struct inode for
151 *
152 * Returns: A VFS inode, or NULL if no mem
153 */
154
155struct inode *gfs2_ip2v(struct gfs2_inode *ip)
156{
157 struct inode *inode, *tmp;
158
159 inode = gfs2_ip2v_lookup(ip);
160 if (inode)
161 return inode;
162
163 tmp = new_inode(ip->i_sbd->sd_vfs);
164 if (!tmp)
165 return NULL;
166
167 inode_attr_in(ip, tmp);
168
169 if (S_ISREG(ip->i_di.di_mode)) {
170 tmp->i_op = &gfs2_file_iops;
171 tmp->i_fop = &gfs2_file_fops;
172 tmp->i_mapping->a_ops = &gfs2_file_aops;
173 } else if (S_ISDIR(ip->i_di.di_mode)) {
174 tmp->i_op = &gfs2_dir_iops;
175 tmp->i_fop = &gfs2_dir_fops;
176 } else if (S_ISLNK(ip->i_di.di_mode)) {
177 tmp->i_op = &gfs2_symlink_iops;
178 } else {
179 tmp->i_op = &gfs2_dev_iops;
180 init_special_inode(tmp, tmp->i_mode, tmp->i_rdev);
181 }
182
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500183 tmp->u.generic_ip = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000184
185 for (;;) {
186 spin_lock(&ip->i_spin);
187 if (!ip->i_vnode)
188 break;
189 inode = igrab(ip->i_vnode);
190 spin_unlock(&ip->i_spin);
191
192 if (inode) {
193 iput(tmp);
194 return inode;
195 }
196 yield();
197 }
198
199 inode = tmp;
200
201 gfs2_inode_hold(ip);
202 ip->i_vnode = inode;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500203 inode->u.generic_ip = ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204
205 spin_unlock(&ip->i_spin);
206
207 insert_inode_hash(inode);
208
209 return inode;
210}
211
212static int iget_test(struct inode *inode, void *opaque)
213{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500214 struct gfs2_inode *ip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215 struct gfs2_inum *inum = (struct gfs2_inum *)opaque;
216
217 if (ip && ip->i_num.no_addr == inum->no_addr)
218 return 1;
219
220 return 0;
221}
222
223struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum *inum)
224{
225 return ilookup5(sb, (unsigned long)inum->no_formal_ino,
226 iget_test, inum);
227}
228
229void gfs2_inode_min_init(struct gfs2_inode *ip, unsigned int type)
230{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000231 if (!test_and_set_bit(GIF_MIN_INIT, &ip->i_flags)) {
232 ip->i_di.di_nlink = 1;
233 ip->i_di.di_mode = DT2IF(type);
234 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235}
236
237/**
238 * gfs2_inode_refresh - Refresh the incore copy of the dinode
239 * @ip: The GFS2 inode
240 *
241 * Returns: errno
242 */
243
244int gfs2_inode_refresh(struct gfs2_inode *ip)
245{
246 struct buffer_head *dibh;
247 int error;
248
249 error = gfs2_meta_inode_buffer(ip, &dibh);
250 if (error)
251 return error;
252
253 if (gfs2_metatype_check(ip->i_sbd, dibh, GFS2_METATYPE_DI)) {
254 brelse(dibh);
255 return -EIO;
256 }
257
David Teiglandb3b94fa2006-01-16 16:50:04 +0000258 gfs2_dinode_in(&ip->i_di, dibh->b_data);
259 set_bit(GIF_MIN_INIT, &ip->i_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260
261 brelse(dibh);
262
263 if (ip->i_num.no_addr != ip->i_di.di_num.no_addr) {
264 if (gfs2_consist_inode(ip))
265 gfs2_dinode_print(&ip->i_di);
266 return -EIO;
267 }
268 if (ip->i_num.no_formal_ino != ip->i_di.di_num.no_formal_ino)
269 return -ESTALE;
270
271 ip->i_vn = ip->i_gl->gl_vn;
272
273 return 0;
274}
275
276/**
277 * inode_create - create a struct gfs2_inode
278 * @i_gl: The glock covering the inode
279 * @inum: The inode number
280 * @io_gl: the iopen glock to acquire/hold (using holder in new gfs2_inode)
281 * @io_state: the state the iopen glock should be acquired in
282 * @ipp: pointer to put the returned inode in
283 *
284 * Returns: errno
285 */
286
Steven Whitehousec9fd4302006-03-01 15:31:02 -0500287static int inode_create(struct gfs2_glock *i_gl, const struct gfs2_inum *inum,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288 struct gfs2_glock *io_gl, unsigned int io_state,
Steven Whitehouse36327522006-04-28 10:46:21 -0400289 struct gfs2_inode **ipp, int need_lock)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000290{
291 struct gfs2_sbd *sdp = i_gl->gl_sbd;
292 struct gfs2_inode *ip;
293 int error = 0;
294
295 ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
296 if (!ip)
297 return -ENOMEM;
298 memset(ip, 0, sizeof(struct gfs2_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000299 ip->i_num = *inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000300 atomic_set(&ip->i_count, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000301 ip->i_vn = i_gl->gl_vn - 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000302 ip->i_gl = i_gl;
303 ip->i_sbd = sdp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000304 spin_lock_init(&ip->i_spin);
305 init_rwsem(&ip->i_rw_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000306 ip->i_greedy = gfs2_tune_get(sdp, gt_greedy_default);
307
Steven Whitehouse36327522006-04-28 10:46:21 -0400308 if (need_lock) {
309 error = gfs2_glock_nq_init(io_gl,
310 io_state, GL_LOCAL_EXCL | GL_EXACT,
311 &ip->i_iopen_gh);
312 if (error)
313 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314
Steven Whitehouse64c14ea2006-05-16 13:37:11 -0400315 spin_lock(&io_gl->gl_spin);
316 gfs2_glock_hold(i_gl);
317 io_gl->gl_object = i_gl;
318 spin_unlock(&io_gl->gl_spin);
319 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000320
321 gfs2_glock_hold(i_gl);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500322 i_gl->gl_object = ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323 atomic_inc(&sdp->sd_inode_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000324 *ipp = ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000325 return 0;
326
Steven Whitehouse64c14ea2006-05-16 13:37:11 -0400327fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000328 gfs2_meta_cache_flush(ip);
329 kmem_cache_free(gfs2_inode_cachep, ip);
330 *ipp = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000331 return error;
332}
333
334/**
335 * gfs2_inode_get - Create or get a reference on an inode
336 * @i_gl: The glock covering the inode
337 * @inum: The inode number
338 * @create:
339 * @ipp: pointer to put the returned inode in
340 *
341 * Returns: errno
342 */
343
Steven Whitehousec9fd4302006-03-01 15:31:02 -0500344int gfs2_inode_get(struct gfs2_glock *i_gl, const struct gfs2_inum *inum,
345 int create, struct gfs2_inode **ipp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346{
347 struct gfs2_sbd *sdp = i_gl->gl_sbd;
348 struct gfs2_glock *io_gl;
349 int error = 0;
350
351 gfs2_glmutex_lock(i_gl);
352
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500353 *ipp = i_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354 if (*ipp) {
355 error = -ESTALE;
356 if ((*ipp)->i_num.no_formal_ino != inum->no_formal_ino)
357 goto out;
358 atomic_inc(&(*ipp)->i_count);
359 error = 0;
360 goto out;
361 }
362
363 if (!create)
364 goto out;
365
366 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops,
367 CREATE, &io_gl);
368 if (!error) {
Steven Whitehouse36327522006-04-28 10:46:21 -0400369 error = inode_create(i_gl, inum, io_gl, LM_ST_SHARED, ipp, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000370 gfs2_glock_put(io_gl);
371 }
372
373 out:
374 gfs2_glmutex_unlock(i_gl);
375
376 return error;
377}
378
379void gfs2_inode_hold(struct gfs2_inode *ip)
380{
381 gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0);
382 atomic_inc(&ip->i_count);
383}
384
385void gfs2_inode_put(struct gfs2_inode *ip)
386{
387 gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0);
388 atomic_dec(&ip->i_count);
389}
390
Steven Whitehouse36327522006-04-28 10:46:21 -0400391void gfs2_inode_destroy(struct gfs2_inode *ip, int unlock)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000392{
393 struct gfs2_sbd *sdp = ip->i_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394 struct gfs2_glock *i_gl = ip->i_gl;
395
396 gfs2_assert_warn(sdp, !atomic_read(&ip->i_count));
Steven Whitehouse36327522006-04-28 10:46:21 -0400397 if (unlock) {
398 struct gfs2_glock *io_gl = ip->i_iopen_gh.gh_gl;
399 gfs2_assert(sdp, io_gl->gl_object == i_gl);
400
401 spin_lock(&io_gl->gl_spin);
402 io_gl->gl_object = NULL;
403 spin_unlock(&io_gl->gl_spin);
404 gfs2_glock_put(i_gl);
405
406 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
407 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408
409 gfs2_meta_cache_flush(ip);
410 kmem_cache_free(gfs2_inode_cachep, ip);
411
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500412 i_gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000413 gfs2_glock_put(i_gl);
414
415 atomic_dec(&sdp->sd_inode_count);
416}
417
418static int dinode_dealloc(struct gfs2_inode *ip, struct gfs2_unlinked *ul)
419{
420 struct gfs2_sbd *sdp = ip->i_sbd;
421 struct gfs2_alloc *al;
422 struct gfs2_rgrpd *rgd;
423 int error;
424
425 if (ip->i_di.di_blocks != 1) {
426 if (gfs2_consist_inode(ip))
427 gfs2_dinode_print(&ip->i_di);
428 return -EIO;
429 }
430
431 al = gfs2_alloc_get(ip);
432
433 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
434 if (error)
435 goto out;
436
437 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
438 if (error)
439 goto out_qs;
440
441 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
442 if (!rgd) {
443 gfs2_consist_inode(ip);
444 error = -EIO;
445 goto out_rindex_relse;
446 }
447
448 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
449 &al->al_rgd_gh);
450 if (error)
451 goto out_rindex_relse;
452
453 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED +
454 RES_STATFS + RES_QUOTA, 1);
455 if (error)
456 goto out_rg_gunlock;
457
458 gfs2_trans_add_gl(ip->i_gl);
459
460 gfs2_free_di(rgd, ip);
461
462 error = gfs2_unlinked_ondisk_rm(sdp, ul);
463
464 gfs2_trans_end(sdp);
465 clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
466
467 out_rg_gunlock:
468 gfs2_glock_dq_uninit(&al->al_rgd_gh);
469
470 out_rindex_relse:
471 gfs2_glock_dq_uninit(&al->al_ri_gh);
472
473 out_qs:
474 gfs2_quota_unhold(ip);
475
476 out:
477 gfs2_alloc_put(ip);
478
479 return error;
480}
481
482/**
483 * inode_dealloc - Deallocate all on-disk blocks for an inode (dinode)
484 * @sdp: the filesystem
485 * @inum: the inode number to deallocate
486 * @io_gh: a holder for the iopen glock for this inode
487 *
Steven Whitehouse36327522006-04-28 10:46:21 -0400488 * N.B. When we enter this we already hold the iopen glock and getting
489 * the glock for the inode means that we are grabbing the locks in the
490 * "wrong" order so we must only so a try lock operation and fail if we
491 * don't get the lock. Thats ok, since if we fail it means someone else
492 * is using the inode still and thus we shouldn't be deallocating it
493 * anyway.
494 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000495 * Returns: errno
496 */
497
498static int inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul,
499 struct gfs2_holder *io_gh)
500{
501 struct gfs2_inode *ip;
502 struct gfs2_holder i_gh;
503 int error;
504
Steven Whitehouse36327522006-04-28 10:46:21 -0400505 error = gfs2_glock_nq_num(sdp, ul->ul_ut.ut_inum.no_addr,
506 &gfs2_inode_glops, LM_ST_EXCLUSIVE,
Steven Whitehouse320dd102006-05-18 16:25:27 -0400507 LM_FLAG_TRY_1CB|GL_DUMP, &i_gh);
Steven Whitehouse36327522006-04-28 10:46:21 -0400508 switch(error) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000509 case 0:
510 break;
511 case GLR_TRYFAILED:
Steven Whitehouse36327522006-04-28 10:46:21 -0400512 return 1; /* or back off and relock in different order? */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513 default:
Steven Whitehouse36327522006-04-28 10:46:21 -0400514 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000515 }
516
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500517 gfs2_assert_warn(sdp, !i_gh.gh_gl->gl_object);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000518 error = inode_create(i_gh.gh_gl, &ul->ul_ut.ut_inum, io_gh->gh_gl,
Steven Whitehouse36327522006-04-28 10:46:21 -0400519 LM_ST_EXCLUSIVE, &ip, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520
521 if (error)
522 goto out;
523
524 error = gfs2_inode_refresh(ip);
525 if (error)
526 goto out_iput;
527
528 if (ip->i_di.di_nlink) {
529 if (gfs2_consist_inode(ip))
530 gfs2_dinode_print(&ip->i_di);
531 error = -EIO;
532 goto out_iput;
533 }
534
535 if (S_ISDIR(ip->i_di.di_mode) &&
536 (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
537 error = gfs2_dir_exhash_dealloc(ip);
538 if (error)
539 goto out_iput;
540 }
541
542 if (ip->i_di.di_eattr) {
543 error = gfs2_ea_dealloc(ip);
544 if (error)
545 goto out_iput;
546 }
547
548 if (!gfs2_is_stuffed(ip)) {
549 error = gfs2_file_dealloc(ip);
550 if (error)
551 goto out_iput;
552 }
553
554 error = dinode_dealloc(ip, ul);
555 if (error)
556 goto out_iput;
557
Steven Whitehouse36327522006-04-28 10:46:21 -0400558out_iput:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559 gfs2_glmutex_lock(i_gh.gh_gl);
560 gfs2_inode_put(ip);
Steven Whitehouse36327522006-04-28 10:46:21 -0400561 gfs2_inode_destroy(ip, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 gfs2_glmutex_unlock(i_gh.gh_gl);
563
Steven Whitehouse36327522006-04-28 10:46:21 -0400564out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 gfs2_glock_dq_uninit(&i_gh);
566
567 return error;
568}
569
570/**
571 * try_inode_dealloc - Try to deallocate an inode and all its blocks
572 * @sdp: the filesystem
573 *
574 * Returns: 0 on success, -errno on error, 1 on busy (inode open)
575 */
576
577static int try_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
578{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000579 int error = 0;
Steven Whitehouse36327522006-04-28 10:46:21 -0400580 struct gfs2_holder iogh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000581
582 gfs2_try_toss_inode(sdp, &ul->ul_ut.ut_inum);
Steven Whitehouse36327522006-04-28 10:46:21 -0400583 error = gfs2_glock_nq_num(sdp, ul->ul_ut.ut_inum.no_addr,
584 &gfs2_iopen_glops, LM_ST_EXCLUSIVE,
585 LM_FLAG_TRY_1CB, &iogh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586 switch (error) {
587 case 0:
588 break;
589 case GLR_TRYFAILED:
590 return 1;
591 default:
592 return error;
593 }
594
Steven Whitehouse36327522006-04-28 10:46:21 -0400595 error = inode_dealloc(sdp, ul, &iogh);
596 gfs2_glock_dq_uninit(&iogh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597
598 return error;
599}
600
601static int inode_dealloc_uninit(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
602{
603 struct gfs2_rgrpd *rgd;
604 struct gfs2_holder ri_gh, rgd_gh;
605 int error;
606
607 error = gfs2_rindex_hold(sdp, &ri_gh);
608 if (error)
609 return error;
610
611 rgd = gfs2_blk2rgrpd(sdp, ul->ul_ut.ut_inum.no_addr);
612 if (!rgd) {
613 gfs2_consist(sdp);
614 error = -EIO;
615 goto out;
616 }
617
618 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rgd_gh);
619 if (error)
620 goto out;
621
Steven Whitehouse36327522006-04-28 10:46:21 -0400622 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED + RES_STATFS, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000623 if (error)
624 goto out_gunlock;
625
626 gfs2_free_uninit_di(rgd, ul->ul_ut.ut_inum.no_addr);
627 gfs2_unlinked_ondisk_rm(sdp, ul);
628
629 gfs2_trans_end(sdp);
630
631 out_gunlock:
632 gfs2_glock_dq_uninit(&rgd_gh);
633 out:
634 gfs2_glock_dq_uninit(&ri_gh);
635
636 return error;
637}
638
639int gfs2_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
640{
641 if (ul->ul_ut.ut_flags & GFS2_UTF_UNINIT)
642 return inode_dealloc_uninit(sdp, ul);
643 else
644 return try_inode_dealloc(sdp, ul);
645}
646
647/**
648 * gfs2_change_nlink - Change nlink count on inode
649 * @ip: The GFS2 inode
650 * @diff: The change in the nlink count required
651 *
652 * Returns: errno
653 */
654
655int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
656{
657 struct buffer_head *dibh;
658 uint32_t nlink;
659 int error;
660
661 nlink = ip->i_di.di_nlink + diff;
662
663 /* If we are reducing the nlink count, but the new value ends up being
664 bigger than the old one, we must have underflowed. */
665 if (diff < 0 && nlink > ip->i_di.di_nlink) {
666 if (gfs2_consist_inode(ip))
667 gfs2_dinode_print(&ip->i_di);
668 return -EIO;
669 }
670
671 error = gfs2_meta_inode_buffer(ip, &dibh);
672 if (error)
673 return error;
674
675 ip->i_di.di_nlink = nlink;
676 ip->i_di.di_ctime = get_seconds();
677
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000678 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000679 gfs2_dinode_out(&ip->i_di, dibh->b_data);
680 brelse(dibh);
681
682 return 0;
683}
684
Steven Whitehousec7526662006-03-20 12:30:04 -0500685struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
686{
687 struct qstr qstr;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500688 gfs2_str2qstr(&qstr, name);
Steven Whitehousec7526662006-03-20 12:30:04 -0500689 return gfs2_lookupi(dip, &qstr, 1, NULL);
690}
691
692
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693/**
694 * gfs2_lookupi - Look up a filename in a directory and return its inode
695 * @d_gh: An initialized holder for the directory glock
696 * @name: The name of the inode to look for
697 * @is_root: If 1, ignore the caller's permissions
698 * @i_gh: An uninitialized holder for the new inode glock
699 *
700 * There will always be a vnode (Linux VFS inode) for the d_gh inode unless
701 * @is_root is true.
702 *
703 * Returns: errno
704 */
705
Steven Whitehousec7526662006-03-20 12:30:04 -0500706struct inode *gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root,
707 struct nameidata *nd)
708
David Teiglandb3b94fa2006-01-16 16:50:04 +0000709{
Steven Whitehousec9fd4302006-03-01 15:31:02 -0500710 struct super_block *sb = dir->i_sb;
Steven Whitehouse7359a192006-02-13 12:27:43 +0000711 struct gfs2_inode *ipp;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500712 struct gfs2_inode *dip = dir->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000713 struct gfs2_sbd *sdp = dip->i_sbd;
714 struct gfs2_holder d_gh;
715 struct gfs2_inum inum;
716 unsigned int type;
717 struct gfs2_glock *gl;
Steven Whitehouse7359a192006-02-13 12:27:43 +0000718 int error = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500719 struct inode *inode = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720
721 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousec7526662006-03-20 12:30:04 -0500722 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723
Steven Whitehousec7526662006-03-20 12:30:04 -0500724 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
725 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
726 dir == sb->s_root->d_inode)) {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400727 igrab(dir);
728 return dir;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 }
730
731 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
732 if (error)
Steven Whitehousec7526662006-03-20 12:30:04 -0500733 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734
735 if (!is_root) {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400736 error = gfs2_repermission(dir, MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737 if (error)
738 goto out;
739 }
740
Steven Whitehousec7526662006-03-20 12:30:04 -0500741 error = gfs2_dir_search(dir, name, &inum, &type);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000742 if (error)
743 goto out;
744
745 error = gfs2_glock_get(sdp, inum.no_addr, &gfs2_inode_glops,
746 CREATE, &gl);
747 if (error)
748 goto out;
749
Steven Whitehouse7359a192006-02-13 12:27:43 +0000750 error = gfs2_inode_get(gl, &inum, CREATE, &ipp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000751 if (!error)
Steven Whitehouse7359a192006-02-13 12:27:43 +0000752 gfs2_inode_min_init(ipp, type);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753
754 gfs2_glock_put(gl);
755
Steven Whitehouse7359a192006-02-13 12:27:43 +0000756out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500758 if (error == -ENOENT)
759 return NULL;
Steven Whitehouse7359a192006-02-13 12:27:43 +0000760 if (error == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500761 inode = gfs2_ip2v(ipp);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000762 gfs2_inode_put(ipp);
Steven Whitehousec7526662006-03-20 12:30:04 -0500763 if (!inode)
764 return ERR_PTR(-ENOMEM);
765 return inode;
Steven Whitehouse7359a192006-02-13 12:27:43 +0000766 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500767 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000768}
769
770static int pick_formal_ino_1(struct gfs2_sbd *sdp, uint64_t *formal_ino)
771{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500772 struct gfs2_inode *ip = sdp->sd_ir_inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773 struct buffer_head *bh;
774 struct gfs2_inum_range ir;
775 int error;
776
777 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
778 if (error)
779 return error;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000780 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781
782 error = gfs2_meta_inode_buffer(ip, &bh);
783 if (error) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000784 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785 gfs2_trans_end(sdp);
786 return error;
787 }
788
789 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
790
791 if (ir.ir_length) {
792 *formal_ino = ir.ir_start++;
793 ir.ir_length--;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000794 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795 gfs2_inum_range_out(&ir,
796 bh->b_data + sizeof(struct gfs2_dinode));
797 brelse(bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000798 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799 gfs2_trans_end(sdp);
800 return 0;
801 }
802
803 brelse(bh);
804
Steven Whitehousef55ab262006-02-21 12:51:39 +0000805 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000806 gfs2_trans_end(sdp);
807
808 return 1;
809}
810
811static int pick_formal_ino_2(struct gfs2_sbd *sdp, uint64_t *formal_ino)
812{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500813 struct gfs2_inode *ip = sdp->sd_ir_inode->u.generic_ip;
814 struct gfs2_inode *m_ip = sdp->sd_inum_inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 struct gfs2_holder gh;
816 struct buffer_head *bh;
817 struct gfs2_inum_range ir;
818 int error;
819
820 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
821 if (error)
822 return error;
823
824 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
825 if (error)
826 goto out;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000827 mutex_lock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828
829 error = gfs2_meta_inode_buffer(ip, &bh);
830 if (error)
831 goto out_end_trans;
832
833 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
834
835 if (!ir.ir_length) {
836 struct buffer_head *m_bh;
837 uint64_t x, y;
838
839 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
840 if (error)
841 goto out_brelse;
842
843 x = *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode));
844 x = y = be64_to_cpu(x);
845 ir.ir_start = x;
846 ir.ir_length = GFS2_INUM_QUANTUM;
847 x += GFS2_INUM_QUANTUM;
848 if (x < y)
849 gfs2_consist_inode(m_ip);
850 x = cpu_to_be64(x);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000851 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852 *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = x;
853
854 brelse(m_bh);
855 }
856
857 *formal_ino = ir.ir_start++;
858 ir.ir_length--;
859
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000860 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
862
863 out_brelse:
864 brelse(bh);
865
866 out_end_trans:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000867 mutex_unlock(&sdp->sd_inum_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000868 gfs2_trans_end(sdp);
869
870 out:
871 gfs2_glock_dq_uninit(&gh);
872
873 return error;
874}
875
876static int pick_formal_ino(struct gfs2_sbd *sdp, uint64_t *inum)
877{
878 int error;
879
880 error = pick_formal_ino_1(sdp, inum);
881 if (error <= 0)
882 return error;
883
884 error = pick_formal_ino_2(sdp, inum);
885
886 return error;
887}
888
889/**
890 * create_ok - OK to create a new on-disk inode here?
891 * @dip: Directory in which dinode is to be created
892 * @name: Name of new dinode
893 * @mode:
894 *
895 * Returns: errno
896 */
897
898static int create_ok(struct gfs2_inode *dip, struct qstr *name,
899 unsigned int mode)
900{
901 int error;
902
903 error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL);
904 if (error)
905 return error;
906
907 /* Don't create entries in an unlinked directory */
908 if (!dip->i_di.di_nlink)
909 return -EPERM;
910
Steven Whitehousec7526662006-03-20 12:30:04 -0500911 error = gfs2_dir_search(dip->i_vnode, name, NULL, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000912 switch (error) {
913 case -ENOENT:
914 error = 0;
915 break;
916 case 0:
917 return -EEXIST;
918 default:
919 return error;
920 }
921
922 if (dip->i_di.di_entries == (uint32_t)-1)
923 return -EFBIG;
924 if (S_ISDIR(mode) && dip->i_di.di_nlink == (uint32_t)-1)
925 return -EMLINK;
926
927 return 0;
928}
929
930static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
931 unsigned int *uid, unsigned int *gid)
932{
933 if (dip->i_sbd->sd_args.ar_suiddir &&
934 (dip->i_di.di_mode & S_ISUID) &&
935 dip->i_di.di_uid) {
936 if (S_ISDIR(*mode))
937 *mode |= S_ISUID;
938 else if (dip->i_di.di_uid != current->fsuid)
939 *mode &= ~07111;
940 *uid = dip->i_di.di_uid;
941 } else
942 *uid = current->fsuid;
943
944 if (dip->i_di.di_mode & S_ISGID) {
945 if (S_ISDIR(*mode))
946 *mode |= S_ISGID;
947 *gid = dip->i_di.di_gid;
948 } else
949 *gid = current->fsgid;
950}
951
952static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_unlinked *ul)
953{
954 struct gfs2_sbd *sdp = dip->i_sbd;
955 int error;
956
957 gfs2_alloc_get(dip);
958
959 dip->i_alloc.al_requested = RES_DINODE;
960 error = gfs2_inplace_reserve(dip);
961 if (error)
962 goto out;
963
964 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED +
965 RES_STATFS, 0);
966 if (error)
967 goto out_ipreserv;
968
969 ul->ul_ut.ut_inum.no_addr = gfs2_alloc_di(dip);
970
971 ul->ul_ut.ut_flags = GFS2_UTF_UNINIT;
972 error = gfs2_unlinked_ondisk_add(sdp, ul);
973
974 gfs2_trans_end(sdp);
975
976 out_ipreserv:
977 gfs2_inplace_release(dip);
978
979 out:
980 gfs2_alloc_put(dip);
981
982 return error;
983}
984
985/**
986 * init_dinode - Fill in a new dinode structure
987 * @dip: the directory this inode is being created in
988 * @gl: The glock covering the new inode
989 * @inum: the inode number
990 * @mode: the file permissions
991 * @uid:
992 * @gid:
993 *
994 */
995
996static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
997 struct gfs2_inum *inum, unsigned int mode,
998 unsigned int uid, unsigned int gid)
999{
1000 struct gfs2_sbd *sdp = dip->i_sbd;
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +00001001 struct gfs2_dinode *di;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001002 struct buffer_head *dibh;
1003
1004 dibh = gfs2_meta_new(gl, inum->no_addr);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001005 gfs2_trans_add_bh(gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001006 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
1007 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +00001008 di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001009
Steven Whitehouse2442a092006-01-30 11:49:32 +00001010 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
1011 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +00001012 di->di_mode = cpu_to_be32(mode);
1013 di->di_uid = cpu_to_be32(uid);
1014 di->di_gid = cpu_to_be32(gid);
1015 di->di_nlink = cpu_to_be32(0);
1016 di->di_size = cpu_to_be64(0);
1017 di->di_blocks = cpu_to_be64(1);
1018 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
1019 di->di_major = di->di_minor = cpu_to_be32(0);
1020 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
1021 di->__pad[0] = di->__pad[1] = 0;
1022 di->di_flags = cpu_to_be32(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001023
1024 if (S_ISREG(mode)) {
1025 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
1026 gfs2_tune_get(sdp, gt_new_files_jdata))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +00001027 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
1029 gfs2_tune_get(sdp, gt_new_files_directio))
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +00001030 di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001031 } else if (S_ISDIR(mode)) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001032 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
1033 GFS2_DIF_INHERIT_DIRECTIO);
1034 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
1035 GFS2_DIF_INHERIT_JDATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001036 }
1037
Steven Whitehouseb96ca4f2006-01-18 10:57:10 +00001038 di->__pad1 = 0;
1039 di->di_height = cpu_to_be32(0);
1040 di->__pad2 = 0;
1041 di->__pad3 = 0;
1042 di->di_depth = cpu_to_be16(0);
1043 di->di_entries = cpu_to_be32(0);
1044 memset(&di->__pad4, 0, sizeof(di->__pad4));
1045 di->di_eattr = cpu_to_be64(0);
1046 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
1047
David Teiglandb3b94fa2006-01-16 16:50:04 +00001048 brelse(dibh);
1049}
1050
1051static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
1052 unsigned int mode, struct gfs2_unlinked *ul)
1053{
1054 struct gfs2_sbd *sdp = dip->i_sbd;
1055 unsigned int uid, gid;
1056 int error;
1057
1058 munge_mode_uid_gid(dip, &mode, &uid, &gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059 gfs2_alloc_get(dip);
1060
1061 error = gfs2_quota_lock(dip, uid, gid);
1062 if (error)
1063 goto out;
1064
1065 error = gfs2_quota_check(dip, uid, gid);
1066 if (error)
1067 goto out_quota;
1068
Steven Whitehouse320dd102006-05-18 16:25:27 -04001069 error = gfs2_trans_begin(sdp, RES_DINODE + RES_UNLINKED + RES_QUOTA, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001070 if (error)
1071 goto out_quota;
1072
1073 ul->ul_ut.ut_flags = 0;
1074 error = gfs2_unlinked_ondisk_munge(sdp, ul);
Steven Whitehouse320dd102006-05-18 16:25:27 -04001075 init_dinode(dip, gl, &ul->ul_ut.ut_inum, mode, uid, gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001076 gfs2_quota_change(dip, +1, uid, gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001077 gfs2_trans_end(sdp);
1078
1079 out_quota:
1080 gfs2_quota_unlock(dip);
1081
1082 out:
1083 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084 return error;
1085}
1086
1087static int link_dinode(struct gfs2_inode *dip, struct qstr *name,
1088 struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1089{
1090 struct gfs2_sbd *sdp = dip->i_sbd;
1091 struct gfs2_alloc *al;
1092 int alloc_required;
1093 struct buffer_head *dibh;
1094 int error;
1095
1096 al = gfs2_alloc_get(dip);
1097
1098 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1099 if (error)
1100 goto fail;
1101
Steven Whitehousec7526662006-03-20 12:30:04 -05001102 error = alloc_required = gfs2_diradd_alloc_required(dip->i_vnode, name);
1103 if (alloc_required < 0)
1104 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001105 if (alloc_required) {
1106 error = gfs2_quota_check(dip, dip->i_di.di_uid,
1107 dip->i_di.di_gid);
1108 if (error)
1109 goto fail_quota_locks;
1110
1111 al->al_requested = sdp->sd_max_dirres;
1112
1113 error = gfs2_inplace_reserve(dip);
1114 if (error)
1115 goto fail_quota_locks;
1116
Steven Whitehouse320dd102006-05-18 16:25:27 -04001117 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118 al->al_rgd->rd_ri.ri_length +
1119 2 * RES_DINODE + RES_UNLINKED +
1120 RES_STATFS + RES_QUOTA, 0);
1121 if (error)
1122 goto fail_ipreserv;
1123 } else {
1124 error = gfs2_trans_begin(sdp,
1125 RES_LEAF +
1126 2 * RES_DINODE +
1127 RES_UNLINKED, 0);
1128 if (error)
1129 goto fail_quota_locks;
1130 }
1131
Steven Whitehousec7526662006-03-20 12:30:04 -05001132 error = gfs2_dir_add(dip->i_vnode, name, &ip->i_num, IF2DT(ip->i_di.di_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001133 if (error)
1134 goto fail_end_trans;
1135
1136 error = gfs2_meta_inode_buffer(ip, &dibh);
1137 if (error)
1138 goto fail_end_trans;
1139 ip->i_di.di_nlink = 1;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001140 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001141 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1142 brelse(dibh);
1143
1144 error = gfs2_unlinked_ondisk_rm(sdp, ul);
1145 if (error)
1146 goto fail_end_trans;
1147
1148 return 0;
1149
Steven Whitehouse320dd102006-05-18 16:25:27 -04001150fail_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001151 gfs2_trans_end(sdp);
1152
Steven Whitehouse320dd102006-05-18 16:25:27 -04001153fail_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001154 if (dip->i_alloc.al_rgd)
1155 gfs2_inplace_release(dip);
1156
Steven Whitehouse320dd102006-05-18 16:25:27 -04001157fail_quota_locks:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001158 gfs2_quota_unlock(dip);
1159
Steven Whitehouse320dd102006-05-18 16:25:27 -04001160fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001161 gfs2_alloc_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001162 return error;
1163}
1164
1165/**
1166 * gfs2_createi - Create a new inode
1167 * @ghs: An array of two holders
1168 * @name: The name of the new file
1169 * @mode: the permissions on the new inode
1170 *
1171 * @ghs[0] is an initialized holder for the directory
1172 * @ghs[1] is the holder for the inode lock
1173 *
Steven Whitehouse7359a192006-02-13 12:27:43 +00001174 * If the return value is not NULL, the glocks on both the directory and the new
David Teiglandb3b94fa2006-01-16 16:50:04 +00001175 * file are held. A transaction has been started and an inplace reservation
1176 * is held, as well.
1177 *
Steven Whitehouse7359a192006-02-13 12:27:43 +00001178 * Returns: An inode
David Teiglandb3b94fa2006-01-16 16:50:04 +00001179 */
1180
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001181struct inode *gfs2_createi(struct gfs2_holder *ghs, struct qstr *name,
1182 unsigned int mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001183{
Steven Whitehouse7359a192006-02-13 12:27:43 +00001184 struct inode *inode;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001185 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001186 struct gfs2_sbd *sdp = dip->i_sbd;
1187 struct gfs2_unlinked *ul;
1188 struct gfs2_inode *ip;
1189 int error;
1190
1191 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehouse7359a192006-02-13 12:27:43 +00001192 return ERR_PTR(-ENAMETOOLONG);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001193
1194 error = gfs2_unlinked_get(sdp, &ul);
1195 if (error)
Steven Whitehouse7359a192006-02-13 12:27:43 +00001196 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001197
1198 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
1199 error = gfs2_glock_nq(ghs);
1200 if (error)
1201 goto fail;
1202
1203 error = create_ok(dip, name, mode);
1204 if (error)
1205 goto fail_gunlock;
1206
1207 error = pick_formal_ino(sdp, &ul->ul_ut.ut_inum.no_formal_ino);
1208 if (error)
1209 goto fail_gunlock;
1210
1211 error = alloc_dinode(dip, ul);
1212 if (error)
1213 goto fail_gunlock;
1214
1215 if (ul->ul_ut.ut_inum.no_addr < dip->i_num.no_addr) {
1216 gfs2_glock_dq(ghs);
1217
Steven Whitehouse320dd102006-05-18 16:25:27 -04001218 error = gfs2_glock_nq_num(sdp, ul->ul_ut.ut_inum.no_addr,
1219 &gfs2_inode_glops, LM_ST_EXCLUSIVE,
1220 GL_SKIP, ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001221 if (error) {
1222 gfs2_unlinked_put(sdp, ul);
Steven Whitehouse7359a192006-02-13 12:27:43 +00001223 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001224 }
1225
1226 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
1227 error = gfs2_glock_nq(ghs);
1228 if (error) {
1229 gfs2_glock_dq_uninit(ghs + 1);
1230 gfs2_unlinked_put(sdp, ul);
Steven Whitehouse7359a192006-02-13 12:27:43 +00001231 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001232 }
1233
1234 error = create_ok(dip, name, mode);
1235 if (error)
1236 goto fail_gunlock2;
1237 } else {
Steven Whitehouse320dd102006-05-18 16:25:27 -04001238 error = gfs2_glock_nq_num(sdp, ul->ul_ut.ut_inum.no_addr,
1239 &gfs2_inode_glops, LM_ST_EXCLUSIVE,
1240 GL_SKIP, ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001241 if (error)
1242 goto fail_gunlock;
1243 }
1244
1245 error = make_dinode(dip, ghs[1].gh_gl, mode, ul);
1246 if (error)
1247 goto fail_gunlock2;
1248
1249 error = gfs2_inode_get(ghs[1].gh_gl, &ul->ul_ut.ut_inum, CREATE, &ip);
1250 if (error)
1251 goto fail_gunlock2;
1252
1253 error = gfs2_inode_refresh(ip);
1254 if (error)
1255 goto fail_iput;
1256
1257 error = gfs2_acl_create(dip, ip);
1258 if (error)
1259 goto fail_iput;
1260
1261 error = link_dinode(dip, name, ip, ul);
1262 if (error)
1263 goto fail_iput;
1264
1265 gfs2_unlinked_put(sdp, ul);
1266
Steven Whitehouse7359a192006-02-13 12:27:43 +00001267 inode = gfs2_ip2v(ip);
1268 gfs2_inode_put(ip);
1269 if (!inode)
1270 return ERR_PTR(-ENOMEM);
1271 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001272
Steven Whitehouse320dd102006-05-18 16:25:27 -04001273fail_iput:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001274 gfs2_inode_put(ip);
1275
Steven Whitehouse320dd102006-05-18 16:25:27 -04001276fail_gunlock2:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001277 gfs2_glock_dq_uninit(ghs + 1);
1278
Steven Whitehouse320dd102006-05-18 16:25:27 -04001279fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001280 gfs2_glock_dq(ghs);
1281
Steven Whitehouse320dd102006-05-18 16:25:27 -04001282fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001283 gfs2_unlinked_put(sdp, ul);
Steven Whitehouse7359a192006-02-13 12:27:43 +00001284 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001285}
1286
1287/**
1288 * gfs2_unlinki - Unlink a file
1289 * @dip: The inode of the directory
1290 * @name: The name of the file to be unlinked
1291 * @ip: The inode of the file to be removed
1292 *
1293 * Assumes Glocks on both dip and ip are held.
1294 *
1295 * Returns: errno
1296 */
1297
1298int gfs2_unlinki(struct gfs2_inode *dip, struct qstr *name,
1299 struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1300{
1301 struct gfs2_sbd *sdp = dip->i_sbd;
1302 int error;
1303
1304 error = gfs2_dir_del(dip, name);
1305 if (error)
1306 return error;
1307
1308 error = gfs2_change_nlink(ip, -1);
1309 if (error)
1310 return error;
1311
1312 /* If this inode is being unlinked from the directory structure,
1313 we need to mark that in the log so that it isn't lost during
1314 a crash. */
1315
1316 if (!ip->i_di.di_nlink) {
1317 ul->ul_ut.ut_inum = ip->i_num;
1318 error = gfs2_unlinked_ondisk_add(sdp, ul);
1319 if (!error)
1320 set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
1321 }
1322
1323 return error;
1324}
1325
1326/**
1327 * gfs2_rmdiri - Remove a directory
1328 * @dip: The parent directory of the directory to be removed
1329 * @name: The name of the directory to be removed
1330 * @ip: The GFS2 inode of the directory to be removed
1331 *
1332 * Assumes Glocks on dip and ip are held
1333 *
1334 * Returns: errno
1335 */
1336
1337int gfs2_rmdiri(struct gfs2_inode *dip, struct qstr *name,
1338 struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1339{
1340 struct gfs2_sbd *sdp = dip->i_sbd;
1341 struct qstr dotname;
1342 int error;
1343
1344 if (ip->i_di.di_entries != 2) {
1345 if (gfs2_consist_inode(ip))
1346 gfs2_dinode_print(&ip->i_di);
1347 return -EIO;
1348 }
1349
1350 error = gfs2_dir_del(dip, name);
1351 if (error)
1352 return error;
1353
1354 error = gfs2_change_nlink(dip, -1);
1355 if (error)
1356 return error;
1357
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001358 gfs2_str2qstr(&dotname, ".");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001359 error = gfs2_dir_del(ip, &dotname);
1360 if (error)
1361 return error;
1362
1363 dotname.len = 2;
1364 dotname.name = "..";
Steven Whitehousec7526662006-03-20 12:30:04 -05001365 dotname.hash = gfs2_disk_hash(dotname.name, dotname.len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001366 error = gfs2_dir_del(ip, &dotname);
1367 if (error)
1368 return error;
1369
1370 error = gfs2_change_nlink(ip, -2);
1371 if (error)
1372 return error;
1373
1374 /* This inode is being unlinked from the directory structure and
1375 we need to mark that in the log so that it isn't lost during
1376 a crash. */
1377
1378 ul->ul_ut.ut_inum = ip->i_num;
1379 error = gfs2_unlinked_ondisk_add(sdp, ul);
1380 if (!error)
1381 set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
1382
1383 return error;
1384}
1385
1386/*
1387 * gfs2_unlink_ok - check to see that a inode is still in a directory
1388 * @dip: the directory
1389 * @name: the name of the file
1390 * @ip: the inode
1391 *
1392 * Assumes that the lock on (at least) @dip is held.
1393 *
1394 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1395 */
1396
1397int gfs2_unlink_ok(struct gfs2_inode *dip, struct qstr *name,
1398 struct gfs2_inode *ip)
1399{
1400 struct gfs2_inum inum;
1401 unsigned int type;
1402 int error;
1403
1404 if (IS_IMMUTABLE(ip->i_vnode) || IS_APPEND(ip->i_vnode))
1405 return -EPERM;
1406
1407 if ((dip->i_di.di_mode & S_ISVTX) &&
1408 dip->i_di.di_uid != current->fsuid &&
1409 ip->i_di.di_uid != current->fsuid &&
1410 !capable(CAP_FOWNER))
1411 return -EPERM;
1412
1413 if (IS_APPEND(dip->i_vnode))
1414 return -EPERM;
1415
1416 error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL);
1417 if (error)
1418 return error;
1419
Steven Whitehousec7526662006-03-20 12:30:04 -05001420 error = gfs2_dir_search(dip->i_vnode, name, &inum, &type);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001421 if (error)
1422 return error;
1423
1424 if (!gfs2_inum_equal(&inum, &ip->i_num))
1425 return -ENOENT;
1426
1427 if (IF2DT(ip->i_di.di_mode) != type) {
1428 gfs2_consist_inode(dip);
1429 return -EIO;
1430 }
1431
1432 return 0;
1433}
1434
1435/*
1436 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1437 * @this: move this
1438 * @to: to here
1439 *
1440 * Follow @to back to the root and make sure we don't encounter @this
1441 * Assumes we already hold the rename lock.
1442 *
1443 * Returns: errno
1444 */
1445
1446int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1447{
Steven Whitehouse7359a192006-02-13 12:27:43 +00001448 struct inode *dir = to->i_vnode;
Steven Whitehousec9fd4302006-03-01 15:31:02 -05001449 struct super_block *sb = dir->i_sb;
Steven Whitehouse7359a192006-02-13 12:27:43 +00001450 struct inode *tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001451 struct qstr dotdot;
1452 int error = 0;
1453
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001454 gfs2_str2qstr(&dotdot, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001455
Steven Whitehouse7359a192006-02-13 12:27:43 +00001456 igrab(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001457
1458 for (;;) {
Steven Whitehouse7359a192006-02-13 12:27:43 +00001459 if (dir == this->i_vnode) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001460 error = -EINVAL;
1461 break;
1462 }
Steven Whitehousec9fd4302006-03-01 15:31:02 -05001463 if (dir == sb->s_root->d_inode) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001464 error = 0;
1465 break;
1466 }
1467
Steven Whitehousec7526662006-03-20 12:30:04 -05001468 tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
1469 if (IS_ERR(tmp)) {
1470 error = PTR_ERR(tmp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001471 break;
Steven Whitehousec7526662006-03-20 12:30:04 -05001472 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001473
Steven Whitehouse7359a192006-02-13 12:27:43 +00001474 iput(dir);
1475 dir = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001476 }
1477
Steven Whitehouse7359a192006-02-13 12:27:43 +00001478 iput(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001479
1480 return error;
1481}
1482
1483/**
1484 * gfs2_readlinki - return the contents of a symlink
1485 * @ip: the symlink's inode
1486 * @buf: a pointer to the buffer to be filled
1487 * @len: a pointer to the length of @buf
1488 *
1489 * If @buf is too small, a piece of memory is kmalloc()ed and needs
1490 * to be freed by the caller.
1491 *
1492 * Returns: errno
1493 */
1494
1495int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1496{
1497 struct gfs2_holder i_gh;
1498 struct buffer_head *dibh;
1499 unsigned int x;
1500 int error;
1501
1502 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1503 error = gfs2_glock_nq_atime(&i_gh);
1504 if (error) {
1505 gfs2_holder_uninit(&i_gh);
1506 return error;
1507 }
1508
1509 if (!ip->i_di.di_size) {
1510 gfs2_consist_inode(ip);
1511 error = -EIO;
1512 goto out;
1513 }
1514
1515 error = gfs2_meta_inode_buffer(ip, &dibh);
1516 if (error)
1517 goto out;
1518
1519 x = ip->i_di.di_size + 1;
1520 if (x > *len) {
1521 *buf = kmalloc(x, GFP_KERNEL);
1522 if (!*buf) {
1523 error = -ENOMEM;
1524 goto out_brelse;
1525 }
1526 }
1527
1528 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1529 *len = x;
1530
1531 out_brelse:
1532 brelse(dibh);
1533
1534 out:
1535 gfs2_glock_dq_uninit(&i_gh);
1536
1537 return error;
1538}
1539
1540/**
1541 * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
1542 * conditionally update the inode's atime
1543 * @gh: the holder to acquire
1544 *
1545 * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
1546 * Update if the difference between the current time and the inode's current
1547 * atime is greater than an interval specified at mount.
1548 *
1549 * Returns: errno
1550 */
1551
1552int gfs2_glock_nq_atime(struct gfs2_holder *gh)
1553{
1554 struct gfs2_glock *gl = gh->gh_gl;
1555 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001556 struct gfs2_inode *ip = gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001557 int64_t curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
1558 unsigned int state;
1559 int flags;
1560 int error;
1561
1562 if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1563 gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1564 gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1565 return -EINVAL;
1566
1567 state = gh->gh_state;
1568 flags = gh->gh_flags;
1569
1570 error = gfs2_glock_nq(gh);
1571 if (error)
1572 return error;
1573
1574 if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1575 (sdp->sd_vfs->s_flags & MS_RDONLY))
1576 return 0;
1577
1578 curtime = get_seconds();
1579 if (curtime - ip->i_di.di_atime >= quantum) {
1580 gfs2_glock_dq(gh);
Steven Whitehousefd88de562006-05-05 16:59:11 -04001581 gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
1582 gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001583 error = gfs2_glock_nq(gh);
1584 if (error)
1585 return error;
1586
1587 /* Verify that atime hasn't been updated while we were
1588 trying to get exclusive lock. */
1589
1590 curtime = get_seconds();
1591 if (curtime - ip->i_di.di_atime >= quantum) {
1592 struct buffer_head *dibh;
1593
1594 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1595 if (error == -EROFS)
1596 return 0;
1597 if (error)
1598 goto fail;
1599
1600 error = gfs2_meta_inode_buffer(ip, &dibh);
1601 if (error)
1602 goto fail_end_trans;
1603
1604 ip->i_di.di_atime = curtime;
1605
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001606 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001607 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1608 brelse(dibh);
1609
1610 gfs2_trans_end(sdp);
1611 }
1612
1613 /* If someone else has asked for the glock,
1614 unlock and let them have it. Then reacquire
1615 in the original state. */
1616 if (gfs2_glock_is_blocking(gl)) {
1617 gfs2_glock_dq(gh);
1618 gfs2_holder_reinit(state, flags, gh);
1619 return gfs2_glock_nq(gh);
1620 }
1621 }
1622
1623 return 0;
1624
1625 fail_end_trans:
1626 gfs2_trans_end(sdp);
1627
1628 fail:
1629 gfs2_glock_dq(gh);
1630
1631 return error;
1632}
1633
1634/**
1635 * glock_compare_atime - Compare two struct gfs2_glock structures for sort
1636 * @arg_a: the first structure
1637 * @arg_b: the second structure
1638 *
1639 * Returns: 1 if A > B
1640 * -1 if A < B
1641 * 0 if A = B
1642 */
1643
1644static int glock_compare_atime(const void *arg_a, const void *arg_b)
1645{
1646 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1647 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1648 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1649 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1650 int ret = 0;
1651
1652 if (a->ln_number > b->ln_number)
1653 ret = 1;
1654 else if (a->ln_number < b->ln_number)
1655 ret = -1;
1656 else {
1657 if (gh_a->gh_state == LM_ST_SHARED &&
1658 gh_b->gh_state == LM_ST_EXCLUSIVE)
1659 ret = 1;
1660 else if (gh_a->gh_state == LM_ST_SHARED &&
1661 (gh_b->gh_flags & GL_ATIME))
1662 ret = 1;
1663 }
1664
1665 return ret;
1666}
1667
1668/**
1669 * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an
1670 * atime update
1671 * @num_gh: the number of structures
1672 * @ghs: an array of struct gfs2_holder structures
1673 *
1674 * Returns: 0 on success (all glocks acquired),
1675 * errno on failure (no glocks acquired)
1676 */
1677
1678int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs)
1679{
1680 struct gfs2_holder **p;
1681 unsigned int x;
1682 int error = 0;
1683
1684 if (!num_gh)
1685 return 0;
1686
1687 if (num_gh == 1) {
1688 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1689 if (ghs->gh_flags & GL_ATIME)
1690 error = gfs2_glock_nq_atime(ghs);
1691 else
1692 error = gfs2_glock_nq(ghs);
1693 return error;
1694 }
1695
1696 p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1697 if (!p)
1698 return -ENOMEM;
1699
1700 for (x = 0; x < num_gh; x++)
1701 p[x] = &ghs[x];
1702
1703 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL);
1704
1705 for (x = 0; x < num_gh; x++) {
1706 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1707
1708 if (p[x]->gh_flags & GL_ATIME)
1709 error = gfs2_glock_nq_atime(p[x]);
1710 else
1711 error = gfs2_glock_nq(p[x]);
1712
1713 if (error) {
1714 while (x--)
1715 gfs2_glock_dq(p[x]);
1716 break;
1717 }
1718 }
1719
1720 kfree(p);
1721
1722 return error;
1723}
1724
1725/**
1726 * gfs2_try_toss_vnode - See if we can toss a vnode from memory
1727 * @ip: the inode
1728 *
1729 * Returns: 1 if the vnode was tossed
1730 */
1731
1732void gfs2_try_toss_vnode(struct gfs2_inode *ip)
1733{
1734 struct inode *inode;
1735
1736 inode = gfs2_ip2v_lookup(ip);
1737 if (!inode)
1738 return;
1739
1740 d_prune_aliases(inode);
1741
1742 if (S_ISDIR(ip->i_di.di_mode)) {
1743 struct list_head *head = &inode->i_dentry;
1744 struct dentry *d = NULL;
1745
1746 spin_lock(&dcache_lock);
1747 if (list_empty(head))
1748 spin_unlock(&dcache_lock);
1749 else {
1750 d = list_entry(head->next, struct dentry, d_alias);
1751 dget_locked(d);
1752 spin_unlock(&dcache_lock);
1753
1754 if (have_submounts(d))
1755 dput(d);
1756 else {
1757 shrink_dcache_parent(d);
1758 dput(d);
1759 d_prune_aliases(inode);
1760 }
1761 }
1762 }
1763
1764 inode->i_nlink = 0;
1765 iput(inode);
1766}
1767
1768
1769static int
1770__gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1771{
1772 struct buffer_head *dibh;
1773 int error;
1774
1775 error = gfs2_meta_inode_buffer(ip, &dibh);
1776 if (!error) {
1777 error = inode_setattr(ip->i_vnode, attr);
1778 gfs2_assert_warn(ip->i_sbd, !error);
1779 gfs2_inode_attr_out(ip);
1780
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001781 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001782 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1783 brelse(dibh);
1784 }
1785 return error;
1786}
1787
1788/**
1789 * gfs2_setattr_simple -
1790 * @ip:
1791 * @attr:
1792 *
1793 * Called with a reference on the vnode.
1794 *
1795 * Returns: errno
1796 */
1797
1798int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1799{
1800 int error;
1801
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001802 if (current->journal_info)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001803 return __gfs2_setattr_simple(ip, attr);
1804
1805 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
1806 if (error)
1807 return error;
1808
1809 error = __gfs2_setattr_simple(ip, attr);
1810
1811 gfs2_trans_end(ip->i_sbd);
1812
1813 return error;
1814}
1815
1816int gfs2_repermission(struct inode *inode, int mask, struct nameidata *nd)
1817{
1818 return permission(inode, mask, nd);
1819}
1820