]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/gfs2/ops_inode.c
[GFS2] Reduce size of struct gdlm_lock
[mirror_ubuntu-hirsute-kernel.git] / fs / gfs2 / ops_inode.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3a8a9a10 3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
b3b94faa
DT
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
9
b3b94faa
DT
10#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>
5c676f6d 19#include <linux/gfs2_ondisk.h>
71b86f56 20#include <linux/crc32.h>
7d308590 21#include <linux/lm_interface.h>
b3b94faa
DT
22#include <asm/uaccess.h>
23
24#include "gfs2.h"
5c676f6d 25#include "incore.h"
b3b94faa
DT
26#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"
b3b94faa
DT
36#include "quota.h"
37#include "rgrp.h"
38#include "trans.h"
5c676f6d 39#include "util.h"
b3b94faa
DT
40
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{
feaa7bba
SW
53 struct gfs2_inode *dip = GFS2_I(dir);
54 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
55 struct gfs2_holder ghs[2];
56 struct inode *inode;
b3b94faa 57
b3b94faa
DT
58 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
59
60 for (;;) {
e7f14f4d 61 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
7359a19c 62 if (!IS_ERR(inode)) {
b3b94faa
DT
63 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);
3a8476dd 69 mark_inode_dirty(inode);
b3b94faa 70 break;
7359a19c 71 } else if (PTR_ERR(inode) != -EEXIST ||
b3b94faa
DT
72 (nd->intent.open.flags & O_EXCL)) {
73 gfs2_holder_uninit(ghs);
7359a19c 74 return PTR_ERR(inode);
b3b94faa
DT
75 }
76
c752666c
SW
77 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
78 if (inode) {
79 if (!IS_ERR(inode)) {
c752666c
SW
80 gfs2_holder_uninit(ghs);
81 break;
82 } else {
83 gfs2_holder_uninit(ghs);
84 return PTR_ERR(inode);
85 }
b3b94faa
DT
86 }
87 }
88
b3b94faa 89 d_instantiate(dentry, inode);
b3b94faa
DT
90
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{
b3b94faa 108 struct inode *inode = NULL;
b3b94faa 109
c752666c 110 dentry->d_op = &gfs2_dops;
b3b94faa 111
c752666c
SW
112 inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
113 if (inode && IS_ERR(inode))
114 return ERR_PTR(PTR_ERR(inode));
b3b94faa
DT
115
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{
feaa7bba
SW
138 struct gfs2_inode *dip = GFS2_I(dir);
139 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa 140 struct inode *inode = old_dentry->d_inode;
feaa7bba 141 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
142 struct gfs2_holder ghs[2];
143 int alloc_required;
144 int error;
145
b60623c2 146 if (S_ISDIR(inode->i_mode))
b3b94faa
DT
147 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
faf450ef 156 error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
b3b94faa
DT
157 if (error)
158 goto out_gunlock;
159
c752666c 160 error = gfs2_dir_search(dir, &dentry->d_name, NULL, NULL);
b3b94faa
DT
161 switch (error) {
162 case -ENOENT:
163 break;
164 case 0:
165 error = -EEXIST;
166 default:
167 goto out_gunlock;
168 }
169
170 error = -EINVAL;
4f56110a 171 if (!dip->i_inode.i_nlink)
b3b94faa
DT
172 goto out_gunlock;
173 error = -EFBIG;
cd915493 174 if (dip->i_di.di_entries == (u32)-1)
b3b94faa
DT
175 goto out_gunlock;
176 error = -EPERM;
177 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
178 goto out_gunlock;
179 error = -EINVAL;
4f56110a 180 if (!ip->i_inode.i_nlink)
b3b94faa
DT
181 goto out_gunlock;
182 error = -EMLINK;
4f56110a 183 if (ip->i_inode.i_nlink == (u32)-1)
b3b94faa
DT
184 goto out_gunlock;
185
c752666c
SW
186 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
187 if (error < 0)
b3b94faa 188 goto out_gunlock;
c752666c 189 error = 0;
b3b94faa
DT
190
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
2933f925 198 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
b3b94faa
DT
199 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
1b50259b 208 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
b3b94faa
DT
209 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
c752666c 220 error = gfs2_dir_add(dir, &dentry->d_name, &ip->i_num,
b60623c2 221 IF2DT(inode->i_mode));
b3b94faa
DT
222 if (error)
223 goto out_end_trans;
224
225 error = gfs2_change_nlink(ip, +1);
226
feaa7bba 227out_end_trans:
b3b94faa 228 gfs2_trans_end(sdp);
feaa7bba 229out_ipres:
b3b94faa
DT
230 if (alloc_required)
231 gfs2_inplace_release(dip);
feaa7bba 232out_gunlock_q:
b3b94faa
DT
233 if (alloc_required)
234 gfs2_quota_unlock(dip);
feaa7bba 235out_alloc:
b3b94faa
DT
236 if (alloc_required)
237 gfs2_alloc_put(dip);
feaa7bba 238out_gunlock:
b3b94faa 239 gfs2_glock_dq_m(2, ghs);
feaa7bba 240out:
b3b94faa
DT
241 gfs2_holder_uninit(ghs);
242 gfs2_holder_uninit(ghs + 1);
b3b94faa 243 if (!error) {
29937ac6 244 atomic_inc(&inode->i_count);
b3b94faa
DT
245 d_instantiate(dentry, inode);
246 mark_inode_dirty(inode);
247 }
b3b94faa
DT
248 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{
feaa7bba
SW
263 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);
ddee7608
RC
266 struct gfs2_holder ghs[3];
267 struct gfs2_rgrpd *rgd;
268 struct gfs2_holder ri_gh;
b3b94faa
DT
269 int error;
270
ddee7608
RC
271 error = gfs2_rindex_hold(sdp, &ri_gh);
272 if (error)
273 return error;
274
b3b94faa 275 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
ddee7608 276 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
b3b94faa 277
ddee7608
RC
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);
b3b94faa
DT
283 if (error)
284 goto out;
285
286 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
287 if (error)
288 goto out_gunlock;
289
feaa7bba 290 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
b3b94faa
DT
291 if (error)
292 goto out_gunlock;
293
feaa7bba
SW
294 error = gfs2_dir_del(dip, &dentry->d_name);
295 if (error)
296 goto out_end_trans;
b3b94faa 297
feaa7bba 298 error = gfs2_change_nlink(ip, -1);
b3b94faa 299
feaa7bba
SW
300out_end_trans:
301 gfs2_trans_end(sdp);
302out_gunlock:
ddee7608 303 gfs2_glock_dq_m(3, ghs);
feaa7bba 304out:
b3b94faa
DT
305 gfs2_holder_uninit(ghs);
306 gfs2_holder_uninit(ghs + 1);
ddee7608
RC
307 gfs2_holder_uninit(ghs + 2);
308 gfs2_glock_dq_uninit(&ri_gh);
b3b94faa
DT
309 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{
feaa7bba
SW
324 struct gfs2_inode *dip = GFS2_I(dir), *ip;
325 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
326 struct gfs2_holder ghs[2];
327 struct inode *inode;
328 struct buffer_head *dibh;
329 int size;
330 int error;
331
b3b94faa
DT
332 /* 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
e7f14f4d 339 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
7359a19c 340 if (IS_ERR(inode)) {
b3b94faa 341 gfs2_holder_uninit(ghs);
7359a19c 342 return PTR_ERR(inode);
b3b94faa
DT
343 }
344
5c676f6d 345 ip = ghs[1].gh_gl->gl_object;
b3b94faa
DT
346
347 ip->i_di.di_size = size;
348
349 error = gfs2_meta_inode_buffer(ip, &dibh);
350
351 if (!gfs2_assert_withdraw(sdp, !error)) {
539e5d6b 352 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
353 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
b3b94faa
DT
366 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{
feaa7bba
SW
383 struct gfs2_inode *dip = GFS2_I(dir), *ip;
384 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
385 struct gfs2_holder ghs[2];
386 struct inode *inode;
387 struct buffer_head *dibh;
388 int error;
389
b3b94faa
DT
390 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
391
e7f14f4d 392 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
7359a19c 393 if (IS_ERR(inode)) {
b3b94faa 394 gfs2_holder_uninit(ghs);
7359a19c 395 return PTR_ERR(inode);
b3b94faa
DT
396 }
397
5c676f6d 398 ip = ghs[1].gh_gl->gl_object;
b3b94faa 399
4f56110a 400 ip->i_inode.i_nlink = 2;
b3b94faa
DT
401 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
402 ip->i_di.di_flags |= GFS2_DIF_JDATA;
b3b94faa
DT
403 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;
c752666c 409 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
71b86f56 410 struct qstr str;
b3b94faa 411
71b86f56 412 gfs2_str2qstr(&str, ".");
c752666c
SW
413 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
414 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
b3b94faa 415 dent->de_inum = di->di_num; /* already GFS2 endian */
7ecdb70a 416 dent->de_type = cpu_to_be16(DT_DIR);
b3b94faa
DT
417 di->di_entries = cpu_to_be32(1);
418
71b86f56 419 gfs2_str2qstr(&str, "..");
c752666c
SW
420 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);
b3b94faa 422
409e185d 423 gfs2_inum_out(&dip->i_num, &dent->de_inum);
7ecdb70a 424 dent->de_type = cpu_to_be16(DT_DIR);
b3b94faa 425
539e5d6b 426 gfs2_dinode_out(ip, di);
b3b94faa
DT
427
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
b3b94faa
DT
442 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{
feaa7bba
SW
460 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);
ddee7608
RC
463 struct gfs2_holder ghs[3];
464 struct gfs2_rgrpd *rgd;
465 struct gfs2_holder ri_gh;
b3b94faa
DT
466 int error;
467
ddee7608
RC
468
469 error = gfs2_rindex_hold(sdp, &ri_gh);
470 if (error)
471 return error;
b3b94faa
DT
472 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
ddee7608
RC
475 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);
b3b94faa
DT
479 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))
4cc14f0b 488 gfs2_dinode_print(ip);
b3b94faa
DT
489 error = -EIO;
490 goto out_gunlock;
491 }
492 if (ip->i_di.di_entries > 2) {
493 error = -ENOTEMPTY;
494 goto out_gunlock;
495 }
496
feaa7bba 497 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
b3b94faa
DT
498 if (error)
499 goto out_gunlock;
500
feaa7bba 501 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
b3b94faa
DT
502
503 gfs2_trans_end(sdp);
504
a91ea69f 505out_gunlock:
ddee7608 506 gfs2_glock_dq_m(3, ghs);
a91ea69f 507out:
b3b94faa
DT
508 gfs2_holder_uninit(ghs);
509 gfs2_holder_uninit(ghs + 1);
ddee7608
RC
510 gfs2_holder_uninit(ghs + 2);
511 gfs2_glock_dq_uninit(&ri_gh);
b3b94faa
DT
512 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{
e7f14f4d 527 struct gfs2_inode *dip = GFS2_I(dir);
feaa7bba 528 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
529 struct gfs2_holder ghs[2];
530 struct inode *inode;
b3b94faa
DT
531
532 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
533
e7f14f4d 534 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
7359a19c 535 if (IS_ERR(inode)) {
b3b94faa 536 gfs2_holder_uninit(ghs);
7359a19c 537 return PTR_ERR(inode);
b3b94faa
DT
538 }
539
b3b94faa
DT
540 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
b3b94faa
DT
548 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{
feaa7bba
SW
567 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);
b3b94faa 570 struct gfs2_inode *nip = NULL;
feaa7bba 571 struct gfs2_sbd *sdp = GFS2_SB(odir);
ddee7608
RC
572 struct gfs2_holder ghs[5], r_gh;
573 struct gfs2_rgrpd *nrgd;
b3b94faa
DT
574 unsigned int num_gh;
575 int dir_rename = 0;
576 int alloc_required;
577 unsigned int x;
578 int error;
579
b3b94faa 580 if (ndentry->d_inode) {
feaa7bba 581 nip = GFS2_I(ndentry->d_inode);
b3b94faa
DT
582 if (ip == nip)
583 return 0;
584 }
585
b3b94faa
DT
586 /* Make sure we aren't trying to move a dirctory into it's subdir */
587
b60623c2 588 if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
b3b94faa
DT
589 dir_rename = 1;
590
b60623c2 591 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
b3b94faa
DT
592 &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
d9d1ca30 601 num_gh = 1;
b3b94faa 602 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
d9d1ca30
SW
603 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++;
b3b94faa 609
d9d1ca30
SW
610 if (nip) {
611 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
612 num_gh++;
ddee7608
RC
613 /* 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++);
d9d1ca30 620 }
b3b94faa
DT
621
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
b60623c2 639 if (S_ISDIR(nip->i_inode.i_mode)) {
b3b94faa
DT
640 if (nip->i_di.di_entries < 2) {
641 if (gfs2_consist_inode(nip))
4cc14f0b 642 gfs2_dinode_print(nip);
b3b94faa
DT
643 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 {
faf450ef 652 error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
b3b94faa
DT
653 if (error)
654 goto out_gunlock;
655
c752666c 656 error = gfs2_dir_search(ndir, &ndentry->d_name, NULL, NULL);
b3b94faa
DT
657 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) {
4f56110a 668 if (!ndip->i_inode.i_nlink) {
b3b94faa
DT
669 error = -EINVAL;
670 goto out_gunlock;
671 }
cd915493 672 if (ndip->i_di.di_entries == (u32)-1) {
b3b94faa
DT
673 error = -EFBIG;
674 goto out_gunlock;
675 }
b60623c2 676 if (S_ISDIR(ip->i_inode.i_mode) &&
4f56110a 677 ndip->i_inode.i_nlink == (u32)-1) {
b3b94faa
DT
678 error = -EMLINK;
679 goto out_gunlock;
680 }
681 }
682 }
683
684 /* Check out the dir to be renamed */
685
686 if (dir_rename) {
faf450ef 687 error = permission(odentry->d_inode, MAY_WRITE, NULL);
b3b94faa
DT
688 if (error)
689 goto out_gunlock;
690 }
691
c752666c
SW
692 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
693 if (error < 0)
b3b94faa 694 goto out_gunlock;
c752666c 695 error = 0;
b3b94faa
DT
696
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
2933f925 704 error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid);
b3b94faa
DT
705 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
fe1bdedc 714 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
b3b94faa
DT
715 al->al_rgd->rd_ri.ri_length +
716 4 * RES_DINODE + 4 * RES_LEAF +
87d21e07 717 RES_STATFS + RES_QUOTA + 4, 0);
b3b94faa
DT
718 if (error)
719 goto out_ipreserv;
720 } else {
721 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
87d21e07 722 5 * RES_LEAF + 4, 0);
b3b94faa
DT
723 if (error)
724 goto out_gunlock;
725 }
726
727 /* Remove the target file, if it exists */
728
729 if (nip) {
b60623c2 730 if (S_ISDIR(nip->i_inode.i_mode))
feaa7bba
SW
731 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;
87d21e07 736 error = gfs2_change_nlink(nip, -1);
feaa7bba 737 }
b3b94faa
DT
738 if (error)
739 goto out_end_trans;
740 }
741
742 if (dir_rename) {
743 struct qstr name;
71b86f56 744 gfs2_str2qstr(&name, "..");
b3b94faa
DT
745
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;
ddfe0627 761 ip->i_inode.i_ctime = CURRENT_TIME_SEC;
d4e9c4c3 762 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 763 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
764 brelse(dibh);
765 }
766
767 error = gfs2_dir_del(odip, &odentry->d_name);
768 if (error)
769 goto out_end_trans;
770
c752666c 771 error = gfs2_dir_add(ndir, &ndentry->d_name, &ip->i_num,
b60623c2 772 IF2DT(ip->i_inode.i_mode));
b3b94faa
DT
773 if (error)
774 goto out_end_trans;
775
feaa7bba 776out_end_trans:
b3b94faa 777 gfs2_trans_end(sdp);
feaa7bba 778out_ipreserv:
b3b94faa
DT
779 if (alloc_required)
780 gfs2_inplace_release(ndip);
feaa7bba 781out_gunlock_q:
b3b94faa
DT
782 if (alloc_required)
783 gfs2_quota_unlock(ndip);
feaa7bba 784out_alloc:
b3b94faa
DT
785 if (alloc_required)
786 gfs2_alloc_put(ndip);
feaa7bba 787out_gunlock:
b3b94faa 788 gfs2_glock_dq_m(num_gh, ghs);
feaa7bba 789out_uninit:
b3b94faa
DT
790 for (x = 0; x < num_gh; x++)
791 gfs2_holder_uninit(ghs + x);
feaa7bba 792out_gunlock_r:
b3b94faa
DT
793 if (dir_rename)
794 gfs2_glock_dq_uninit(&r_gh);
feaa7bba 795out:
b3b94faa
DT
796 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{
feaa7bba 811 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
b3b94faa
DT
812 char array[GFS2_FAST_NAME_SIZE], *buf = array;
813 unsigned int len = GFS2_FAST_NAME_SIZE;
814 int error;
815
b3b94faa
DT
816 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{
feaa7bba 847 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
b3b94faa
DT
848 char array[GFS2_FAST_NAME_SIZE], *buf = array;
849 unsigned int len = GFS2_FAST_NAME_SIZE;
850 int error;
851
b3b94faa
DT
852 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 *
300c7d75
SW
868 * 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 *
b3b94faa
DT
872 * Returns: errno
873 */
874
875static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
876{
feaa7bba 877 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
878 struct gfs2_holder i_gh;
879 int error;
300c7d75 880 int unlock = 0;
b3b94faa 881
300c7d75
SW
882 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;
887 }
b3b94faa 888
77386e1f 889 error = generic_permission(inode, mask, gfs2_check_acl);
300c7d75 890 if (unlock)
b3b94faa 891 gfs2_glock_dq_uninit(&i_gh);
b3b94faa
DT
892
893 return error;
894}
895
896static int setattr_size(struct inode *inode, struct iattr *attr)
897{
feaa7bba 898 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
899 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
aa6a85a9 907 error = gfs2_truncatei(ip, attr->ia_size);
b3b94faa
DT
908 if (error)
909 return error;
910
911 return error;
912}
913
914static int setattr_chown(struct inode *inode, struct iattr *attr)
915{
feaa7bba
SW
916 struct gfs2_inode *ip = GFS2_I(inode);
917 struct gfs2_sbd *sdp = GFS2_SB(inode);
b3b94faa 918 struct buffer_head *dibh;
cd915493 919 u32 ouid, ogid, nuid, ngid;
b3b94faa
DT
920 int error;
921
2933f925
SW
922 ouid = inode->i_uid;
923 ogid = inode->i_gid;
b3b94faa
DT
924 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);
b3b94faa 954
d4e9c4c3 955 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 956 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
957 brelse(dibh);
958
959 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
af18ddb8
SW
960 gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid);
961 gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid);
b3b94faa
DT
962 }
963
a91ea69f 964out_end_trans:
b3b94faa 965 gfs2_trans_end(sdp);
a91ea69f 966out_gunlock_q:
b3b94faa 967 gfs2_quota_unlock(ip);
a91ea69f 968out_alloc:
b3b94faa 969 gfs2_alloc_put(ip);
b3b94faa
DT
970 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;
feaa7bba 987 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
988 struct gfs2_holder i_gh;
989 int error;
990
b3b94faa
DT
991 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
a91ea69f 1012out:
b3b94faa 1013 gfs2_glock_dq_uninit(&i_gh);
b3b94faa
DT
1014 if (!error)
1015 mark_inode_dirty(inode);
b3b94faa
DT
1016 return error;
1017}
1018
1019/**
1020 * gfs2_getattr - Read out an inode's attributes
26c1a574 1021 * @mnt: The vfsmount the inode is being accessed from
b3b94faa
DT
1022 * @dentry: The dentry to stat
1023 * @stat: The inode's stats
1024 *
dcf3dd85
SW
1025 * 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 *
b3b94faa
DT
1031 * 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;
feaa7bba 1038 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
1039 struct gfs2_holder gh;
1040 int error;
dcf3dd85 1041 int unlock = 0;
b3b94faa 1042
dcf3dd85
SW
1043 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;
b3b94faa
DT
1048 }
1049
dcf3dd85 1050 generic_fillattr(inode, stat);
d7c103d0 1051 if (unlock)
dcf3dd85
SW
1052 gfs2_glock_dq_uninit(&gh);
1053
1054 return 0;
b3b94faa
DT
1055}
1056
1057static int gfs2_setxattr(struct dentry *dentry, const char *name,
1058 const void *data, size_t size, int flags)
1059{
feaa7bba 1060 struct inode *inode = dentry->d_inode;
b3b94faa
DT
1061 struct gfs2_ea_request er;
1062
b3b94faa
DT
1063 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
feaa7bba 1072 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
b3b94faa 1073
feaa7bba 1074 return gfs2_ea_set(GFS2_I(inode), &er);
b3b94faa
DT
1075}
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
b3b94faa
DT
1082 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
feaa7bba 1090 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1091}
1092
1093static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1094{
1095 struct gfs2_ea_request er;
1096
b3b94faa
DT
1097 memset(&er, 0, sizeof(struct gfs2_ea_request));
1098 er.er_data = (size) ? buffer : NULL;
1099 er.er_data_len = size;
1100
feaa7bba 1101 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1102}
1103
1104static int gfs2_removexattr(struct dentry *dentry, const char *name)
1105{
1106 struct gfs2_ea_request er;
1107
b3b94faa
DT
1108 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
feaa7bba 1114 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1115}
1116
92e1d5be 1117const struct inode_operations gfs2_file_iops = {
b3b94faa
DT
1118 .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
92e1d5be 1127const struct inode_operations gfs2_dev_iops = {
b3b94faa
DT
1128 .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
92e1d5be 1137const struct inode_operations gfs2_dir_iops = {
b3b94faa
DT
1138 .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
92e1d5be 1156const struct inode_operations gfs2_symlink_iops = {
b3b94faa
DT
1157 .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