blob: 1c70fa5168d6aa46ddf872f326a2a1399bee981a [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>
Steven Whitehousee9079cc2008-10-14 14:43:29 +010021#include <linux/fiemap.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include <asm/uaccess.h>
23
24#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050025#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000026#include "acl.h"
27#include "bmap.h"
28#include "dir.h"
29#include "eaops.h"
30#include "eattr.h"
31#include "glock.h"
32#include "inode.h"
33#include "meta_io.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000034#include "quota.h"
35#include "rgrp.h"
36#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050037#include "util.h"
Steven Whitehouseb2760582008-10-14 16:05:55 +010038#include "super.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000039
40/**
41 * gfs2_create - Create a file
42 * @dir: The directory in which to create the file
43 * @dentry: The dentry of the new file
44 * @mode: The mode of the new file
45 *
46 * Returns: errno
47 */
48
49static int gfs2_create(struct inode *dir, struct dentry *dentry,
50 int mode, struct nameidata *nd)
51{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040052 struct gfs2_inode *dip = GFS2_I(dir);
53 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +000054 struct gfs2_holder ghs[2];
55 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +000056
David Teiglandb3b94fa2006-01-16 16:50:04 +000057 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
58
59 for (;;) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -050060 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +000061 if (!IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000062 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +000063 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +000064 gfs2_inplace_release(dip);
65 gfs2_quota_unlock(dip);
66 gfs2_alloc_put(dip);
67 gfs2_glock_dq_uninit_m(2, ghs);
Steven Whitehouse3a8476d2006-06-19 09:10:39 -040068 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000069 break;
Steven Whitehouse7359a192006-02-13 12:27:43 +000070 } else if (PTR_ERR(inode) != -EEXIST ||
Al Viro35165862008-08-05 03:00:49 -040071 (nd && nd->flags & LOOKUP_EXCL)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000072 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +000073 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000074 }
75
Al Viroa569c712008-07-23 14:42:05 -040076 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
Steven Whitehousec7526662006-03-20 12:30:04 -050077 if (inode) {
78 if (!IS_ERR(inode)) {
Steven Whitehousec7526662006-03-20 12:30:04 -050079 gfs2_holder_uninit(ghs);
80 break;
81 } else {
82 gfs2_holder_uninit(ghs);
83 return PTR_ERR(inode);
84 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000085 }
86 }
87
David Teiglandb3b94fa2006-01-16 16:50:04 +000088 d_instantiate(dentry, inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000089
90 return 0;
91}
92
93/**
94 * gfs2_lookup - Look up a filename in a directory and return its inode
95 * @dir: The directory inode
96 * @dentry: The dentry of the new inode
97 * @nd: passed from Linux VFS, ignored by us
98 *
99 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
100 *
101 * Returns: errno
102 */
103
104static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
105 struct nameidata *nd)
106{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000107 struct inode *inode = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108
Steven Whitehousec7526662006-03-20 12:30:04 -0500109 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110
Al Viroa569c712008-07-23 14:42:05 -0400111 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
Steven Whitehousec7526662006-03-20 12:30:04 -0500112 if (inode && IS_ERR(inode))
David Howellse231c2e2008-02-07 00:15:26 -0800113 return ERR_CAST(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000114
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000115 if (inode) {
116 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
117 struct gfs2_holder gh;
118 int error;
119 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
120 if (error) {
121 iput(inode);
122 return ERR_PTR(error);
123 }
124 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000125 return d_splice_alias(inode, dentry);
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000126 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127 d_add(dentry, inode);
128
129 return NULL;
130}
131
132/**
133 * gfs2_link - Link to a file
134 * @old_dentry: The inode to link
135 * @dir: Add link to this directory
136 * @dentry: The name of the link
137 *
138 * Link the inode in "old_dentry" into the directory "dir" with the
139 * name in "dentry".
140 *
141 * Returns: errno
142 */
143
144static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
145 struct dentry *dentry)
146{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400147 struct gfs2_inode *dip = GFS2_I(dir);
148 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000149 struct inode *inode = old_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400150 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000151 struct gfs2_holder ghs[2];
152 int alloc_required;
153 int error;
154
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500155 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000156 return -EPERM;
157
158 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
159 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
160
Bob Peterson72dbf472008-08-12 13:39:29 -0500161 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500163 goto out_parent;
164
165 error = gfs2_glock_nq(ghs + 1); /* child */
166 if (error)
167 goto out_child;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000168
Miklos Szeredif58ba882008-07-02 21:12:01 +0200169 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000170 if (error)
171 goto out_gunlock;
172
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100173 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000174 switch (error) {
175 case -ENOENT:
176 break;
177 case 0:
178 error = -EEXIST;
179 default:
180 goto out_gunlock;
181 }
182
183 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500184 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185 goto out_gunlock;
186 error = -EFBIG;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000187 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000188 goto out_gunlock;
189 error = -EPERM;
190 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
191 goto out_gunlock;
192 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500193 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000194 goto out_gunlock;
195 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500196 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000197 goto out_gunlock;
198
Steven Whitehousec7526662006-03-20 12:30:04 -0500199 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
200 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000201 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500202 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203
204 if (alloc_required) {
205 struct gfs2_alloc *al = gfs2_alloc_get(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300206 if (!al) {
207 error = -ENOMEM;
208 goto out_gunlock;
209 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000210
Steven Whitehoused82661d2008-03-10 15:34:50 +0000211 error = gfs2_quota_lock_check(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212 if (error)
213 goto out_alloc;
214
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215 al->al_requested = sdp->sd_max_dirres;
216
217 error = gfs2_inplace_reserve(dip);
218 if (error)
219 goto out_gunlock_q;
220
Steven Whitehouse1b502592006-05-18 14:10:52 -0400221 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100222 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000223 2 * RES_DINODE + RES_STATFS +
224 RES_QUOTA, 0);
225 if (error)
226 goto out_ipres;
227 } else {
228 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
229 if (error)
230 goto out_ipres;
231 }
232
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100233 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000234 if (error)
235 goto out_end_trans;
236
237 error = gfs2_change_nlink(ip, +1);
238
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400239out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400241out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242 if (alloc_required)
243 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400244out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245 if (alloc_required)
246 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400247out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000248 if (alloc_required)
249 gfs2_alloc_put(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400250out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500251 gfs2_glock_dq(ghs + 1);
252out_child:
253 gfs2_glock_dq(ghs);
254out_parent:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000255 gfs2_holder_uninit(ghs);
256 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000257 if (!error) {
Steven Whitehouse29937ac2006-07-06 17:58:03 -0400258 atomic_inc(&inode->i_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000259 d_instantiate(dentry, inode);
260 mark_inode_dirty(inode);
261 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000262 return error;
263}
264
265/**
266 * gfs2_unlink - Unlink a file
267 * @dir: The inode of the directory containing the file to unlink
268 * @dentry: The file itself
269 *
270 * Unlink a file. Call gfs2_unlinki()
271 *
272 * Returns: errno
273 */
274
275static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
276{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400277 struct gfs2_inode *dip = GFS2_I(dir);
278 struct gfs2_sbd *sdp = GFS2_SB(dir);
279 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600280 struct gfs2_holder ghs[3];
281 struct gfs2_rgrpd *rgd;
282 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000283 int error;
284
Russell Cattelanddee7602007-01-29 17:13:44 -0600285 error = gfs2_rindex_hold(sdp, &ri_gh);
286 if (error)
287 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288
Russell Cattelanddee7602007-01-29 17:13:44 -0600289 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
290 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
291
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100292 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600293 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
294
295
Steven Whitehouse8497a462007-08-26 14:23:56 +0100296 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000297 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100298 goto out_parent;
299
300 error = gfs2_glock_nq(ghs + 1); /* child */
301 if (error)
302 goto out_child;
303
304 error = gfs2_glock_nq(ghs + 2); /* rgrp */
305 if (error)
306 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000307
308 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
309 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500310 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000311
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400312 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000313 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +0100314 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000315
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400316 error = gfs2_dir_del(dip, &dentry->d_name);
317 if (error)
318 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000319
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400320 error = gfs2_change_nlink(ip, -1);
321
322out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323 gfs2_trans_end(sdp);
Bob Peterson72dbf472008-08-12 13:39:29 -0500324out_gunlock:
Steven Whitehouse8497a462007-08-26 14:23:56 +0100325 gfs2_glock_dq(ghs + 2);
326out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600327 gfs2_holder_uninit(ghs + 2);
Steven Whitehouse8497a462007-08-26 14:23:56 +0100328 gfs2_glock_dq(ghs + 1);
329out_child:
330 gfs2_holder_uninit(ghs + 1);
331 gfs2_glock_dq(ghs);
332out_parent:
333 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600334 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335 return error;
336}
337
338/**
339 * gfs2_symlink - Create a symlink
340 * @dir: The directory to create the symlink in
341 * @dentry: The dentry to put the symlink in
342 * @symname: The thing which the link points to
343 *
344 * Returns: errno
345 */
346
347static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
348 const char *symname)
349{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400350 struct gfs2_inode *dip = GFS2_I(dir), *ip;
351 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352 struct gfs2_holder ghs[2];
353 struct inode *inode;
354 struct buffer_head *dibh;
355 int size;
356 int error;
357
David Teiglandb3b94fa2006-01-16 16:50:04 +0000358 /* Must be stuffed with a null terminator for gfs2_follow_link() */
359 size = strlen(symname);
360 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
361 return -ENAMETOOLONG;
362
363 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
364
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500365 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000366 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000368 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369 }
370
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500371 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372
Steven Whitehousec9e98882008-11-04 09:47:33 +0000373 ip->i_disksize = size;
Steven Whitehouse5cf32522009-03-31 16:06:27 +0100374 i_size_write(inode, size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000375
376 error = gfs2_meta_inode_buffer(ip, &dibh);
377
378 if (!gfs2_assert_withdraw(sdp, !error)) {
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500379 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000380 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
381 size);
382 brelse(dibh);
383 }
384
385 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000386 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387 gfs2_inplace_release(dip);
388 gfs2_quota_unlock(dip);
389 gfs2_alloc_put(dip);
390
391 gfs2_glock_dq_uninit_m(2, ghs);
392
David Teiglandb3b94fa2006-01-16 16:50:04 +0000393 d_instantiate(dentry, inode);
394 mark_inode_dirty(inode);
395
396 return 0;
397}
398
399/**
400 * gfs2_mkdir - Make a directory
401 * @dir: The parent directory of the new one
402 * @dentry: The dentry of the new directory
403 * @mode: The mode of the new directory
404 *
405 * Returns: errno
406 */
407
408static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
409{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400410 struct gfs2_inode *dip = GFS2_I(dir), *ip;
411 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412 struct gfs2_holder ghs[2];
413 struct inode *inode;
414 struct buffer_head *dibh;
415 int error;
416
David Teiglandb3b94fa2006-01-16 16:50:04 +0000417 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
418
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500419 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000420 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000422 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423 }
424
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500425 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000426
Steven Whitehouse4f561102006-11-01 14:04:17 -0500427 ip->i_inode.i_nlink = 2;
Steven Whitehousec9e98882008-11-04 09:47:33 +0000428 ip->i_disksize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000429 ip->i_diskflags |= GFS2_DIF_JDATA;
Steven Whitehousead6203f2008-11-03 13:59:19 +0000430 ip->i_entries = 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000431
432 error = gfs2_meta_inode_buffer(ip, &dibh);
433
434 if (!gfs2_assert_withdraw(sdp, !error)) {
435 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -0500436 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500437 struct qstr str;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000438
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500439 gfs2_str2qstr(&str, ".");
Steven Whitehousec7526662006-03-20 12:30:04 -0500440 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
441 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442 dent->de_inum = di->di_num; /* already GFS2 endian */
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400443 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000444 di->di_entries = cpu_to_be32(1);
445
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500446 gfs2_str2qstr(&str, "..");
Steven Whitehousec7526662006-03-20 12:30:04 -0500447 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
448 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000449
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100450 gfs2_inum_out(dip, dent);
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400451 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000452
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500453 gfs2_dinode_out(ip, di);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454
455 brelse(dibh);
456 }
457
458 error = gfs2_change_nlink(dip, +1);
459 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
460
461 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000462 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000463 gfs2_inplace_release(dip);
464 gfs2_quota_unlock(dip);
465 gfs2_alloc_put(dip);
466
467 gfs2_glock_dq_uninit_m(2, ghs);
468
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469 d_instantiate(dentry, inode);
470 mark_inode_dirty(inode);
471
472 return 0;
473}
474
475/**
476 * gfs2_rmdir - Remove a directory
477 * @dir: The parent directory of the directory to be removed
478 * @dentry: The dentry of the directory to remove
479 *
480 * Remove a directory. Call gfs2_rmdiri()
481 *
482 * Returns: errno
483 */
484
485static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
486{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400487 struct gfs2_inode *dip = GFS2_I(dir);
488 struct gfs2_sbd *sdp = GFS2_SB(dir);
489 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600490 struct gfs2_holder ghs[3];
491 struct gfs2_rgrpd *rgd;
492 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000493 int error;
494
Russell Cattelanddee7602007-01-29 17:13:44 -0600495 error = gfs2_rindex_hold(sdp, &ri_gh);
496 if (error)
497 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000498 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
499 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
500
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100501 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600502 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
503
Bob Peterson72dbf472008-08-12 13:39:29 -0500504 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500506 goto out_parent;
507
508 error = gfs2_glock_nq(ghs + 1); /* child */
509 if (error)
510 goto out_child;
511
512 error = gfs2_glock_nq(ghs + 2); /* rgrp */
513 if (error)
514 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000515
516 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
517 if (error)
518 goto out_gunlock;
519
Steven Whitehousead6203f2008-11-03 13:59:19 +0000520 if (ip->i_entries < 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000521 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500522 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000523 error = -EIO;
524 goto out_gunlock;
525 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000526 if (ip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527 error = -ENOTEMPTY;
528 goto out_gunlock;
529 }
530
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400531 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532 if (error)
533 goto out_gunlock;
534
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400535 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536
537 gfs2_trans_end(sdp);
538
Steven Whitehousea91ea692006-09-04 12:04:26 -0400539out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500540 gfs2_glock_dq(ghs + 2);
541out_rgrp:
Russell Cattelanddee7602007-01-29 17:13:44 -0600542 gfs2_holder_uninit(ghs + 2);
Bob Peterson72dbf472008-08-12 13:39:29 -0500543 gfs2_glock_dq(ghs + 1);
544out_child:
545 gfs2_holder_uninit(ghs + 1);
546 gfs2_glock_dq(ghs);
547out_parent:
548 gfs2_holder_uninit(ghs);
Russell Cattelanddee7602007-01-29 17:13:44 -0600549 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000550 return error;
551}
552
553/**
554 * gfs2_mknod - Make a special file
555 * @dir: The directory in which the special file will reside
556 * @dentry: The dentry of the special file
557 * @mode: The mode of the special file
558 * @rdev: The device specification of the special file
559 *
560 */
561
562static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
563 dev_t dev)
564{
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500565 struct gfs2_inode *dip = GFS2_I(dir);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400566 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000567 struct gfs2_holder ghs[2];
568 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000569
570 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
571
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500572 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000573 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000575 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576 }
577
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578 gfs2_trans_end(sdp);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000579 if (dip->i_alloc->al_rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 gfs2_inplace_release(dip);
581 gfs2_quota_unlock(dip);
582 gfs2_alloc_put(dip);
583
584 gfs2_glock_dq_uninit_m(2, ghs);
585
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586 d_instantiate(dentry, inode);
587 mark_inode_dirty(inode);
588
589 return 0;
590}
591
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100592/*
593 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
594 * @this: move this
595 * @to: to here
596 *
597 * Follow @to back to the root and make sure we don't encounter @this
598 * Assumes we already hold the rename lock.
599 *
600 * Returns: errno
601 */
602
603static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
604{
605 struct inode *dir = &to->i_inode;
606 struct super_block *sb = dir->i_sb;
607 struct inode *tmp;
608 struct qstr dotdot;
609 int error = 0;
610
611 gfs2_str2qstr(&dotdot, "..");
612
613 igrab(dir);
614
615 for (;;) {
616 if (dir == &this->i_inode) {
617 error = -EINVAL;
618 break;
619 }
620 if (dir == sb->s_root->d_inode) {
621 error = 0;
622 break;
623 }
624
625 tmp = gfs2_lookupi(dir, &dotdot, 1);
626 if (IS_ERR(tmp)) {
627 error = PTR_ERR(tmp);
628 break;
629 }
630
631 iput(dir);
632 dir = tmp;
633 }
634
635 iput(dir);
636
637 return error;
638}
639
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640/**
641 * gfs2_rename - Rename a file
642 * @odir: Parent directory of old file name
643 * @odentry: The old dentry of the file
644 * @ndir: Parent directory of new file name
645 * @ndentry: The new dentry of the file
646 *
647 * Returns: errno
648 */
649
650static int gfs2_rename(struct inode *odir, struct dentry *odentry,
651 struct inode *ndir, struct dentry *ndentry)
652{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400653 struct gfs2_inode *odip = GFS2_I(odir);
654 struct gfs2_inode *ndip = GFS2_I(ndir);
655 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400657 struct gfs2_sbd *sdp = GFS2_SB(odir);
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100658 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
Russell Cattelanddee7602007-01-29 17:13:44 -0600659 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660 unsigned int num_gh;
661 int dir_rename = 0;
662 int alloc_required;
663 unsigned int x;
664 int error;
665
David Teiglandb3b94fa2006-01-16 16:50:04 +0000666 if (ndentry->d_inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400667 nip = GFS2_I(ndentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000668 if (ip == nip)
669 return 0;
670 }
671
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100673 if (odip != ndip) {
674 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
675 0, &r_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000676 if (error)
677 goto out;
678
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100679 if (S_ISDIR(ip->i_inode.i_mode)) {
680 dir_rename = 1;
681 /* don't move a dirctory into it's subdir */
682 error = gfs2_ok_to_move(ip, ndip);
683 if (error)
684 goto out_gunlock_r;
685 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000686 }
687
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400688 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400690 if (odip != ndip) {
691 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
692 num_gh++;
693 }
694 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
695 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000696
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400697 if (nip) {
698 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
699 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -0600700 /* grab the resource lock for unlink flag twiddling
701 * this is the case of the target file already existing
702 * so we unlink before doing the rename
703 */
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100704 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
Russell Cattelanddee7602007-01-29 17:13:44 -0600705 if (nrgd)
706 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400707 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708
Bob Peterson72dbf472008-08-12 13:39:29 -0500709 for (x = 0; x < num_gh; x++) {
710 error = gfs2_glock_nq(ghs + x);
711 if (error)
712 goto out_gunlock;
713 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000714
715 /* Check out the old directory */
716
717 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
718 if (error)
719 goto out_gunlock;
720
721 /* Check out the new directory */
722
723 if (nip) {
724 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
725 if (error)
726 goto out_gunlock;
727
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500728 if (S_ISDIR(nip->i_inode.i_mode)) {
Steven Whitehousead6203f2008-11-03 13:59:19 +0000729 if (nip->i_entries < 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000730 if (gfs2_consist_inode(nip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500731 gfs2_dinode_print(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732 error = -EIO;
733 goto out_gunlock;
734 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000735 if (nip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000736 error = -ENOTEMPTY;
737 goto out_gunlock;
738 }
739 }
740 } else {
Miklos Szeredif58ba882008-07-02 21:12:01 +0200741 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000742 if (error)
743 goto out_gunlock;
744
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100745 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746 switch (error) {
747 case -ENOENT:
748 error = 0;
749 break;
750 case 0:
751 error = -EEXIST;
752 default:
753 goto out_gunlock;
754 };
755
756 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -0500757 if (!ndip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000758 error = -EINVAL;
759 goto out_gunlock;
760 }
Steven Whitehousead6203f2008-11-03 13:59:19 +0000761 if (ndip->i_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000762 error = -EFBIG;
763 goto out_gunlock;
764 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500765 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -0500766 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 error = -EMLINK;
768 goto out_gunlock;
769 }
770 }
771 }
772
773 /* Check out the dir to be renamed */
774
775 if (dir_rename) {
Miklos Szeredif58ba882008-07-02 21:12:01 +0200776 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777 if (error)
778 goto out_gunlock;
779 }
780
Steven Whitehousec7526662006-03-20 12:30:04 -0500781 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
782 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500784 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785
786 if (alloc_required) {
787 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300788 if (!al) {
789 error = -ENOMEM;
790 goto out_gunlock;
791 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000792
Steven Whitehoused82661d2008-03-10 15:34:50 +0000793 error = gfs2_quota_lock_check(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000794 if (error)
795 goto out_alloc;
796
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797 al->al_requested = sdp->sd_max_dirres;
798
799 error = gfs2_inplace_reserve(ndip);
800 if (error)
801 goto out_gunlock_q;
802
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400803 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100804 al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000805 4 * RES_DINODE + 4 * RES_LEAF +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500806 RES_STATFS + RES_QUOTA + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000807 if (error)
808 goto out_ipreserv;
809 } else {
810 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500811 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812 if (error)
813 goto out_gunlock;
814 }
815
816 /* Remove the target file, if it exists */
817
818 if (nip) {
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500819 if (S_ISDIR(nip->i_inode.i_mode))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400820 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
821 else {
822 error = gfs2_dir_del(ndip, &ndentry->d_name);
823 if (error)
824 goto out_end_trans;
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500825 error = gfs2_change_nlink(nip, -1);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400826 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000827 if (error)
828 goto out_end_trans;
829 }
830
831 if (dir_rename) {
832 struct qstr name;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500833 gfs2_str2qstr(&name, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834
835 error = gfs2_change_nlink(ndip, +1);
836 if (error)
837 goto out_end_trans;
838 error = gfs2_change_nlink(odip, -1);
839 if (error)
840 goto out_end_trans;
841
Steven Whitehouseffed8ab2007-06-07 11:29:35 +0100842 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843 if (error)
844 goto out_end_trans;
845 } else {
846 struct buffer_head *dibh;
847 error = gfs2_meta_inode_buffer(ip, &dibh);
848 if (error)
849 goto out_end_trans;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100850 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000851 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500852 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853 brelse(dibh);
854 }
855
856 error = gfs2_dir_del(odip, &odentry->d_name);
857 if (error)
858 goto out_end_trans;
859
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100860 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861 if (error)
862 goto out_end_trans;
863
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400864out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000865 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400866out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867 if (alloc_required)
868 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400869out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000870 if (alloc_required)
871 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400872out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000873 if (alloc_required)
874 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400875out_gunlock:
Bob Peterson72dbf472008-08-12 13:39:29 -0500876 while (x--) {
877 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000878 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -0500879 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400880out_gunlock_r:
Steven Whitehouse0188d6c2008-08-26 09:38:26 +0100881 if (r_gh.gh_gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400883out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000884 return error;
885}
886
887/**
888 * gfs2_readlink - Read the value of a symlink
889 * @dentry: the symlink
890 * @buf: the buffer to read the symlink data into
891 * @size: the size of the buffer
892 *
893 * Returns: errno
894 */
895
896static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
897 int user_size)
898{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400899 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900 char array[GFS2_FAST_NAME_SIZE], *buf = array;
901 unsigned int len = GFS2_FAST_NAME_SIZE;
902 int error;
903
David Teiglandb3b94fa2006-01-16 16:50:04 +0000904 error = gfs2_readlinki(ip, &buf, &len);
905 if (error)
906 return error;
907
908 if (user_size > len - 1)
909 user_size = len - 1;
910
911 if (copy_to_user(user_buf, buf, user_size))
912 error = -EFAULT;
913 else
914 error = user_size;
915
916 if (buf != array)
917 kfree(buf);
918
919 return error;
920}
921
922/**
923 * gfs2_follow_link - Follow a symbolic link
924 * @dentry: The dentry of the link
925 * @nd: Data that we pass to vfs_follow_link()
926 *
927 * This can handle symlinks of any size. It is optimised for symlinks
928 * under GFS2_FAST_NAME_SIZE.
929 *
930 * Returns: 0 on success or error code
931 */
932
933static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
934{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400935 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000936 char array[GFS2_FAST_NAME_SIZE], *buf = array;
937 unsigned int len = GFS2_FAST_NAME_SIZE;
938 int error;
939
David Teiglandb3b94fa2006-01-16 16:50:04 +0000940 error = gfs2_readlinki(ip, &buf, &len);
941 if (!error) {
942 error = vfs_follow_link(nd, buf);
943 if (buf != array)
944 kfree(buf);
945 }
946
947 return ERR_PTR(error);
948}
949
950/**
951 * gfs2_permission -
952 * @inode:
953 * @mask:
954 * @nd: passed from Linux VFS, ignored by us
955 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500956 * This may be called from the VFS directly, or from within GFS2 with the
957 * inode locked, so we look to see if the glock is already locked and only
958 * lock the glock if its not already been done.
959 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000960 * Returns: errno
961 */
962
Miklos Szeredif58ba882008-07-02 21:12:01 +0200963int gfs2_permission(struct inode *inode, int mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000964{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400965 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966 struct gfs2_holder i_gh;
967 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500968 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000969
Steven Whitehouse7afd88d2008-02-22 16:07:18 +0000970 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500971 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
972 if (error)
973 return error;
974 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000975 }
976
Miklos Szeredif58ba882008-07-02 21:12:01 +0200977 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
978 error = -EACCES;
979 else
980 error = generic_permission(inode, mask, gfs2_check_acl);
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500981 if (unlock)
982 gfs2_glock_dq_uninit(&i_gh);
983
David Teiglandb3b94fa2006-01-16 16:50:04 +0000984 return error;
985}
986
987static int setattr_size(struct inode *inode, struct iattr *attr)
988{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400989 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse16615be2007-09-17 10:59:52 +0100990 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000991 int error;
992
Steven Whitehousec9e98882008-11-04 09:47:33 +0000993 if (attr->ia_size != ip->i_disksize) {
Steven Whitehouse16615be2007-09-17 10:59:52 +0100994 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000995 if (error)
996 return error;
Steven Whitehouse16615be2007-09-17 10:59:52 +0100997 error = vmtruncate(inode, attr->ia_size);
998 gfs2_trans_end(sdp);
999 if (error)
1000 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001 }
1002
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +00001003 error = gfs2_truncatei(ip, attr->ia_size);
Steven Whitehousec9e98882008-11-04 09:47:33 +00001004 if (error && (inode->i_size != ip->i_disksize))
1005 i_size_write(inode, ip->i_disksize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001006
1007 return error;
1008}
1009
1010static int setattr_chown(struct inode *inode, struct iattr *attr)
1011{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001012 struct gfs2_inode *ip = GFS2_I(inode);
1013 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001014 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001015 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016 int error;
1017
Steven Whitehouse2933f922006-11-01 13:23:29 -05001018 ouid = inode->i_uid;
1019 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001020 nuid = attr->ia_uid;
1021 ngid = attr->ia_gid;
1022
1023 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1024 ouid = nuid = NO_QUOTA_CHANGE;
1025 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1026 ogid = ngid = NO_QUOTA_CHANGE;
1027
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001028 if (!gfs2_alloc_get(ip))
1029 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001030
1031 error = gfs2_quota_lock(ip, nuid, ngid);
1032 if (error)
1033 goto out_alloc;
1034
1035 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1036 error = gfs2_quota_check(ip, nuid, ngid);
1037 if (error)
1038 goto out_gunlock_q;
1039 }
1040
1041 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1042 if (error)
1043 goto out_gunlock_q;
1044
1045 error = gfs2_meta_inode_buffer(ip, &dibh);
1046 if (error)
1047 goto out_end_trans;
1048
1049 error = inode_setattr(inode, attr);
1050 gfs2_assert_warn(sdp, !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001051
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001052 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001053 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001054 brelse(dibh);
1055
1056 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001057 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1058 gfs2_quota_change(ip, -blocks, ouid, ogid);
1059 gfs2_quota_change(ip, blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001060 }
1061
Steven Whitehousea91ea692006-09-04 12:04:26 -04001062out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001064out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001065 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001066out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001067 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001068 return error;
1069}
1070
1071/**
1072 * gfs2_setattr - Change attributes on an inode
1073 * @dentry: The dentry which is changing
1074 * @attr: The structure describing the change
1075 *
1076 * The VFS layer wants to change one or more of an inodes attributes. Write
1077 * that change out to disk.
1078 *
1079 * Returns: errno
1080 */
1081
1082static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1083{
1084 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001085 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001086 struct gfs2_holder i_gh;
1087 int error;
1088
David Teiglandb3b94fa2006-01-16 16:50:04 +00001089 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1090 if (error)
1091 return error;
1092
1093 error = -EPERM;
1094 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1095 goto out;
1096
1097 error = inode_change_ok(inode, attr);
1098 if (error)
1099 goto out;
1100
1101 if (attr->ia_valid & ATTR_SIZE)
1102 error = setattr_size(inode, attr);
1103 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1104 error = setattr_chown(inode, attr);
1105 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1106 error = gfs2_acl_chmod(ip, attr);
1107 else
1108 error = gfs2_setattr_simple(ip, attr);
1109
Steven Whitehousea91ea692006-09-04 12:04:26 -04001110out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001112 if (!error)
1113 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001114 return error;
1115}
1116
1117/**
1118 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001119 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001120 * @dentry: The dentry to stat
1121 * @stat: The inode's stats
1122 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001123 * This may be called from the VFS directly, or from within GFS2 with the
1124 * inode locked, so we look to see if the glock is already locked and only
1125 * lock the glock if its not already been done. Note that its the NFS
1126 * readdirplus operation which causes this to be called (from filldir)
1127 * with the glock already held.
1128 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001129 * Returns: errno
1130 */
1131
1132static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1133 struct kstat *stat)
1134{
1135 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001136 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001137 struct gfs2_holder gh;
1138 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001139 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001140
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001141 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001142 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1143 if (error)
1144 return error;
1145 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001146 }
1147
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001148 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001149 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001150 gfs2_glock_dq_uninit(&gh);
1151
1152 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001153}
1154
1155static int gfs2_setxattr(struct dentry *dentry, const char *name,
1156 const void *data, size_t size, int flags)
1157{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001158 struct inode *inode = dentry->d_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001159 struct gfs2_ea_request er;
1160
David Teiglandb3b94fa2006-01-16 16:50:04 +00001161 memset(&er, 0, sizeof(struct gfs2_ea_request));
1162 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1163 if (er.er_type == GFS2_EATYPE_UNUSED)
1164 return -EOPNOTSUPP;
1165 er.er_data = (char *)data;
1166 er.er_name_len = strlen(er.er_name);
1167 er.er_data_len = size;
1168 er.er_flags = flags;
1169
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001170 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001171
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001172 return gfs2_ea_set(GFS2_I(inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001173}
1174
1175static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1176 void *data, size_t size)
1177{
1178 struct gfs2_ea_request er;
1179
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180 memset(&er, 0, sizeof(struct gfs2_ea_request));
1181 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1182 if (er.er_type == GFS2_EATYPE_UNUSED)
1183 return -EOPNOTSUPP;
1184 er.er_data = data;
1185 er.er_name_len = strlen(er.er_name);
1186 er.er_data_len = size;
1187
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001188 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001189}
1190
1191static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1192{
1193 struct gfs2_ea_request er;
1194
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195 memset(&er, 0, sizeof(struct gfs2_ea_request));
1196 er.er_data = (size) ? buffer : NULL;
1197 er.er_data_len = size;
1198
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001199 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001200}
1201
1202static int gfs2_removexattr(struct dentry *dentry, const char *name)
1203{
1204 struct gfs2_ea_request er;
1205
David Teiglandb3b94fa2006-01-16 16:50:04 +00001206 memset(&er, 0, sizeof(struct gfs2_ea_request));
1207 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1208 if (er.er_type == GFS2_EATYPE_UNUSED)
1209 return -EOPNOTSUPP;
1210 er.er_name_len = strlen(er.er_name);
1211
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001212 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001213}
1214
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001215static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1216 u64 start, u64 len)
1217{
1218 struct gfs2_inode *ip = GFS2_I(inode);
1219 struct gfs2_holder gh;
1220 int ret;
1221
1222 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1223 if (ret)
1224 return ret;
1225
1226 mutex_lock(&inode->i_mutex);
1227
1228 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1229 if (ret)
1230 goto out;
1231
1232 if (gfs2_is_stuffed(ip)) {
1233 u64 phys = ip->i_no_addr << inode->i_blkbits;
1234 u64 size = i_size_read(inode);
1235 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1236 FIEMAP_EXTENT_DATA_INLINE;
1237 phys += sizeof(struct gfs2_dinode);
1238 phys += start;
1239 if (start + len > size)
1240 len = size - start;
1241 if (start < size)
1242 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1243 len, flags);
1244 if (ret == 1)
1245 ret = 0;
1246 } else {
1247 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1248 gfs2_block_map);
1249 }
1250
1251 gfs2_glock_dq_uninit(&gh);
1252out:
1253 mutex_unlock(&inode->i_mutex);
1254 return ret;
1255}
1256
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001257const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04001258 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001259 .setattr = gfs2_setattr,
1260 .getattr = gfs2_getattr,
1261 .setxattr = gfs2_setxattr,
1262 .getxattr = gfs2_getxattr,
1263 .listxattr = gfs2_listxattr,
1264 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001265 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001266};
1267
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001268const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001269 .create = gfs2_create,
1270 .lookup = gfs2_lookup,
1271 .link = gfs2_link,
1272 .unlink = gfs2_unlink,
1273 .symlink = gfs2_symlink,
1274 .mkdir = gfs2_mkdir,
1275 .rmdir = gfs2_rmdir,
1276 .mknod = gfs2_mknod,
1277 .rename = gfs2_rename,
Al Viroe6305c42008-07-15 21:03:57 -04001278 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279 .setattr = gfs2_setattr,
1280 .getattr = gfs2_getattr,
1281 .setxattr = gfs2_setxattr,
1282 .getxattr = gfs2_getxattr,
1283 .listxattr = gfs2_listxattr,
1284 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001285 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001286};
1287
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001288const struct inode_operations gfs2_symlink_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001289 .readlink = gfs2_readlink,
1290 .follow_link = gfs2_follow_link,
Al Viroe6305c42008-07-15 21:03:57 -04001291 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001292 .setattr = gfs2_setattr,
1293 .getattr = gfs2_getattr,
1294 .setxattr = gfs2_setxattr,
1295 .getxattr = gfs2_getxattr,
1296 .listxattr = gfs2_listxattr,
1297 .removexattr = gfs2_removexattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01001298 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001299};
1300