blob: d85f6e05cb955d99cebcad6aa3bd660871cef49f [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>
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"
34#include "ops_dentry.h"
35#include "ops_inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000036#include "quota.h"
37#include "rgrp.h"
38#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050039#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000040
41/**
42 * gfs2_create - Create a file
43 * @dir: The directory in which to create the file
44 * @dentry: The dentry of the new file
45 * @mode: The mode of the new file
46 *
47 * Returns: errno
48 */
49
50static int gfs2_create(struct inode *dir, struct dentry *dentry,
51 int mode, struct nameidata *nd)
52{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040053 struct gfs2_inode *dip = GFS2_I(dir);
54 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +000055 struct gfs2_holder ghs[2];
56 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +000057
David Teiglandb3b94fa2006-01-16 16:50:04 +000058 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
59
60 for (;;) {
Steven Whitehousee7f14f42006-10-31 21:45:08 -050061 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +000062 if (!IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000063 gfs2_trans_end(sdp);
64 if (dip->i_alloc.al_rgd)
65 gfs2_inplace_release(dip);
66 gfs2_quota_unlock(dip);
67 gfs2_alloc_put(dip);
68 gfs2_glock_dq_uninit_m(2, ghs);
Steven Whitehouse3a8476d2006-06-19 09:10:39 -040069 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000070 break;
Steven Whitehouse7359a192006-02-13 12:27:43 +000071 } else if (PTR_ERR(inode) != -EEXIST ||
David Teiglandb3b94fa2006-01-16 16:50:04 +000072 (nd->intent.open.flags & O_EXCL)) {
73 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +000074 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000075 }
76
Steven Whitehousec7526662006-03-20 12:30:04 -050077 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
78 if (inode) {
79 if (!IS_ERR(inode)) {
Steven Whitehousec7526662006-03-20 12:30:04 -050080 gfs2_holder_uninit(ghs);
81 break;
82 } else {
83 gfs2_holder_uninit(ghs);
84 return PTR_ERR(inode);
85 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000086 }
87 }
88
David Teiglandb3b94fa2006-01-16 16:50:04 +000089 d_instantiate(dentry, inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000090
91 return 0;
92}
93
94/**
95 * gfs2_lookup - Look up a filename in a directory and return its inode
96 * @dir: The directory inode
97 * @dentry: The dentry of the new inode
98 * @nd: passed from Linux VFS, ignored by us
99 *
100 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
101 *
102 * Returns: errno
103 */
104
105static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
106 struct nameidata *nd)
107{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108 struct inode *inode = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109
Steven Whitehousec7526662006-03-20 12:30:04 -0500110 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000111
Steven Whitehousec7526662006-03-20 12:30:04 -0500112 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
113 if (inode && IS_ERR(inode))
114 return ERR_PTR(PTR_ERR(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115
116 if (inode)
117 return d_splice_alias(inode, dentry);
118 d_add(dentry, inode);
119
120 return NULL;
121}
122
123/**
124 * gfs2_link - Link to a file
125 * @old_dentry: The inode to link
126 * @dir: Add link to this directory
127 * @dentry: The name of the link
128 *
129 * Link the inode in "old_dentry" into the directory "dir" with the
130 * name in "dentry".
131 *
132 * Returns: errno
133 */
134
135static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
136 struct dentry *dentry)
137{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400138 struct gfs2_inode *dip = GFS2_I(dir);
139 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140 struct inode *inode = old_dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400141 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000142 struct gfs2_holder ghs[2];
143 int alloc_required;
144 int error;
145
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500146 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 return -EPERM;
148
149 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
150 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
151
152 error = gfs2_glock_nq_m(2, ghs);
153 if (error)
154 goto out;
155
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400156 error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157 if (error)
158 goto out_gunlock;
159
Steven Whitehousec7526662006-03-20 12:30:04 -0500160 error = gfs2_dir_search(dir, &dentry->d_name, NULL, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161 switch (error) {
162 case -ENOENT:
163 break;
164 case 0:
165 error = -EEXIST;
166 default:
167 goto out_gunlock;
168 }
169
170 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500171 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 goto out_gunlock;
173 error = -EFBIG;
Steven Whitehousecd915492006-09-04 12:49:07 -0400174 if (dip->i_di.di_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175 goto out_gunlock;
176 error = -EPERM;
177 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
178 goto out_gunlock;
179 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500180 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000181 goto out_gunlock;
182 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500183 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000184 goto out_gunlock;
185
Steven Whitehousec7526662006-03-20 12:30:04 -0500186 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
187 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000188 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500189 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190
191 if (alloc_required) {
192 struct gfs2_alloc *al = gfs2_alloc_get(dip);
193
194 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
195 if (error)
196 goto out_alloc;
197
Steven Whitehouse2933f922006-11-01 13:23:29 -0500198 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000199 if (error)
200 goto out_gunlock_q;
201
202 al->al_requested = sdp->sd_max_dirres;
203
204 error = gfs2_inplace_reserve(dip);
205 if (error)
206 goto out_gunlock_q;
207
Steven Whitehouse1b502592006-05-18 14:10:52 -0400208 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000209 al->al_rgd->rd_ri.ri_length +
210 2 * RES_DINODE + RES_STATFS +
211 RES_QUOTA, 0);
212 if (error)
213 goto out_ipres;
214 } else {
215 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
216 if (error)
217 goto out_ipres;
218 }
219
Steven Whitehousec7526662006-03-20 12:30:04 -0500220 error = gfs2_dir_add(dir, &dentry->d_name, &ip->i_num,
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500221 IF2DT(inode->i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000222 if (error)
223 goto out_end_trans;
224
225 error = gfs2_change_nlink(ip, +1);
226
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400227out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000228 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400229out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230 if (alloc_required)
231 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400232out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000233 if (alloc_required)
234 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400235out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000236 if (alloc_required)
237 gfs2_alloc_put(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400238out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000239 gfs2_glock_dq_m(2, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400240out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000241 gfs2_holder_uninit(ghs);
242 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000243 if (!error) {
Steven Whitehouse29937ac2006-07-06 17:58:03 -0400244 atomic_inc(&inode->i_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245 d_instantiate(dentry, inode);
246 mark_inode_dirty(inode);
247 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000248 return error;
249}
250
251/**
252 * gfs2_unlink - Unlink a file
253 * @dir: The inode of the directory containing the file to unlink
254 * @dentry: The file itself
255 *
256 * Unlink a file. Call gfs2_unlinki()
257 *
258 * Returns: errno
259 */
260
261static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
262{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400263 struct gfs2_inode *dip = GFS2_I(dir);
264 struct gfs2_sbd *sdp = GFS2_SB(dir);
265 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600266 struct gfs2_holder ghs[3];
267 struct gfs2_rgrpd *rgd;
268 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269 int error;
270
Russell Cattelanddee7602007-01-29 17:13:44 -0600271 error = gfs2_rindex_hold(sdp, &ri_gh);
272 if (error)
273 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274
Russell Cattelanddee7602007-01-29 17:13:44 -0600275 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
276 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
277
278 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
279 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
280
281
282 error = gfs2_glock_nq_m(3, ghs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000283 if (error)
284 goto out;
285
286 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
287 if (error)
288 goto out_gunlock;
289
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400290 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000291 if (error)
292 goto out_gunlock;
293
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400294 error = gfs2_dir_del(dip, &dentry->d_name);
295 if (error)
296 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000297
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400298 error = gfs2_change_nlink(ip, -1);
299
300out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000301 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400302out_gunlock:
Russell Cattelanddee7602007-01-29 17:13:44 -0600303 gfs2_glock_dq_m(3, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400304out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000305 gfs2_holder_uninit(ghs);
306 gfs2_holder_uninit(ghs + 1);
Russell Cattelanddee7602007-01-29 17:13:44 -0600307 gfs2_holder_uninit(ghs + 2);
308 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000309 return error;
310}
311
312/**
313 * gfs2_symlink - Create a symlink
314 * @dir: The directory to create the symlink in
315 * @dentry: The dentry to put the symlink in
316 * @symname: The thing which the link points to
317 *
318 * Returns: errno
319 */
320
321static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
322 const char *symname)
323{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400324 struct gfs2_inode *dip = GFS2_I(dir), *ip;
325 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000326 struct gfs2_holder ghs[2];
327 struct inode *inode;
328 struct buffer_head *dibh;
329 int size;
330 int error;
331
David Teiglandb3b94fa2006-01-16 16:50:04 +0000332 /* Must be stuffed with a null terminator for gfs2_follow_link() */
333 size = strlen(symname);
334 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
335 return -ENAMETOOLONG;
336
337 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
338
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500339 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000340 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000342 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000343 }
344
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500345 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346
347 ip->i_di.di_size = size;
348
349 error = gfs2_meta_inode_buffer(ip, &dibh);
350
351 if (!gfs2_assert_withdraw(sdp, !error)) {
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500352 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
354 size);
355 brelse(dibh);
356 }
357
358 gfs2_trans_end(sdp);
359 if (dip->i_alloc.al_rgd)
360 gfs2_inplace_release(dip);
361 gfs2_quota_unlock(dip);
362 gfs2_alloc_put(dip);
363
364 gfs2_glock_dq_uninit_m(2, ghs);
365
David Teiglandb3b94fa2006-01-16 16:50:04 +0000366 d_instantiate(dentry, inode);
367 mark_inode_dirty(inode);
368
369 return 0;
370}
371
372/**
373 * gfs2_mkdir - Make a directory
374 * @dir: The parent directory of the new one
375 * @dentry: The dentry of the new directory
376 * @mode: The mode of the new directory
377 *
378 * Returns: errno
379 */
380
381static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
382{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400383 struct gfs2_inode *dip = GFS2_I(dir), *ip;
384 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000385 struct gfs2_holder ghs[2];
386 struct inode *inode;
387 struct buffer_head *dibh;
388 int error;
389
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
391
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500392 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000393 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000395 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000396 }
397
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500398 ip = ghs[1].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000399
Steven Whitehouse4f561102006-11-01 14:04:17 -0500400 ip->i_inode.i_nlink = 2;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000401 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
402 ip->i_di.di_flags |= GFS2_DIF_JDATA;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000403 ip->i_di.di_entries = 2;
404
405 error = gfs2_meta_inode_buffer(ip, &dibh);
406
407 if (!gfs2_assert_withdraw(sdp, !error)) {
408 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -0500409 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500410 struct qstr str;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500412 gfs2_str2qstr(&str, ".");
Steven Whitehousec7526662006-03-20 12:30:04 -0500413 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
414 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000415 dent->de_inum = di->di_num; /* already GFS2 endian */
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400416 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000417 di->di_entries = cpu_to_be32(1);
418
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500419 gfs2_str2qstr(&str, "..");
Steven Whitehousec7526662006-03-20 12:30:04 -0500420 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
421 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422
Steven Whitehouse409e1852006-10-02 14:20:43 -0400423 gfs2_inum_out(&dip->i_num, &dent->de_inum);
Steven Whitehouse7ecdb702006-10-03 21:03:35 -0400424 dent->de_type = cpu_to_be16(DT_DIR);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500426 gfs2_dinode_out(ip, di);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427
428 brelse(dibh);
429 }
430
431 error = gfs2_change_nlink(dip, +1);
432 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
433
434 gfs2_trans_end(sdp);
435 if (dip->i_alloc.al_rgd)
436 gfs2_inplace_release(dip);
437 gfs2_quota_unlock(dip);
438 gfs2_alloc_put(dip);
439
440 gfs2_glock_dq_uninit_m(2, ghs);
441
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442 d_instantiate(dentry, inode);
443 mark_inode_dirty(inode);
444
445 return 0;
446}
447
448/**
449 * gfs2_rmdir - Remove a directory
450 * @dir: The parent directory of the directory to be removed
451 * @dentry: The dentry of the directory to remove
452 *
453 * Remove a directory. Call gfs2_rmdiri()
454 *
455 * Returns: errno
456 */
457
458static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
459{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400460 struct gfs2_inode *dip = GFS2_I(dir);
461 struct gfs2_sbd *sdp = GFS2_SB(dir);
462 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
Russell Cattelanddee7602007-01-29 17:13:44 -0600463 struct gfs2_holder ghs[3];
464 struct gfs2_rgrpd *rgd;
465 struct gfs2_holder ri_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000466 int error;
467
Russell Cattelanddee7602007-01-29 17:13:44 -0600468
469 error = gfs2_rindex_hold(sdp, &ri_gh);
470 if (error)
471 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000472 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
473 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
474
Russell Cattelanddee7602007-01-29 17:13:44 -0600475 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
476 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
477
478 error = gfs2_glock_nq_m(3, ghs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000479 if (error)
480 goto out;
481
482 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
483 if (error)
484 goto out_gunlock;
485
486 if (ip->i_di.di_entries < 2) {
487 if (gfs2_consist_inode(ip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500488 gfs2_dinode_print(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489 error = -EIO;
490 goto out_gunlock;
491 }
492 if (ip->i_di.di_entries > 2) {
493 error = -ENOTEMPTY;
494 goto out_gunlock;
495 }
496
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400497 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000498 if (error)
499 goto out_gunlock;
500
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400501 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000502
503 gfs2_trans_end(sdp);
504
Steven Whitehousea91ea692006-09-04 12:04:26 -0400505out_gunlock:
Russell Cattelanddee7602007-01-29 17:13:44 -0600506 gfs2_glock_dq_m(3, ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400507out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000508 gfs2_holder_uninit(ghs);
509 gfs2_holder_uninit(ghs + 1);
Russell Cattelanddee7602007-01-29 17:13:44 -0600510 gfs2_holder_uninit(ghs + 2);
511 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512 return error;
513}
514
515/**
516 * gfs2_mknod - Make a special file
517 * @dir: The directory in which the special file will reside
518 * @dentry: The dentry of the special file
519 * @mode: The mode of the special file
520 * @rdev: The device specification of the special file
521 *
522 */
523
524static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
525 dev_t dev)
526{
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500527 struct gfs2_inode *dip = GFS2_I(dir);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400528 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000529 struct gfs2_holder ghs[2];
530 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000531
532 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
533
Steven Whitehousee7f14f42006-10-31 21:45:08 -0500534 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000535 if (IS_ERR(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536 gfs2_holder_uninit(ghs);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000537 return PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000538 }
539
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540 gfs2_trans_end(sdp);
541 if (dip->i_alloc.al_rgd)
542 gfs2_inplace_release(dip);
543 gfs2_quota_unlock(dip);
544 gfs2_alloc_put(dip);
545
546 gfs2_glock_dq_uninit_m(2, ghs);
547
David Teiglandb3b94fa2006-01-16 16:50:04 +0000548 d_instantiate(dentry, inode);
549 mark_inode_dirty(inode);
550
551 return 0;
552}
553
554/**
555 * gfs2_rename - Rename a file
556 * @odir: Parent directory of old file name
557 * @odentry: The old dentry of the file
558 * @ndir: Parent directory of new file name
559 * @ndentry: The new dentry of the file
560 *
561 * Returns: errno
562 */
563
564static int gfs2_rename(struct inode *odir, struct dentry *odentry,
565 struct inode *ndir, struct dentry *ndentry)
566{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400567 struct gfs2_inode *odip = GFS2_I(odir);
568 struct gfs2_inode *ndip = GFS2_I(ndir);
569 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000570 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400571 struct gfs2_sbd *sdp = GFS2_SB(odir);
Russell Cattelanddee7602007-01-29 17:13:44 -0600572 struct gfs2_holder ghs[5], r_gh;
573 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574 unsigned int num_gh;
575 int dir_rename = 0;
576 int alloc_required;
577 unsigned int x;
578 int error;
579
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 if (ndentry->d_inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400581 nip = GFS2_I(ndentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000582 if (ip == nip)
583 return 0;
584 }
585
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586 /* Make sure we aren't trying to move a dirctory into it's subdir */
587
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500588 if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589 dir_rename = 1;
590
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500591 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592 &r_gh);
593 if (error)
594 goto out;
595
596 error = gfs2_ok_to_move(ip, ndip);
597 if (error)
598 goto out_gunlock_r;
599 }
600
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400601 num_gh = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000602 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400603 if (odip != ndip) {
604 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
605 num_gh++;
606 }
607 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
608 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000609
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400610 if (nip) {
611 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
612 num_gh++;
Russell Cattelanddee7602007-01-29 17:13:44 -0600613 /* grab the resource lock for unlink flag twiddling
614 * this is the case of the target file already existing
615 * so we unlink before doing the rename
616 */
617 nrgd = gfs2_blk2rgrpd(sdp, nip->i_num.no_addr);
618 if (nrgd)
619 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -0400620 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621
622 error = gfs2_glock_nq_m(num_gh, ghs);
623 if (error)
624 goto out_uninit;
625
626 /* Check out the old directory */
627
628 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
629 if (error)
630 goto out_gunlock;
631
632 /* Check out the new directory */
633
634 if (nip) {
635 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
636 if (error)
637 goto out_gunlock;
638
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500639 if (S_ISDIR(nip->i_inode.i_mode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640 if (nip->i_di.di_entries < 2) {
641 if (gfs2_consist_inode(nip))
Steven Whitehouse4cc14f02006-10-31 19:00:24 -0500642 gfs2_dinode_print(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000643 error = -EIO;
644 goto out_gunlock;
645 }
646 if (nip->i_di.di_entries > 2) {
647 error = -ENOTEMPTY;
648 goto out_gunlock;
649 }
650 }
651 } else {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400652 error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000653 if (error)
654 goto out_gunlock;
655
Steven Whitehousec7526662006-03-20 12:30:04 -0500656 error = gfs2_dir_search(ndir, &ndentry->d_name, NULL, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000657 switch (error) {
658 case -ENOENT:
659 error = 0;
660 break;
661 case 0:
662 error = -EEXIST;
663 default:
664 goto out_gunlock;
665 };
666
667 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -0500668 if (!ndip->i_inode.i_nlink) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669 error = -EINVAL;
670 goto out_gunlock;
671 }
Steven Whitehousecd915492006-09-04 12:49:07 -0400672 if (ndip->i_di.di_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000673 error = -EFBIG;
674 goto out_gunlock;
675 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500676 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -0500677 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678 error = -EMLINK;
679 goto out_gunlock;
680 }
681 }
682 }
683
684 /* Check out the dir to be renamed */
685
686 if (dir_rename) {
Steven Whitehousefaf450e2006-06-22 10:59:10 -0400687 error = permission(odentry->d_inode, MAY_WRITE, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688 if (error)
689 goto out_gunlock;
690 }
691
Steven Whitehousec7526662006-03-20 12:30:04 -0500692 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
693 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694 goto out_gunlock;
Steven Whitehousec7526662006-03-20 12:30:04 -0500695 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000696
697 if (alloc_required) {
698 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
699
700 error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
701 if (error)
702 goto out_alloc;
703
Steven Whitehouse2933f922006-11-01 13:23:29 -0500704 error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705 if (error)
706 goto out_gunlock_q;
707
708 al->al_requested = sdp->sd_max_dirres;
709
710 error = gfs2_inplace_reserve(ndip);
711 if (error)
712 goto out_gunlock_q;
713
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400714 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715 al->al_rgd->rd_ri.ri_length +
716 4 * RES_DINODE + 4 * RES_LEAF +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500717 RES_STATFS + RES_QUOTA + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000718 if (error)
719 goto out_ipreserv;
720 } else {
721 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500722 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723 if (error)
724 goto out_gunlock;
725 }
726
727 /* Remove the target file, if it exists */
728
729 if (nip) {
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500730 if (S_ISDIR(nip->i_inode.i_mode))
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400731 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
732 else {
733 error = gfs2_dir_del(ndip, &ndentry->d_name);
734 if (error)
735 goto out_end_trans;
S. Wendy Cheng87d21e02007-01-18 16:07:03 -0500736 error = gfs2_change_nlink(nip, -1);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400737 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738 if (error)
739 goto out_end_trans;
740 }
741
742 if (dir_rename) {
743 struct qstr name;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500744 gfs2_str2qstr(&name, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745
746 error = gfs2_change_nlink(ndip, +1);
747 if (error)
748 goto out_end_trans;
749 error = gfs2_change_nlink(odip, -1);
750 if (error)
751 goto out_end_trans;
752
753 error = gfs2_dir_mvino(ip, &name, &ndip->i_num, DT_DIR);
754 if (error)
755 goto out_end_trans;
756 } else {
757 struct buffer_head *dibh;
758 error = gfs2_meta_inode_buffer(ip, &dibh);
759 if (error)
760 goto out_end_trans;
Eric Sandeenddfe0622007-01-18 16:41:23 -0600761 ip->i_inode.i_ctime = CURRENT_TIME_SEC;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000762 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500763 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 brelse(dibh);
765 }
766
767 error = gfs2_dir_del(odip, &odentry->d_name);
768 if (error)
769 goto out_end_trans;
770
Steven Whitehousec7526662006-03-20 12:30:04 -0500771 error = gfs2_dir_add(ndir, &ndentry->d_name, &ip->i_num,
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500772 IF2DT(ip->i_inode.i_mode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773 if (error)
774 goto out_end_trans;
775
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400776out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400778out_ipreserv:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000779 if (alloc_required)
780 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400781out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000782 if (alloc_required)
783 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400784out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785 if (alloc_required)
786 gfs2_alloc_put(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400787out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788 gfs2_glock_dq_m(num_gh, ghs);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400789out_uninit:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790 for (x = 0; x < num_gh; x++)
791 gfs2_holder_uninit(ghs + x);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400792out_gunlock_r:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793 if (dir_rename)
794 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400795out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796 return error;
797}
798
799/**
800 * gfs2_readlink - Read the value of a symlink
801 * @dentry: the symlink
802 * @buf: the buffer to read the symlink data into
803 * @size: the size of the buffer
804 *
805 * Returns: errno
806 */
807
808static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
809 int user_size)
810{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400811 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812 char array[GFS2_FAST_NAME_SIZE], *buf = array;
813 unsigned int len = GFS2_FAST_NAME_SIZE;
814 int error;
815
David Teiglandb3b94fa2006-01-16 16:50:04 +0000816 error = gfs2_readlinki(ip, &buf, &len);
817 if (error)
818 return error;
819
820 if (user_size > len - 1)
821 user_size = len - 1;
822
823 if (copy_to_user(user_buf, buf, user_size))
824 error = -EFAULT;
825 else
826 error = user_size;
827
828 if (buf != array)
829 kfree(buf);
830
831 return error;
832}
833
834/**
835 * gfs2_follow_link - Follow a symbolic link
836 * @dentry: The dentry of the link
837 * @nd: Data that we pass to vfs_follow_link()
838 *
839 * This can handle symlinks of any size. It is optimised for symlinks
840 * under GFS2_FAST_NAME_SIZE.
841 *
842 * Returns: 0 on success or error code
843 */
844
845static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
846{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400847 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 char array[GFS2_FAST_NAME_SIZE], *buf = array;
849 unsigned int len = GFS2_FAST_NAME_SIZE;
850 int error;
851
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852 error = gfs2_readlinki(ip, &buf, &len);
853 if (!error) {
854 error = vfs_follow_link(nd, buf);
855 if (buf != array)
856 kfree(buf);
857 }
858
859 return ERR_PTR(error);
860}
861
862/**
863 * gfs2_permission -
864 * @inode:
865 * @mask:
866 * @nd: passed from Linux VFS, ignored by us
867 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500868 * This may be called from the VFS directly, or from within GFS2 with the
869 * inode locked, so we look to see if the glock is already locked and only
870 * lock the glock if its not already been done.
871 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872 * Returns: errno
873 */
874
875static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
876{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400877 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000878 struct gfs2_holder i_gh;
879 int error;
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500880 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500882 if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
883 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
884 if (error)
885 return error;
886 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000887 }
888
Steven Whitehouse77386e12006-11-29 10:41:49 -0500889 error = generic_permission(inode, mask, gfs2_check_acl);
Steven Whitehouse300c7d72006-11-27 09:55:28 -0500890 if (unlock)
891 gfs2_glock_dq_uninit(&i_gh);
892
David Teiglandb3b94fa2006-01-16 16:50:04 +0000893 return error;
894}
895
896static int setattr_size(struct inode *inode, struct iattr *attr)
897{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400898 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899 int error;
900
901 if (attr->ia_size != ip->i_di.di_size) {
902 error = vmtruncate(inode, attr->ia_size);
903 if (error)
904 return error;
905 }
906
Steven Whitehouseaa6a85a2006-01-24 10:37:06 +0000907 error = gfs2_truncatei(ip, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000908 if (error)
909 return error;
910
911 return error;
912}
913
914static int setattr_chown(struct inode *inode, struct iattr *attr)
915{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400916 struct gfs2_inode *ip = GFS2_I(inode);
917 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000918 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400919 u32 ouid, ogid, nuid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920 int error;
921
Steven Whitehouse2933f922006-11-01 13:23:29 -0500922 ouid = inode->i_uid;
923 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924 nuid = attr->ia_uid;
925 ngid = attr->ia_gid;
926
927 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
928 ouid = nuid = NO_QUOTA_CHANGE;
929 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
930 ogid = ngid = NO_QUOTA_CHANGE;
931
932 gfs2_alloc_get(ip);
933
934 error = gfs2_quota_lock(ip, nuid, ngid);
935 if (error)
936 goto out_alloc;
937
938 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
939 error = gfs2_quota_check(ip, nuid, ngid);
940 if (error)
941 goto out_gunlock_q;
942 }
943
944 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
945 if (error)
946 goto out_gunlock_q;
947
948 error = gfs2_meta_inode_buffer(ip, &dibh);
949 if (error)
950 goto out_end_trans;
951
952 error = inode_setattr(inode, attr);
953 gfs2_assert_warn(sdp, !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000955 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500956 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000957 brelse(dibh);
958
959 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
Steven Whitehouseaf18ddb2006-06-24 15:42:21 -0400960 gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid);
961 gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000962 }
963
Steven Whitehousea91ea692006-09-04 12:04:26 -0400964out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400966out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400968out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000969 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000970 return error;
971}
972
973/**
974 * gfs2_setattr - Change attributes on an inode
975 * @dentry: The dentry which is changing
976 * @attr: The structure describing the change
977 *
978 * The VFS layer wants to change one or more of an inodes attributes. Write
979 * that change out to disk.
980 *
981 * Returns: errno
982 */
983
984static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
985{
986 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400987 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000988 struct gfs2_holder i_gh;
989 int error;
990
David Teiglandb3b94fa2006-01-16 16:50:04 +0000991 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
992 if (error)
993 return error;
994
995 error = -EPERM;
996 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
997 goto out;
998
999 error = inode_change_ok(inode, attr);
1000 if (error)
1001 goto out;
1002
1003 if (attr->ia_valid & ATTR_SIZE)
1004 error = setattr_size(inode, attr);
1005 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1006 error = setattr_chown(inode, attr);
1007 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1008 error = gfs2_acl_chmod(ip, attr);
1009 else
1010 error = gfs2_setattr_simple(ip, attr);
1011
Steven Whitehousea91ea692006-09-04 12:04:26 -04001012out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001013 gfs2_glock_dq_uninit(&i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001014 if (!error)
1015 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016 return error;
1017}
1018
1019/**
1020 * gfs2_getattr - Read out an inode's attributes
Steven Whitehouse26c1a572006-09-04 15:32:10 -04001021 * @mnt: The vfsmount the inode is being accessed from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022 * @dentry: The dentry to stat
1023 * @stat: The inode's stats
1024 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001025 * This may be called from the VFS directly, or from within GFS2 with the
1026 * inode locked, so we look to see if the glock is already locked and only
1027 * lock the glock if its not already been done. Note that its the NFS
1028 * readdirplus operation which causes this to be called (from filldir)
1029 * with the glock already held.
1030 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001031 * Returns: errno
1032 */
1033
1034static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1035 struct kstat *stat)
1036{
1037 struct inode *inode = dentry->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001038 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001039 struct gfs2_holder gh;
1040 int error;
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001041 int unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001043 if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
1044 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1045 if (error)
1046 return error;
1047 unlock = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001048 }
1049
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001050 generic_fillattr(inode, stat);
Steven Whitehoused7c103d2007-01-25 17:14:59 +00001051 if (unlock)
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001052 gfs2_glock_dq_uninit(&gh);
1053
1054 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001055}
1056
1057static int gfs2_setxattr(struct dentry *dentry, const char *name,
1058 const void *data, size_t size, int flags)
1059{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001060 struct inode *inode = dentry->d_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061 struct gfs2_ea_request er;
1062
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063 memset(&er, 0, sizeof(struct gfs2_ea_request));
1064 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1065 if (er.er_type == GFS2_EATYPE_UNUSED)
1066 return -EOPNOTSUPP;
1067 er.er_data = (char *)data;
1068 er.er_name_len = strlen(er.er_name);
1069 er.er_data_len = size;
1070 er.er_flags = flags;
1071
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001072 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001073
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001074 return gfs2_ea_set(GFS2_I(inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075}
1076
1077static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1078 void *data, size_t size)
1079{
1080 struct gfs2_ea_request er;
1081
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082 memset(&er, 0, sizeof(struct gfs2_ea_request));
1083 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1084 if (er.er_type == GFS2_EATYPE_UNUSED)
1085 return -EOPNOTSUPP;
1086 er.er_data = data;
1087 er.er_name_len = strlen(er.er_name);
1088 er.er_data_len = size;
1089
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001090 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001091}
1092
1093static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1094{
1095 struct gfs2_ea_request er;
1096
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 memset(&er, 0, sizeof(struct gfs2_ea_request));
1098 er.er_data = (size) ? buffer : NULL;
1099 er.er_data_len = size;
1100
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001101 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001102}
1103
1104static int gfs2_removexattr(struct dentry *dentry, const char *name)
1105{
1106 struct gfs2_ea_request er;
1107
David Teiglandb3b94fa2006-01-16 16:50:04 +00001108 memset(&er, 0, sizeof(struct gfs2_ea_request));
1109 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1110 if (er.er_type == GFS2_EATYPE_UNUSED)
1111 return -EOPNOTSUPP;
1112 er.er_name_len = strlen(er.er_name);
1113
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001114 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115}
1116
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001117const struct inode_operations gfs2_file_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118 .permission = gfs2_permission,
1119 .setattr = gfs2_setattr,
1120 .getattr = gfs2_getattr,
1121 .setxattr = gfs2_setxattr,
1122 .getxattr = gfs2_getxattr,
1123 .listxattr = gfs2_listxattr,
1124 .removexattr = gfs2_removexattr,
1125};
1126
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001127const struct inode_operations gfs2_dev_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001128 .permission = gfs2_permission,
1129 .setattr = gfs2_setattr,
1130 .getattr = gfs2_getattr,
1131 .setxattr = gfs2_setxattr,
1132 .getxattr = gfs2_getxattr,
1133 .listxattr = gfs2_listxattr,
1134 .removexattr = gfs2_removexattr,
1135};
1136
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001137const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001138 .create = gfs2_create,
1139 .lookup = gfs2_lookup,
1140 .link = gfs2_link,
1141 .unlink = gfs2_unlink,
1142 .symlink = gfs2_symlink,
1143 .mkdir = gfs2_mkdir,
1144 .rmdir = gfs2_rmdir,
1145 .mknod = gfs2_mknod,
1146 .rename = gfs2_rename,
1147 .permission = gfs2_permission,
1148 .setattr = gfs2_setattr,
1149 .getattr = gfs2_getattr,
1150 .setxattr = gfs2_setxattr,
1151 .getxattr = gfs2_getxattr,
1152 .listxattr = gfs2_listxattr,
1153 .removexattr = gfs2_removexattr,
1154};
1155
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001156const struct inode_operations gfs2_symlink_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001157 .readlink = gfs2_readlink,
1158 .follow_link = gfs2_follow_link,
1159 .permission = gfs2_permission,
1160 .setattr = gfs2_setattr,
1161 .getattr = gfs2_getattr,
1162 .setxattr = gfs2_setxattr,
1163 .getxattr = gfs2_getxattr,
1164 .listxattr = gfs2_listxattr,
1165 .removexattr = gfs2_removexattr,
1166};
1167