blob: 1e24b65e1d23d50048064767100662189bcaf6ee [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
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
David Teiglandb3b94fa2006-01-16 16:50:04 +000010#include <linux/slab.h>
11#include <linux/spinlock.h>
12#include <linux/completion.h>
13#include <linux/buffer_head.h>
14#include <linux/namei.h>
15#include <linux/utsname.h>
16#include <linux/mm.h>
17#include <linux/xattr.h>
18#include <linux/posix_acl.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050020#include <linux/crc32.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020021#include <linux/lm_interface.h>
Steven Whitehousee9079cc2008-10-14 14:43:29 +010022#include <linux/fiemap.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000023#include <asm/uaccess.h>
24
25#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050026#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000027#include "acl.h"
28#include "bmap.h"
29#include "dir.h"
30#include "eaops.h"
31#include "eattr.h"
32#include "glock.h"
33#include "inode.h"
34#include "meta_io.h"
35#include "ops_dentry.h"
36#include "ops_inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000037#include "quota.h"
38#include "rgrp.h"
39#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050040#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000041
42/**
43 * gfs2_create - Create a file
44 * @dir: The directory in which to create the file
45 * @dentry: The dentry of the new file
46 * @mode: The mode of the new file
47 *
48 * Returns: errno
49 */
50
51static int gfs2_create(struct inode *dir, struct dentry *dentry,
52 int mode, struct nameidata *nd)
53{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040054 struct gfs2_inode *dip = GFS2_I(dir);
55 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +000056 struct gfs2_holder ghs[2];
57 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +000058
David Teiglandb3b94fa2006-01-16 16:50:04 +000059 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
60
61 for (;;) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -050062 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +000063 if (!IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000064 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +000065 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +000066 gfs2_inplace_release(dip);
67 gfs2_quota_unlock(dip);
68 gfs2_alloc_put(dip);
69 gfs2_glock_dq_uninit_m(2, ghs);
Steven Whitehouse3a8476d2006-06-19 09:10:39 -040070 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000071 break;
Steven Whitehouse7359a192006-02-13 12:27:43 +000072 } else if (PTR_ERR(inode) != -EEXIST ||
Al Viro35165862008-08-05 03:00:49 -040073 (nd && nd->flags & LOOKUP_EXCL)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000074 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +000075 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000076 }
77
Al Viroa569c712008-07-23 14:42:05 -040078 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
Steven Whitehousec7526662006-03-20 12:30:04 -050079 if (inode) {
80 if (!IS_ERR(inode)) {
Steven Whitehousec7526662006-03-20 12:30:04 -050081 gfs2_holder_uninit(ghs);
82 break;
83 } else {
84 gfs2_holder_uninit(ghs);
85 return PTR_ERR(inode);
86 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000087 }
88 }
89
David Teiglandb3b94fa2006-01-16 16:50:04 +000090 d_instantiate(dentry, inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000091
92 return 0;
93}
94
95/**
96 * gfs2_lookup - Look up a filename in a directory and return its inode
97 * @dir: The directory inode
98 * @dentry: The dentry of the new inode
99 * @nd: passed from Linux VFS, ignored by us
100 *
101 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
102 *
103 * Returns: errno
104 */
105
106static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
107 struct nameidata *nd)
108{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109 struct inode *inode = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110
Steven Whitehousec7526662006-03-20 12:30:04 -0500111 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000112
Al Viroa569c712008-07-23 14:42:05 -0400113 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
Steven Whitehousec7526662006-03-20 12:30:04 -0500114 if (inode && IS_ERR(inode))
David Howellse231c2e2008-02-07 00:15:26 -0800115 return ERR_CAST(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000116
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000117 if (inode) {
118 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
119 struct gfs2_holder gh;
120 int error;
121 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
122 if (error) {
123 iput(inode);
124 return ERR_PTR(error);
125 }
126 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127 return d_splice_alias(inode, dentry);
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000128 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000129 d_add(dentry, inode);
130
131 return NULL;
132}
133
134/**
135 * gfs2_link - Link to a file
136 * @old_dentry: The inode to link
137 * @dir: Add link to this directory
138 * @dentry: The name of the link
139 *
140 * Link the inode in "old_dentry" into the directory "dir" with the
141 * name in "dentry".
142 *
143 * Returns: errno
144 */
145
146static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
147 struct dentry *dentry)
148{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400149 struct gfs2_inode *dip = GFS2_I(dir);
150 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000151 struct inode *inode = old_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400152 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000153 struct gfs2_holder ghs[2];
154 int alloc_required;
155 int error;
156
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500157 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158 return -EPERM;
159
160 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
161 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
162
Bob Peterson72dbf472008-08-12 13:39:29 -0500163 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500165 goto out_parent;
166
167 error = gfs2_glock_nq(ghs + 1); /* child */
168 if (error)
169 goto out_child;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000170
Miklos Szeredif58ba882008-07-02 21:12:01 +0200171 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 if (error)
173 goto out_gunlock;
174
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100175 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176 switch (error) {
177 case -ENOENT:
178 break;
179 case 0:
180 error = -EEXIST;
181 default:
182 goto out_gunlock;
183 }
184
185 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500186 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000187 goto out_gunlock;
188 error = -EFBIG;
Steven Whitehousecd915492006-09-04 12:49:07 -0400189 if (dip->i_di.di_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190 goto out_gunlock;
191 error = -EPERM;
192 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
193 goto out_gunlock;
194 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500195 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000196 goto out_gunlock;
197 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500198 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000199 goto out_gunlock;
200
Steven Whitehousec7526662006-03-20 12:30:04 -0500201 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
202 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500204 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000205
206 if (alloc_required) {
207 struct gfs2_alloc *al = gfs2_alloc_get(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300208 if (!al) {
209 error = -ENOMEM;
210 goto out_gunlock;
211 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212
Steven Whitehoused82661d2008-03-10 15:34:50 +0000213 error = gfs2_quota_lock_check(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000214 if (error)
215 goto out_alloc;
216
David Teiglandb3b94fa2006-01-16 16:50:04 +0000217 al->al_requested = sdp->sd_max_dirres;
218
219 error = gfs2_inplace_reserve(dip);
220 if (error)
221 goto out_gunlock_q;
222
Steven Whitehouse1b502592006-05-18 14:10:52 -0400223 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100224 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000225 2 * RES_DINODE + RES_STATFS +
226 RES_QUOTA, 0);
227 if (error)
228 goto out_ipres;
229 } else {
230 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
231 if (error)
232 goto out_ipres;
233 }
234
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100235 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000236 if (error)
237 goto out_end_trans;
238
239 error = gfs2_change_nlink(ip, +1);
240
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400241out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400243out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244 if (alloc_required)
245 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400246out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000247 if (alloc_required)
248 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400249out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250 if (alloc_required)
251 gfs2_alloc_put(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400252out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500253 gfs2_glock_dq(ghs + 1);
254out_child:
255 gfs2_glock_dq(ghs);
256out_parent:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000257 gfs2_holder_uninit(ghs);
258 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000259 if (!error) {
Steven Whitehouse29937ac2006-07-06 17:58:03 -0400260 atomic_inc(&inode->i_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261 d_instantiate(dentry, inode);
262 mark_inode_dirty(inode);
263 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000264 return error;
265}
266
267/**
268 * gfs2_unlink - Unlink a file
269 * @dir: The inode of the directory containing the file to unlink
270 * @dentry: The file itself
271 *
272 * Unlink a file. Call gfs2_unlinki()
273 *
274 * Returns: errno
275 */
276
277static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
278{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400279 struct gfs2_inode *dip = GFS2_I(dir);
280 struct gfs2_sbd *sdp = GFS2_SB(dir);
281 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600282 struct gfs2_holder ghs[3];
283 struct gfs2_rgrpd *rgd;
284 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000285 int error;
286
Russell Cattelanddee7602007-01-29 17:13:44 -0600287 error = gfs2_rindex_hold(sdp, &ri_gh);
288 if (error)
289 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000290
Russell Cattelanddee7602007-01-29 17:13:44 -0600291 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
292 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
293
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100294 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600295 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
296
297
Steven Whitehouse8497a462007-08-26 14:23:56 +0100298 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000299 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100300 goto out_parent;
301
302 error = gfs2_glock_nq(ghs + 1); /* child */
303 if (error)
304 goto out_child;
305
306 error = gfs2_glock_nq(ghs + 2); /* rgrp */
307 if (error)
308 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000309
310 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
311 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500312 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000313
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400314 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000315 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100316 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000317
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400318 error = gfs2_dir_del(dip, &dentry->d_name);
319 if (error)
320 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000321
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400322 error = gfs2_change_nlink(ip, -1);
323
324out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000325 gfs2_trans_end(sdp);
Bob Peterson72dbf472008-08-12 13:39:29 -0500326out_gunlock:
Steven Whitehouse8497a462007-08-26 14:23:56 +0100327 gfs2_glock_dq(ghs + 2);
328out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600329 gfs2_holder_uninit(ghs + 2);
Steven Whitehouse8497a462007-08-26 14:23:56 +0100330 gfs2_glock_dq(ghs + 1);
331out_child:
332 gfs2_holder_uninit(ghs + 1);
333 gfs2_glock_dq(ghs);
334out_parent:
335 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600336 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337 return error;
338}
339
340/**
341 * gfs2_symlink - Create a symlink
342 * @dir: The directory to create the symlink in
343 * @dentry: The dentry to put the symlink in
344 * @symname: The thing which the link points to
345 *
346 * Returns: errno
347 */
348
349static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
350 const char *symname)
351{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400352 struct gfs2_inode *dip = GFS2_I(dir), *ip;
353 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354 struct gfs2_holder ghs[2];
355 struct inode *inode;
356 struct buffer_head *dibh;
357 int size;
358 int error;
359
David Teiglandb3b94fa2006-01-16 16:50:04 +0000360 /* Must be stuffed with a null terminator for gfs2_follow_link() */
361 size = strlen(symname);
362 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
363 return -ENAMETOOLONG;
364
365 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
366
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500367 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000368 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000370 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000371 }
372
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500373 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000374
375 ip->i_di.di_size = size;
376
377 error = gfs2_meta_inode_buffer(ip, &dibh);
378
379 if (!gfs2_assert_withdraw(sdp, !error)) {
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500380 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000381 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
382 size);
383 brelse(dibh);
384 }
385
386 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000387 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000388 gfs2_inplace_release(dip);
389 gfs2_quota_unlock(dip);
390 gfs2_alloc_put(dip);
391
392 gfs2_glock_dq_uninit_m(2, ghs);
393
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394 d_instantiate(dentry, inode);
395 mark_inode_dirty(inode);
396
397 return 0;
398}
399
400/**
401 * gfs2_mkdir - Make a directory
402 * @dir: The parent directory of the new one
403 * @dentry: The dentry of the new directory
404 * @mode: The mode of the new directory
405 *
406 * Returns: errno
407 */
408
409static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
410{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400411 struct gfs2_inode *dip = GFS2_I(dir), *ip;
412 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000413 struct gfs2_holder ghs[2];
414 struct inode *inode;
415 struct buffer_head *dibh;
416 int error;
417
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
419
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500420 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000421 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000423 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424 }
425
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500426 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427
Steven Whitehouse4f561102006-11-01 14:04:17 -0500428 ip->i_inode.i_nlink = 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000429 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
430 ip->i_di.di_flags |= GFS2_DIF_JDATA;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000431 ip->i_di.di_entries = 2;
432
433 error = gfs2_meta_inode_buffer(ip, &dibh);
434
435 if (!gfs2_assert_withdraw(sdp, !error)) {
436 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -0500437 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500438 struct qstr str;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000439
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500440 gfs2_str2qstr(&str, ".");
Steven Whitehousec7526662006-03-20 12:30:04 -0500441 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
442 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443 dent->de_inum = di->di_num; /* already GFS2 endian */
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400444 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445 di->di_entries = cpu_to_be32(1);
446
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500447 gfs2_str2qstr(&str, "..");
Steven Whitehousec7526662006-03-20 12:30:04 -0500448 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
449 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000450
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100451 gfs2_inum_out(dip, dent);
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400452 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000453
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500454 gfs2_dinode_out(ip, di);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455
456 brelse(dibh);
457 }
458
459 error = gfs2_change_nlink(dip, +1);
460 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
461
462 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000463 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000464 gfs2_inplace_release(dip);
465 gfs2_quota_unlock(dip);
466 gfs2_alloc_put(dip);
467
468 gfs2_glock_dq_uninit_m(2, ghs);
469
David Teiglandb3b94fa2006-01-16 16:50:04 +0000470 d_instantiate(dentry, inode);
471 mark_inode_dirty(inode);
472
473 return 0;
474}
475
476/**
477 * gfs2_rmdir - Remove a directory
478 * @dir: The parent directory of the directory to be removed
479 * @dentry: The dentry of the directory to remove
480 *
481 * Remove a directory. Call gfs2_rmdiri()
482 *
483 * Returns: errno
484 */
485
486static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
487{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400488 struct gfs2_inode *dip = GFS2_I(dir);
489 struct gfs2_sbd *sdp = GFS2_SB(dir);
490 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600491 struct gfs2_holder ghs[3];
492 struct gfs2_rgrpd *rgd;
493 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000494 int error;
495
Russell Cattelanddee7602007-01-29 17:13:44 -0600496 error = gfs2_rindex_hold(sdp, &ri_gh);
497 if (error)
498 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
500 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
501
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100502 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600503 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
504
Bob Peterson72dbf472008-08-12 13:39:29 -0500505 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000506 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500507 goto out_parent;
508
509 error = gfs2_glock_nq(ghs + 1); /* child */
510 if (error)
511 goto out_child;
512
513 error = gfs2_glock_nq(ghs + 2); /* rgrp */
514 if (error)
515 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516
517 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
518 if (error)
519 goto out_gunlock;
520
521 if (ip->i_di.di_entries < 2) {
522 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500523 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000524 error = -EIO;
525 goto out_gunlock;
526 }
527 if (ip->i_di.di_entries > 2) {
528 error = -ENOTEMPTY;
529 goto out_gunlock;
530 }
531
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400532 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000533 if (error)
534 goto out_gunlock;
535
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400536 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000537
538 gfs2_trans_end(sdp);
539
Steven Whitehousea91ea692006-09-04 12:04:26 -0400540out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500541 gfs2_glock_dq(ghs + 2);
542out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600543 gfs2_holder_uninit(ghs + 2);
Bob Peterson72dbf472008-08-12 13:39:29 -0500544 gfs2_glock_dq(ghs + 1);
545out_child:
546 gfs2_holder_uninit(ghs + 1);
547 gfs2_glock_dq(ghs);
548out_parent:
549 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600550 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551 return error;
552}
553
554/**
555 * gfs2_mknod - Make a special file
556 * @dir: The directory in which the special file will reside
557 * @dentry: The dentry of the special file
558 * @mode: The mode of the special file
559 * @rdev: The device specification of the special file
560 *
561 */
562
563static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
564 dev_t dev)
565{
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500566 struct gfs2_inode *dip = GFS2_I(dir);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400567 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568 struct gfs2_holder ghs[2];
569 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000570
571 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
572
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500573 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000574 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000576 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577 }
578
David Teiglandb3b94fa2006-01-16 16:50:04 +0000579 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000580 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000581 gfs2_inplace_release(dip);
582 gfs2_quota_unlock(dip);
583 gfs2_alloc_put(dip);
584
585 gfs2_glock_dq_uninit_m(2, ghs);
586
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587 d_instantiate(dentry, inode);
588 mark_inode_dirty(inode);
589
590 return 0;
591}
592
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100593/*
594 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
595 * @this: move this
596 * @to: to here
597 *
598 * Follow @to back to the root and make sure we don't encounter @this
599 * Assumes we already hold the rename lock.
600 *
601 * Returns: errno
602 */
603
604static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
605{
606 struct inode *dir = &to->i_inode;
607 struct super_block *sb = dir->i_sb;
608 struct inode *tmp;
609 struct qstr dotdot;
610 int error = 0;
611
612 gfs2_str2qstr(&dotdot, "..");
613
614 igrab(dir);
615
616 for (;;) {
617 if (dir == &this->i_inode) {
618 error = -EINVAL;
619 break;
620 }
621 if (dir == sb->s_root->d_inode) {
622 error = 0;
623 break;
624 }
625
626 tmp = gfs2_lookupi(dir, &dotdot, 1);
627 if (IS_ERR(tmp)) {
628 error = PTR_ERR(tmp);
629 break;
630 }
631
632 iput(dir);
633 dir = tmp;
634 }
635
636 iput(dir);
637
638 return error;
639}
640
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641/**
642 * gfs2_rename - Rename a file
643 * @odir: Parent directory of old file name
644 * @odentry: The old dentry of the file
645 * @ndir: Parent directory of new file name
646 * @ndentry: The new dentry of the file
647 *
648 * Returns: errno
649 */
650
651static int gfs2_rename(struct inode *odir, struct dentry *odentry,
652 struct inode *ndir, struct dentry *ndentry)
653{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400654 struct gfs2_inode *odip = GFS2_I(odir);
655 struct gfs2_inode *ndip = GFS2_I(ndir);
656 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000657 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400658 struct gfs2_sbd *sdp = GFS2_SB(odir);
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100659 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
Russell Cattelanddee7602007-01-29 17:13:44 -0600660 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000661 unsigned int num_gh;
662 int dir_rename = 0;
663 int alloc_required;
664 unsigned int x;
665 int error;
666
David Teiglandb3b94fa2006-01-16 16:50:04 +0000667 if (ndentry->d_inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400668 nip = GFS2_I(ndentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669 if (ip == nip)
670 return 0;
671 }
672
David Teiglandb3b94fa2006-01-16 16:50:04 +0000673
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100674 if (odip != ndip) {
675 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
676 0, &r_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677 if (error)
678 goto out;
679
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100680 if (S_ISDIR(ip->i_inode.i_mode)) {
681 dir_rename = 1;
682 /* don't move a dirctory into it's subdir */
683 error = gfs2_ok_to_move(ip, ndip);
684 if (error)
685 goto out_gunlock_r;
686 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687 }
688
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400689 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000690 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400691 if (odip != ndip) {
692 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
693 num_gh++;
694 }
695 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
696 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400698 if (nip) {
699 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
700 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -0600701 /* grab the resource lock for unlink flag twiddling
702 * this is the case of the target file already existing
703 * so we unlink before doing the rename
704 */
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100705 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600706 if (nrgd)
707 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400708 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000709
Bob Peterson72dbf472008-08-12 13:39:29 -0500710 for (x = 0; x < num_gh; x++) {
711 error = gfs2_glock_nq(ghs + x);
712 if (error)
713 goto out_gunlock;
714 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715
716 /* Check out the old directory */
717
718 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
719 if (error)
720 goto out_gunlock;
721
722 /* Check out the new directory */
723
724 if (nip) {
725 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
726 if (error)
727 goto out_gunlock;
728
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500729 if (S_ISDIR(nip->i_inode.i_mode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000730 if (nip->i_di.di_entries < 2) {
731 if (gfs2_consist_inode(nip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500732 gfs2_dinode_print(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000733 error = -EIO;
734 goto out_gunlock;
735 }
736 if (nip->i_di.di_entries > 2) {
737 error = -ENOTEMPTY;
738 goto out_gunlock;
739 }
740 }
741 } else {
Miklos Szeredif58ba882008-07-02 21:12:01 +0200742 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 if (error)
744 goto out_gunlock;
745
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100746 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000747 switch (error) {
748 case -ENOENT:
749 error = 0;
750 break;
751 case 0:
752 error = -EEXIST;
753 default:
754 goto out_gunlock;
755 };
756
757 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -0500758 if (!ndip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759 error = -EINVAL;
760 goto out_gunlock;
761 }
Steven Whitehousecd915492006-09-04 12:49:07 -0400762 if (ndip->i_di.di_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000763 error = -EFBIG;
764 goto out_gunlock;
765 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500766 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -0500767 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000768 error = -EMLINK;
769 goto out_gunlock;
770 }
771 }
772 }
773
774 /* Check out the dir to be renamed */
775
776 if (dir_rename) {
Miklos Szeredif58ba882008-07-02 21:12:01 +0200777 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000778 if (error)
779 goto out_gunlock;
780 }
781
Steven Whitehousec7526662006-03-20 12:30:04 -0500782 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
783 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000784 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500785 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786
787 if (alloc_required) {
788 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300789 if (!al) {
790 error = -ENOMEM;
791 goto out_gunlock;
792 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793
Steven Whitehoused82661d2008-03-10 15:34:50 +0000794 error = gfs2_quota_lock_check(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795 if (error)
796 goto out_alloc;
797
David Teiglandb3b94fa2006-01-16 16:50:04 +0000798 al->al_requested = sdp->sd_max_dirres;
799
800 error = gfs2_inplace_reserve(ndip);
801 if (error)
802 goto out_gunlock_q;
803
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400804 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100805 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000806 4 * RES_DINODE + 4 * RES_LEAF +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500807 RES_STATFS + RES_QUOTA + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808 if (error)
809 goto out_ipreserv;
810 } else {
811 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500812 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000813 if (error)
814 goto out_gunlock;
815 }
816
817 /* Remove the target file, if it exists */
818
819 if (nip) {
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500820 if (S_ISDIR(nip->i_inode.i_mode))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400821 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
822 else {
823 error = gfs2_dir_del(ndip, &ndentry->d_name);
824 if (error)
825 goto out_end_trans;
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500826 error = gfs2_change_nlink(nip, -1);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400827 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828 if (error)
829 goto out_end_trans;
830 }
831
832 if (dir_rename) {
833 struct qstr name;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500834 gfs2_str2qstr(&name, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835
836 error = gfs2_change_nlink(ndip, +1);
837 if (error)
838 goto out_end_trans;
839 error = gfs2_change_nlink(odip, -1);
840 if (error)
841 goto out_end_trans;
842
Steven Whitehouseffed8ab2007-06-07 11:29:35 +0100843 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844 if (error)
845 goto out_end_trans;
846 } else {
847 struct buffer_head *dibh;
848 error = gfs2_meta_inode_buffer(ip, &dibh);
849 if (error)
850 goto out_end_trans;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100851 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000852 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500853 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854 brelse(dibh);
855 }
856
857 error = gfs2_dir_del(odip, &odentry->d_name);
858 if (error)
859 goto out_end_trans;
860
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100861 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862 if (error)
863 goto out_end_trans;
864
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400865out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000866 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400867out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000868 if (alloc_required)
869 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400870out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871 if (alloc_required)
872 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400873out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000874 if (alloc_required)
875 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400876out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500877 while (x--) {
878 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -0500880 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400881out_gunlock_r:
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100882 if (r_gh.gh_gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000883 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400884out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000885 return error;
886}
887
888/**
889 * gfs2_readlink - Read the value of a symlink
890 * @dentry: the symlink
891 * @buf: the buffer to read the symlink data into
892 * @size: the size of the buffer
893 *
894 * Returns: errno
895 */
896
897static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
898 int user_size)
899{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400900 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000901 char array[GFS2_FAST_NAME_SIZE], *buf = array;
902 unsigned int len = GFS2_FAST_NAME_SIZE;
903 int error;
904
David Teiglandb3b94fa2006-01-16 16:50:04 +0000905 error = gfs2_readlinki(ip, &buf, &len);
906 if (error)
907 return error;
908
909 if (user_size > len - 1)
910 user_size = len - 1;
911
912 if (copy_to_user(user_buf, buf, user_size))
913 error = -EFAULT;
914 else
915 error = user_size;
916
917 if (buf != array)
918 kfree(buf);
919
920 return error;
921}
922
923/**
924 * gfs2_follow_link - Follow a symbolic link
925 * @dentry: The dentry of the link
926 * @nd: Data that we pass to vfs_follow_link()
927 *
928 * This can handle symlinks of any size. It is optimised for symlinks
929 * under GFS2_FAST_NAME_SIZE.
930 *
931 * Returns: 0 on success or error code
932 */
933
934static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
935{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400936 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000937 char array[GFS2_FAST_NAME_SIZE], *buf = array;
938 unsigned int len = GFS2_FAST_NAME_SIZE;
939 int error;
940
David Teiglandb3b94fa2006-01-16 16:50:04 +0000941 error = gfs2_readlinki(ip, &buf, &len);
942 if (!error) {
943 error = vfs_follow_link(nd, buf);
944 if (buf != array)
945 kfree(buf);
946 }
947
948 return ERR_PTR(error);
949}
950
951/**
952 * gfs2_permission -
953 * @inode:
954 * @mask:
955 * @nd: passed from Linux VFS, ignored by us
956 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500957 * This may be called from the VFS directly, or from within GFS2 with the
958 * inode locked, so we look to see if the glock is already locked and only
959 * lock the glock if its not already been done.
960 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000961 * Returns: errno
962 */
963
Miklos Szeredif58ba882008-07-02 21:12:01 +0200964int gfs2_permission(struct inode *inode, int mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400966 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967 struct gfs2_holder i_gh;
968 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500969 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000970
Steven Whitehouse7afd88d2008-02-22 16:07:18 +0000971 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500972 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
973 if (error)
974 return error;
975 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000976 }
977
Miklos Szeredif58ba882008-07-02 21:12:01 +0200978 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
979 error = -EACCES;
980 else
981 error = generic_permission(inode, mask, gfs2_check_acl);
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500982 if (unlock)
983 gfs2_glock_dq_uninit(&i_gh);
984
David Teiglandb3b94fa2006-01-16 16:50:04 +0000985 return error;
986}
987
988static int setattr_size(struct inode *inode, struct iattr *attr)
989{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400990 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100991 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000992 int error;
993
994 if (attr->ia_size != ip->i_di.di_size) {
Steven Whitehouse16615be2007-09-17 10:59:52 +0100995 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996 if (error)
997 return error;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100998 error = vmtruncate(inode, attr->ia_size);
999 gfs2_trans_end(sdp);
1000 if (error)
1001 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001002 }
1003
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001004 error = gfs2_truncatei(ip, attr->ia_size);
Wendy Cheng090ffaa2007-06-27 11:00:03 -04001005 if (error && (inode->i_size != ip->i_di.di_size))
1006 i_size_write(inode, ip->i_di.di_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001007
1008 return error;
1009}
1010
1011static int setattr_chown(struct inode *inode, struct iattr *attr)
1012{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001013 struct gfs2_inode *ip = GFS2_I(inode);
1014 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001015 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001016 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017 int error;
1018
Steven Whitehouse2933f922006-11-01 13:23:29 -05001019 ouid = inode->i_uid;
1020 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001021 nuid = attr->ia_uid;
1022 ngid = attr->ia_gid;
1023
1024 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1025 ouid = nuid = NO_QUOTA_CHANGE;
1026 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1027 ogid = ngid = NO_QUOTA_CHANGE;
1028
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001029 if (!gfs2_alloc_get(ip))
1030 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001031
1032 error = gfs2_quota_lock(ip, nuid, ngid);
1033 if (error)
1034 goto out_alloc;
1035
1036 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1037 error = gfs2_quota_check(ip, nuid, ngid);
1038 if (error)
1039 goto out_gunlock_q;
1040 }
1041
1042 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1043 if (error)
1044 goto out_gunlock_q;
1045
1046 error = gfs2_meta_inode_buffer(ip, &dibh);
1047 if (error)
1048 goto out_end_trans;
1049
1050 error = inode_setattr(inode, attr);
1051 gfs2_assert_warn(sdp, !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001052
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001053 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001054 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001055 brelse(dibh);
1056
1057 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001058 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1059 gfs2_quota_change(ip, -blocks, ouid, ogid);
1060 gfs2_quota_change(ip, blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061 }
1062
Steven Whitehousea91ea692006-09-04 12:04:26 -04001063out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001064 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001065out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001066 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001067out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001068 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001069 return error;
1070}
1071
1072/**
1073 * gfs2_setattr - Change attributes on an inode
1074 * @dentry: The dentry which is changing
1075 * @attr: The structure describing the change
1076 *
1077 * The VFS layer wants to change one or more of an inodes attributes. Write
1078 * that change out to disk.
1079 *
1080 * Returns: errno
1081 */
1082
1083static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1084{
1085 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001086 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001087 struct gfs2_holder i_gh;
1088 int error;
1089
David Teiglandb3b94fa2006-01-16 16:50:04 +00001090 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1091 if (error)
1092 return error;
1093
1094 error = -EPERM;
1095 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1096 goto out;
1097
1098 error = inode_change_ok(inode, attr);
1099 if (error)
1100 goto out;
1101
1102 if (attr->ia_valid & ATTR_SIZE)
1103 error = setattr_size(inode, attr);
1104 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1105 error = setattr_chown(inode, attr);
1106 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1107 error = gfs2_acl_chmod(ip, attr);
1108 else
1109 error = gfs2_setattr_simple(ip, attr);
1110
Steven Whitehousea91ea692006-09-04 12:04:26 -04001111out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001112 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001113 if (!error)
1114 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115 return error;
1116}
1117
1118/**
1119 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001120 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001121 * @dentry: The dentry to stat
1122 * @stat: The inode's stats
1123 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001124 * This may be called from the VFS directly, or from within GFS2 with the
1125 * inode locked, so we look to see if the glock is already locked and only
1126 * lock the glock if its not already been done. Note that its the NFS
1127 * readdirplus operation which causes this to be called (from filldir)
1128 * with the glock already held.
1129 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001130 * Returns: errno
1131 */
1132
1133static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1134 struct kstat *stat)
1135{
1136 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001137 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001138 struct gfs2_holder gh;
1139 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001140 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001141
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001142 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001143 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1144 if (error)
1145 return error;
1146 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147 }
1148
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001149 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001150 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001151 gfs2_glock_dq_uninit(&gh);
1152
1153 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001154}
1155
1156static int gfs2_setxattr(struct dentry *dentry, const char *name,
1157 const void *data, size_t size, int flags)
1158{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001159 struct inode *inode = dentry->d_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001160 struct gfs2_ea_request er;
1161
David Teiglandb3b94fa2006-01-16 16:50:04 +00001162 memset(&er, 0, sizeof(struct gfs2_ea_request));
1163 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1164 if (er.er_type == GFS2_EATYPE_UNUSED)
1165 return -EOPNOTSUPP;
1166 er.er_data = (char *)data;
1167 er.er_name_len = strlen(er.er_name);
1168 er.er_data_len = size;
1169 er.er_flags = flags;
1170
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001171 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001172
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001173 return gfs2_ea_set(GFS2_I(inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001174}
1175
1176static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1177 void *data, size_t size)
1178{
1179 struct gfs2_ea_request er;
1180
David Teiglandb3b94fa2006-01-16 16:50:04 +00001181 memset(&er, 0, sizeof(struct gfs2_ea_request));
1182 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1183 if (er.er_type == GFS2_EATYPE_UNUSED)
1184 return -EOPNOTSUPP;
1185 er.er_data = data;
1186 er.er_name_len = strlen(er.er_name);
1187 er.er_data_len = size;
1188
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001189 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001190}
1191
1192static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1193{
1194 struct gfs2_ea_request er;
1195
David Teiglandb3b94fa2006-01-16 16:50:04 +00001196 memset(&er, 0, sizeof(struct gfs2_ea_request));
1197 er.er_data = (size) ? buffer : NULL;
1198 er.er_data_len = size;
1199
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001200 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001201}
1202
1203static int gfs2_removexattr(struct dentry *dentry, const char *name)
1204{
1205 struct gfs2_ea_request er;
1206
David Teiglandb3b94fa2006-01-16 16:50:04 +00001207 memset(&er, 0, sizeof(struct gfs2_ea_request));
1208 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1209 if (er.er_type == GFS2_EATYPE_UNUSED)
1210 return -EOPNOTSUPP;
1211 er.er_name_len = strlen(er.er_name);
1212
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001213 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001214}
1215
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001216static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1217 u64 start, u64 len)
1218{
1219 struct gfs2_inode *ip = GFS2_I(inode);
1220 struct gfs2_holder gh;
1221 int ret;
1222
1223 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1224 if (ret)
1225 return ret;
1226
1227 mutex_lock(&inode->i_mutex);
1228
1229 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1230 if (ret)
1231 goto out;
1232
1233 if (gfs2_is_stuffed(ip)) {
1234 u64 phys = ip->i_no_addr << inode->i_blkbits;
1235 u64 size = i_size_read(inode);
1236 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1237 FIEMAP_EXTENT_DATA_INLINE;
1238 phys += sizeof(struct gfs2_dinode);
1239 phys += start;
1240 if (start + len > size)
1241 len = size - start;
1242 if (start < size)
1243 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1244 len, flags);
1245 if (ret == 1)
1246 ret = 0;
1247 } else {
1248 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1249 gfs2_block_map);
1250 }
1251
1252 gfs2_glock_dq_uninit(&gh);
1253out:
1254 mutex_unlock(&inode->i_mutex);
1255 return ret;
1256}
1257
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001258const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04001259 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001260 .setattr = gfs2_setattr,
1261 .getattr = gfs2_getattr,
1262 .setxattr = gfs2_setxattr,
1263 .getxattr = gfs2_getxattr,
1264 .listxattr = gfs2_listxattr,
1265 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001266 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001267};
1268
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001269const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001270 .create = gfs2_create,
1271 .lookup = gfs2_lookup,
1272 .link = gfs2_link,
1273 .unlink = gfs2_unlink,
1274 .symlink = gfs2_symlink,
1275 .mkdir = gfs2_mkdir,
1276 .rmdir = gfs2_rmdir,
1277 .mknod = gfs2_mknod,
1278 .rename = gfs2_rename,
Al Viroe6305c42008-07-15 21:03:57 -04001279 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001280 .setattr = gfs2_setattr,
1281 .getattr = gfs2_getattr,
1282 .setxattr = gfs2_setxattr,
1283 .getxattr = gfs2_getxattr,
1284 .listxattr = gfs2_listxattr,
1285 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001286 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001287};
1288
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001289const struct inode_operations gfs2_symlink_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001290 .readlink = gfs2_readlink,
1291 .follow_link = gfs2_follow_link,
Al Viroe6305c42008-07-15 21:03:57 -04001292 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001293 .setattr = gfs2_setattr,
1294 .getattr = gfs2_getattr,
1295 .setxattr = gfs2_setxattr,
1296 .getxattr = gfs2_getxattr,
1297 .listxattr = gfs2_listxattr,
1298 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001299 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001300};
1301