]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/gfs2/ops_inode.c
GFS2: Use new workqueue scheme
[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>
b3b94faa
DT
15#include <linux/mm.h>
16#include <linux/xattr.h>
17#include <linux/posix_acl.h>
5c676f6d 18#include <linux/gfs2_ondisk.h>
71b86f56 19#include <linux/crc32.h>
e9079cce 20#include <linux/fiemap.h>
3921120e
BM
21#include <linux/swap.h>
22#include <linux/falloc.h>
b3b94faa
DT
23#include <asm/uaccess.h>
24
25#include "gfs2.h"
5c676f6d 26#include "incore.h"
b3b94faa
DT
27#include "acl.h"
28#include "bmap.h"
29#include "dir.h"
307cf6e6 30#include "xattr.h"
b3b94faa
DT
31#include "glock.h"
32#include "inode.h"
33#include "meta_io.h"
b3b94faa
DT
34#include "quota.h"
35#include "rgrp.h"
36#include "trans.h"
5c676f6d 37#include "util.h"
b2760583 38#include "super.h"
b3b94faa
DT
39
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{
feaa7bba
SW
52 struct gfs2_inode *dip = GFS2_I(dir);
53 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
54 struct gfs2_holder ghs[2];
55 struct inode *inode;
b3b94faa 56
b3b94faa
DT
57 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
58
59 for (;;) {
e7f14f4d 60 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
7359a19c 61 if (!IS_ERR(inode)) {
b3b94faa 62 gfs2_trans_end(sdp);
6dbd8224 63 if (dip->i_alloc->al_rgd)
b3b94faa
DT
64 gfs2_inplace_release(dip);
65 gfs2_quota_unlock(dip);
66 gfs2_alloc_put(dip);
67 gfs2_glock_dq_uninit_m(2, ghs);
3a8476dd 68 mark_inode_dirty(inode);
b3b94faa 69 break;
7359a19c 70 } else if (PTR_ERR(inode) != -EEXIST ||
3516586a 71 (nd && nd->flags & LOOKUP_EXCL)) {
b3b94faa 72 gfs2_holder_uninit(ghs);
7359a19c 73 return PTR_ERR(inode);
b3b94faa
DT
74 }
75
a569c711 76 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
c752666c
SW
77 if (inode) {
78 if (!IS_ERR(inode)) {
c752666c
SW
79 gfs2_holder_uninit(ghs);
80 break;
81 } else {
82 gfs2_holder_uninit(ghs);
83 return PTR_ERR(inode);
84 }
b3b94faa
DT
85 }
86 }
87
b3b94faa 88 d_instantiate(dentry, inode);
b3b94faa
DT
89
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{
b3b94faa 107 struct inode *inode = NULL;
b3b94faa 108
c752666c 109 dentry->d_op = &gfs2_dops;
b3b94faa 110
a569c711 111 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
c752666c 112 if (inode && IS_ERR(inode))
e231c2ee 113 return ERR_CAST(inode);
b3b94faa 114
9656b2c1
SW
115 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);
b3b94faa 125 return d_splice_alias(inode, dentry);
9656b2c1 126 }
b3b94faa
DT
127 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{
feaa7bba
SW
147 struct gfs2_inode *dip = GFS2_I(dir);
148 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa 149 struct inode *inode = old_dentry->d_inode;
feaa7bba 150 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
151 struct gfs2_holder ghs[2];
152 int alloc_required;
153 int error;
154
b60623c2 155 if (S_ISDIR(inode->i_mode))
b3b94faa
DT
156 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
72dbf479
BP
161 error = gfs2_glock_nq(ghs); /* parent */
162 if (error)
163 goto out_parent;
164
165 error = gfs2_glock_nq(ghs + 1); /* child */
b3b94faa 166 if (error)
72dbf479 167 goto out_child;
b3b94faa 168
f58ba889 169 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
b3b94faa
DT
170 if (error)
171 goto out_gunlock;
172
dbb7cae2 173 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
b3b94faa
DT
174 switch (error) {
175 case -ENOENT:
176 break;
177 case 0:
178 error = -EEXIST;
179 default:
180 goto out_gunlock;
181 }
182
183 error = -EINVAL;
4f56110a 184 if (!dip->i_inode.i_nlink)
b3b94faa
DT
185 goto out_gunlock;
186 error = -EFBIG;
ad6203f2 187 if (dip->i_entries == (u32)-1)
b3b94faa
DT
188 goto out_gunlock;
189 error = -EPERM;
190 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
191 goto out_gunlock;
192 error = -EINVAL;
4f56110a 193 if (!ip->i_inode.i_nlink)
b3b94faa
DT
194 goto out_gunlock;
195 error = -EMLINK;
4f56110a 196 if (ip->i_inode.i_nlink == (u32)-1)
b3b94faa
DT
197 goto out_gunlock;
198
c752666c
SW
199 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
200 if (error < 0)
b3b94faa 201 goto out_gunlock;
c752666c 202 error = 0;
b3b94faa
DT
203
204 if (alloc_required) {
205 struct gfs2_alloc *al = gfs2_alloc_get(dip);
182fe5ab
CG
206 if (!al) {
207 error = -ENOMEM;
208 goto out_gunlock;
209 }
b3b94faa 210
d82661d9 211 error = gfs2_quota_lock_check(dip);
b3b94faa
DT
212 if (error)
213 goto out_alloc;
214
b3b94faa
DT
215 al->al_requested = sdp->sd_max_dirres;
216
217 error = gfs2_inplace_reserve(dip);
218 if (error)
219 goto out_gunlock_q;
220
1b50259b 221 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
bb8d8a6f 222 al->al_rgd->rd_length +
b3b94faa
DT
223 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
dbb7cae2 233 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
b3b94faa
DT
234 if (error)
235 goto out_end_trans;
236
237 error = gfs2_change_nlink(ip, +1);
238
feaa7bba 239out_end_trans:
b3b94faa 240 gfs2_trans_end(sdp);
feaa7bba 241out_ipres:
b3b94faa
DT
242 if (alloc_required)
243 gfs2_inplace_release(dip);
feaa7bba 244out_gunlock_q:
b3b94faa
DT
245 if (alloc_required)
246 gfs2_quota_unlock(dip);
feaa7bba 247out_alloc:
b3b94faa
DT
248 if (alloc_required)
249 gfs2_alloc_put(dip);
feaa7bba 250out_gunlock:
72dbf479
BP
251 gfs2_glock_dq(ghs + 1);
252out_child:
253 gfs2_glock_dq(ghs);
254out_parent:
b3b94faa
DT
255 gfs2_holder_uninit(ghs);
256 gfs2_holder_uninit(ghs + 1);
b3b94faa 257 if (!error) {
29937ac6 258 atomic_inc(&inode->i_count);
b3b94faa
DT
259 d_instantiate(dentry, inode);
260 mark_inode_dirty(inode);
261 }
b3b94faa
DT
262 return error;
263}
264
87ec2174
SW
265/*
266 * gfs2_unlink_ok - check to see that a inode is still in a directory
267 * @dip: the directory
268 * @name: the name of the file
269 * @ip: the inode
270 *
271 * Assumes that the lock on (at least) @dip is held.
272 *
273 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
274 */
275
276static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
277 const struct gfs2_inode *ip)
278{
279 int error;
280
281 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
282 return -EPERM;
283
284 if ((dip->i_inode.i_mode & S_ISVTX) &&
285 dip->i_inode.i_uid != current_fsuid() &&
286 ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
287 return -EPERM;
288
289 if (IS_APPEND(&dip->i_inode))
290 return -EPERM;
291
292 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
293 if (error)
294 return error;
295
296 error = gfs2_dir_check(&dip->i_inode, name, ip);
297 if (error)
298 return error;
299
300 return 0;
301}
302
b3b94faa
DT
303/**
304 * gfs2_unlink - Unlink a file
305 * @dir: The inode of the directory containing the file to unlink
306 * @dentry: The file itself
307 *
308 * Unlink a file. Call gfs2_unlinki()
309 *
310 * Returns: errno
311 */
312
313static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
314{
feaa7bba
SW
315 struct gfs2_inode *dip = GFS2_I(dir);
316 struct gfs2_sbd *sdp = GFS2_SB(dir);
317 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
ddee7608
RC
318 struct gfs2_holder ghs[3];
319 struct gfs2_rgrpd *rgd;
320 struct gfs2_holder ri_gh;
b3b94faa
DT
321 int error;
322
ddee7608
RC
323 error = gfs2_rindex_hold(sdp, &ri_gh);
324 if (error)
325 return error;
326
b3b94faa 327 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
ddee7608 328 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
b3b94faa 329
dbb7cae2 330 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
ddee7608
RC
331 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
332
333
8497a46e 334 error = gfs2_glock_nq(ghs); /* parent */
b3b94faa 335 if (error)
8497a46e
SW
336 goto out_parent;
337
338 error = gfs2_glock_nq(ghs + 1); /* child */
339 if (error)
340 goto out_child;
341
342 error = gfs2_glock_nq(ghs + 2); /* rgrp */
343 if (error)
344 goto out_rgrp;
b3b94faa
DT
345
346 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
347 if (error)
72dbf479 348 goto out_gunlock;
b3b94faa 349
feaa7bba 350 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
b3b94faa 351 if (error)
cd012075 352 goto out_gunlock;
b3b94faa 353
feaa7bba
SW
354 error = gfs2_dir_del(dip, &dentry->d_name);
355 if (error)
356 goto out_end_trans;
b3b94faa 357
feaa7bba 358 error = gfs2_change_nlink(ip, -1);
b3b94faa 359
feaa7bba
SW
360out_end_trans:
361 gfs2_trans_end(sdp);
72dbf479 362out_gunlock:
8497a46e
SW
363 gfs2_glock_dq(ghs + 2);
364out_rgrp:
ddee7608 365 gfs2_holder_uninit(ghs + 2);
8497a46e
SW
366 gfs2_glock_dq(ghs + 1);
367out_child:
368 gfs2_holder_uninit(ghs + 1);
369 gfs2_glock_dq(ghs);
370out_parent:
371 gfs2_holder_uninit(ghs);
ddee7608 372 gfs2_glock_dq_uninit(&ri_gh);
b3b94faa
DT
373 return error;
374}
375
376/**
377 * gfs2_symlink - Create a symlink
378 * @dir: The directory to create the symlink in
379 * @dentry: The dentry to put the symlink in
380 * @symname: The thing which the link points to
381 *
382 * Returns: errno
383 */
384
385static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
386 const char *symname)
387{
feaa7bba
SW
388 struct gfs2_inode *dip = GFS2_I(dir), *ip;
389 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
390 struct gfs2_holder ghs[2];
391 struct inode *inode;
392 struct buffer_head *dibh;
393 int size;
394 int error;
395
b3b94faa
DT
396 /* Must be stuffed with a null terminator for gfs2_follow_link() */
397 size = strlen(symname);
398 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
399 return -ENAMETOOLONG;
400
401 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
402
e7f14f4d 403 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
7359a19c 404 if (IS_ERR(inode)) {
b3b94faa 405 gfs2_holder_uninit(ghs);
7359a19c 406 return PTR_ERR(inode);
b3b94faa
DT
407 }
408
5c676f6d 409 ip = ghs[1].gh_gl->gl_object;
b3b94faa 410
5cf32524 411 i_size_write(inode, size);
b3b94faa
DT
412
413 error = gfs2_meta_inode_buffer(ip, &dibh);
414
415 if (!gfs2_assert_withdraw(sdp, !error)) {
539e5d6b 416 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
417 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
418 size);
419 brelse(dibh);
420 }
421
422 gfs2_trans_end(sdp);
6dbd8224 423 if (dip->i_alloc->al_rgd)
b3b94faa
DT
424 gfs2_inplace_release(dip);
425 gfs2_quota_unlock(dip);
426 gfs2_alloc_put(dip);
427
428 gfs2_glock_dq_uninit_m(2, ghs);
429
b3b94faa
DT
430 d_instantiate(dentry, inode);
431 mark_inode_dirty(inode);
432
433 return 0;
434}
435
436/**
437 * gfs2_mkdir - Make a directory
438 * @dir: The parent directory of the new one
439 * @dentry: The dentry of the new directory
440 * @mode: The mode of the new directory
441 *
442 * Returns: errno
443 */
444
445static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
446{
feaa7bba
SW
447 struct gfs2_inode *dip = GFS2_I(dir), *ip;
448 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
449 struct gfs2_holder ghs[2];
450 struct inode *inode;
451 struct buffer_head *dibh;
452 int error;
453
b3b94faa
DT
454 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
455
e7f14f4d 456 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
7359a19c 457 if (IS_ERR(inode)) {
b3b94faa 458 gfs2_holder_uninit(ghs);
7359a19c 459 return PTR_ERR(inode);
b3b94faa
DT
460 }
461
5c676f6d 462 ip = ghs[1].gh_gl->gl_object;
b3b94faa 463
4f56110a 464 ip->i_inode.i_nlink = 2;
a2e0f799 465 i_size_write(inode, sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
383f01fb 466 ip->i_diskflags |= GFS2_DIF_JDATA;
ad6203f2 467 ip->i_entries = 2;
b3b94faa
DT
468
469 error = gfs2_meta_inode_buffer(ip, &dibh);
470
471 if (!gfs2_assert_withdraw(sdp, !error)) {
472 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
c752666c 473 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
71b86f56 474 struct qstr str;
b3b94faa 475
71b86f56 476 gfs2_str2qstr(&str, ".");
c752666c
SW
477 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
478 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
b3b94faa 479 dent->de_inum = di->di_num; /* already GFS2 endian */
7ecdb70a 480 dent->de_type = cpu_to_be16(DT_DIR);
b3b94faa
DT
481 di->di_entries = cpu_to_be32(1);
482
71b86f56 483 gfs2_str2qstr(&str, "..");
c752666c
SW
484 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
485 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
b3b94faa 486
dbb7cae2 487 gfs2_inum_out(dip, dent);
7ecdb70a 488 dent->de_type = cpu_to_be16(DT_DIR);
b3b94faa 489
539e5d6b 490 gfs2_dinode_out(ip, di);
b3b94faa
DT
491
492 brelse(dibh);
493 }
494
495 error = gfs2_change_nlink(dip, +1);
496 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
497
498 gfs2_trans_end(sdp);
6dbd8224 499 if (dip->i_alloc->al_rgd)
b3b94faa
DT
500 gfs2_inplace_release(dip);
501 gfs2_quota_unlock(dip);
502 gfs2_alloc_put(dip);
503
504 gfs2_glock_dq_uninit_m(2, ghs);
505
b3b94faa
DT
506 d_instantiate(dentry, inode);
507 mark_inode_dirty(inode);
508
509 return 0;
510}
511
2286dbfa
SW
512/**
513 * gfs2_rmdiri - Remove a directory
514 * @dip: The parent directory of the directory to be removed
515 * @name: The name of the directory to be removed
516 * @ip: The GFS2 inode of the directory to be removed
517 *
518 * Assumes Glocks on dip and ip are held
519 *
520 * Returns: errno
521 */
522
523static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
524 struct gfs2_inode *ip)
525{
526 struct qstr dotname;
527 int error;
528
529 if (ip->i_entries != 2) {
530 if (gfs2_consist_inode(ip))
531 gfs2_dinode_print(ip);
532 return -EIO;
533 }
534
535 error = gfs2_dir_del(dip, name);
536 if (error)
537 return error;
538
539 error = gfs2_change_nlink(dip, -1);
540 if (error)
541 return error;
542
543 gfs2_str2qstr(&dotname, ".");
544 error = gfs2_dir_del(ip, &dotname);
545 if (error)
546 return error;
547
548 gfs2_str2qstr(&dotname, "..");
549 error = gfs2_dir_del(ip, &dotname);
550 if (error)
551 return error;
552
553 /* It looks odd, but it really should be done twice */
554 error = gfs2_change_nlink(ip, -1);
555 if (error)
556 return error;
557
558 error = gfs2_change_nlink(ip, -1);
559 if (error)
560 return error;
561
562 return error;
563}
564
b3b94faa
DT
565/**
566 * gfs2_rmdir - Remove a directory
567 * @dir: The parent directory of the directory to be removed
568 * @dentry: The dentry of the directory to remove
569 *
570 * Remove a directory. Call gfs2_rmdiri()
571 *
572 * Returns: errno
573 */
574
575static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
576{
feaa7bba
SW
577 struct gfs2_inode *dip = GFS2_I(dir);
578 struct gfs2_sbd *sdp = GFS2_SB(dir);
579 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
ddee7608
RC
580 struct gfs2_holder ghs[3];
581 struct gfs2_rgrpd *rgd;
582 struct gfs2_holder ri_gh;
b3b94faa
DT
583 int error;
584
ddee7608
RC
585 error = gfs2_rindex_hold(sdp, &ri_gh);
586 if (error)
587 return error;
b3b94faa
DT
588 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
589 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
590
dbb7cae2 591 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
ddee7608
RC
592 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
593
72dbf479
BP
594 error = gfs2_glock_nq(ghs); /* parent */
595 if (error)
596 goto out_parent;
597
598 error = gfs2_glock_nq(ghs + 1); /* child */
b3b94faa 599 if (error)
72dbf479
BP
600 goto out_child;
601
602 error = gfs2_glock_nq(ghs + 2); /* rgrp */
603 if (error)
604 goto out_rgrp;
b3b94faa
DT
605
606 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
607 if (error)
608 goto out_gunlock;
609
ad6203f2 610 if (ip->i_entries < 2) {
b3b94faa 611 if (gfs2_consist_inode(ip))
4cc14f0b 612 gfs2_dinode_print(ip);
b3b94faa
DT
613 error = -EIO;
614 goto out_gunlock;
615 }
ad6203f2 616 if (ip->i_entries > 2) {
b3b94faa
DT
617 error = -ENOTEMPTY;
618 goto out_gunlock;
619 }
620
feaa7bba 621 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
b3b94faa
DT
622 if (error)
623 goto out_gunlock;
624
feaa7bba 625 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
b3b94faa
DT
626
627 gfs2_trans_end(sdp);
628
a91ea69f 629out_gunlock:
72dbf479
BP
630 gfs2_glock_dq(ghs + 2);
631out_rgrp:
ddee7608 632 gfs2_holder_uninit(ghs + 2);
72dbf479
BP
633 gfs2_glock_dq(ghs + 1);
634out_child:
635 gfs2_holder_uninit(ghs + 1);
636 gfs2_glock_dq(ghs);
637out_parent:
638 gfs2_holder_uninit(ghs);
ddee7608 639 gfs2_glock_dq_uninit(&ri_gh);
b3b94faa
DT
640 return error;
641}
642
643/**
644 * gfs2_mknod - Make a special file
645 * @dir: The directory in which the special file will reside
646 * @dentry: The dentry of the special file
647 * @mode: The mode of the special file
648 * @rdev: The device specification of the special file
649 *
650 */
651
652static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
653 dev_t dev)
654{
e7f14f4d 655 struct gfs2_inode *dip = GFS2_I(dir);
feaa7bba 656 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
657 struct gfs2_holder ghs[2];
658 struct inode *inode;
b3b94faa
DT
659
660 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
661
e7f14f4d 662 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
7359a19c 663 if (IS_ERR(inode)) {
b3b94faa 664 gfs2_holder_uninit(ghs);
7359a19c 665 return PTR_ERR(inode);
b3b94faa
DT
666 }
667
b3b94faa 668 gfs2_trans_end(sdp);
6dbd8224 669 if (dip->i_alloc->al_rgd)
b3b94faa
DT
670 gfs2_inplace_release(dip);
671 gfs2_quota_unlock(dip);
672 gfs2_alloc_put(dip);
673
674 gfs2_glock_dq_uninit_m(2, ghs);
675
b3b94faa
DT
676 d_instantiate(dentry, inode);
677 mark_inode_dirty(inode);
678
679 return 0;
680}
681
0188d6c5
SW
682/*
683 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
684 * @this: move this
685 * @to: to here
686 *
687 * Follow @to back to the root and make sure we don't encounter @this
688 * Assumes we already hold the rename lock.
689 *
690 * Returns: errno
691 */
692
693static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
694{
695 struct inode *dir = &to->i_inode;
696 struct super_block *sb = dir->i_sb;
697 struct inode *tmp;
698 struct qstr dotdot;
699 int error = 0;
700
701 gfs2_str2qstr(&dotdot, "..");
702
703 igrab(dir);
704
705 for (;;) {
706 if (dir == &this->i_inode) {
707 error = -EINVAL;
708 break;
709 }
710 if (dir == sb->s_root->d_inode) {
711 error = 0;
712 break;
713 }
714
715 tmp = gfs2_lookupi(dir, &dotdot, 1);
716 if (IS_ERR(tmp)) {
717 error = PTR_ERR(tmp);
718 break;
719 }
720
721 iput(dir);
722 dir = tmp;
723 }
724
725 iput(dir);
726
727 return error;
728}
729
b3b94faa
DT
730/**
731 * gfs2_rename - Rename a file
732 * @odir: Parent directory of old file name
733 * @odentry: The old dentry of the file
734 * @ndir: Parent directory of new file name
735 * @ndentry: The new dentry of the file
736 *
737 * Returns: errno
738 */
739
740static int gfs2_rename(struct inode *odir, struct dentry *odentry,
741 struct inode *ndir, struct dentry *ndentry)
742{
feaa7bba
SW
743 struct gfs2_inode *odip = GFS2_I(odir);
744 struct gfs2_inode *ndip = GFS2_I(ndir);
745 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
b3b94faa 746 struct gfs2_inode *nip = NULL;
feaa7bba 747 struct gfs2_sbd *sdp = GFS2_SB(odir);
0188d6c5 748 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
ddee7608 749 struct gfs2_rgrpd *nrgd;
b3b94faa
DT
750 unsigned int num_gh;
751 int dir_rename = 0;
24b977b5 752 int alloc_required = 0;
b3b94faa
DT
753 unsigned int x;
754 int error;
755
b3b94faa 756 if (ndentry->d_inode) {
feaa7bba 757 nip = GFS2_I(ndentry->d_inode);
b3b94faa
DT
758 if (ip == nip)
759 return 0;
760 }
761
b3b94faa 762
0188d6c5
SW
763 if (odip != ndip) {
764 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
765 0, &r_gh);
b3b94faa
DT
766 if (error)
767 goto out;
768
0188d6c5
SW
769 if (S_ISDIR(ip->i_inode.i_mode)) {
770 dir_rename = 1;
771 /* don't move a dirctory into it's subdir */
772 error = gfs2_ok_to_move(ip, ndip);
773 if (error)
774 goto out_gunlock_r;
775 }
b3b94faa
DT
776 }
777
d9d1ca30 778 num_gh = 1;
b3b94faa 779 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
d9d1ca30
SW
780 if (odip != ndip) {
781 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
782 num_gh++;
783 }
784 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
785 num_gh++;
b3b94faa 786
d9d1ca30
SW
787 if (nip) {
788 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
789 num_gh++;
ddee7608
RC
790 /* grab the resource lock for unlink flag twiddling
791 * this is the case of the target file already existing
792 * so we unlink before doing the rename
793 */
dbb7cae2 794 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
ddee7608
RC
795 if (nrgd)
796 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
d9d1ca30 797 }
b3b94faa 798
72dbf479
BP
799 for (x = 0; x < num_gh; x++) {
800 error = gfs2_glock_nq(ghs + x);
801 if (error)
802 goto out_gunlock;
803 }
b3b94faa
DT
804
805 /* Check out the old directory */
806
807 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
808 if (error)
809 goto out_gunlock;
810
811 /* Check out the new directory */
812
813 if (nip) {
814 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
815 if (error)
816 goto out_gunlock;
817
b60623c2 818 if (S_ISDIR(nip->i_inode.i_mode)) {
ad6203f2 819 if (nip->i_entries < 2) {
b3b94faa 820 if (gfs2_consist_inode(nip))
4cc14f0b 821 gfs2_dinode_print(nip);
b3b94faa
DT
822 error = -EIO;
823 goto out_gunlock;
824 }
ad6203f2 825 if (nip->i_entries > 2) {
b3b94faa
DT
826 error = -ENOTEMPTY;
827 goto out_gunlock;
828 }
829 }
830 } else {
f58ba889 831 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
b3b94faa
DT
832 if (error)
833 goto out_gunlock;
834
dbb7cae2 835 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
b3b94faa
DT
836 switch (error) {
837 case -ENOENT:
838 error = 0;
839 break;
840 case 0:
841 error = -EEXIST;
842 default:
843 goto out_gunlock;
844 };
845
846 if (odip != ndip) {
4f56110a 847 if (!ndip->i_inode.i_nlink) {
b3b94faa
DT
848 error = -EINVAL;
849 goto out_gunlock;
850 }
ad6203f2 851 if (ndip->i_entries == (u32)-1) {
b3b94faa
DT
852 error = -EFBIG;
853 goto out_gunlock;
854 }
b60623c2 855 if (S_ISDIR(ip->i_inode.i_mode) &&
4f56110a 856 ndip->i_inode.i_nlink == (u32)-1) {
b3b94faa
DT
857 error = -EMLINK;
858 goto out_gunlock;
859 }
860 }
861 }
862
863 /* Check out the dir to be renamed */
864
865 if (dir_rename) {
f58ba889 866 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
b3b94faa
DT
867 if (error)
868 goto out_gunlock;
869 }
870
24b977b5
SW
871 if (nip == NULL)
872 alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
873 error = alloc_required;
c752666c 874 if (error < 0)
b3b94faa 875 goto out_gunlock;
c752666c 876 error = 0;
b3b94faa
DT
877
878 if (alloc_required) {
879 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
182fe5ab
CG
880 if (!al) {
881 error = -ENOMEM;
882 goto out_gunlock;
883 }
b3b94faa 884
d82661d9 885 error = gfs2_quota_lock_check(ndip);
b3b94faa
DT
886 if (error)
887 goto out_alloc;
888
b3b94faa
DT
889 al->al_requested = sdp->sd_max_dirres;
890
891 error = gfs2_inplace_reserve(ndip);
892 if (error)
893 goto out_gunlock_q;
894
fe1bdedc 895 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
bb8d8a6f 896 al->al_rgd->rd_length +
b3b94faa 897 4 * RES_DINODE + 4 * RES_LEAF +
87d21e07 898 RES_STATFS + RES_QUOTA + 4, 0);
b3b94faa
DT
899 if (error)
900 goto out_ipreserv;
901 } else {
902 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
87d21e07 903 5 * RES_LEAF + 4, 0);
b3b94faa
DT
904 if (error)
905 goto out_gunlock;
906 }
907
908 /* Remove the target file, if it exists */
909
910 if (nip) {
b60623c2 911 if (S_ISDIR(nip->i_inode.i_mode))
feaa7bba
SW
912 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
913 else {
914 error = gfs2_dir_del(ndip, &ndentry->d_name);
915 if (error)
916 goto out_end_trans;
87d21e07 917 error = gfs2_change_nlink(nip, -1);
feaa7bba 918 }
b3b94faa
DT
919 if (error)
920 goto out_end_trans;
921 }
922
923 if (dir_rename) {
924 struct qstr name;
71b86f56 925 gfs2_str2qstr(&name, "..");
b3b94faa
DT
926
927 error = gfs2_change_nlink(ndip, +1);
928 if (error)
929 goto out_end_trans;
930 error = gfs2_change_nlink(odip, -1);
931 if (error)
932 goto out_end_trans;
933
ffed8ab3 934 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
b3b94faa
DT
935 if (error)
936 goto out_end_trans;
937 } else {
938 struct buffer_head *dibh;
939 error = gfs2_meta_inode_buffer(ip, &dibh);
940 if (error)
941 goto out_end_trans;
4bd91ba1 942 ip->i_inode.i_ctime = CURRENT_TIME;
d4e9c4c3 943 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 944 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
945 brelse(dibh);
946 }
947
948 error = gfs2_dir_del(odip, &odentry->d_name);
949 if (error)
950 goto out_end_trans;
951
dbb7cae2 952 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
b3b94faa
DT
953 if (error)
954 goto out_end_trans;
955
feaa7bba 956out_end_trans:
b3b94faa 957 gfs2_trans_end(sdp);
feaa7bba 958out_ipreserv:
b3b94faa
DT
959 if (alloc_required)
960 gfs2_inplace_release(ndip);
feaa7bba 961out_gunlock_q:
b3b94faa
DT
962 if (alloc_required)
963 gfs2_quota_unlock(ndip);
feaa7bba 964out_alloc:
b3b94faa
DT
965 if (alloc_required)
966 gfs2_alloc_put(ndip);
feaa7bba 967out_gunlock:
72dbf479
BP
968 while (x--) {
969 gfs2_glock_dq(ghs + x);
b3b94faa 970 gfs2_holder_uninit(ghs + x);
72dbf479 971 }
feaa7bba 972out_gunlock_r:
0188d6c5 973 if (r_gh.gh_gl)
b3b94faa 974 gfs2_glock_dq_uninit(&r_gh);
feaa7bba 975out:
b3b94faa
DT
976 return error;
977}
978
536baf02 979/**
c177c2ac
AV
980 * gfs2_follow_link - Follow a symbolic link
981 * @dentry: The dentry of the link
982 * @nd: Data that we pass to vfs_follow_link()
536baf02 983 *
c177c2ac 984 * This can handle symlinks of any size.
536baf02 985 *
c177c2ac 986 * Returns: 0 on success or error code
536baf02
SW
987 */
988
c177c2ac 989static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
536baf02 990{
c177c2ac 991 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
536baf02
SW
992 struct gfs2_holder i_gh;
993 struct buffer_head *dibh;
a2e0f799 994 unsigned int x, size;
c177c2ac 995 char *buf;
536baf02
SW
996 int error;
997
998 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
999 error = gfs2_glock_nq(&i_gh);
1000 if (error) {
1001 gfs2_holder_uninit(&i_gh);
c177c2ac
AV
1002 nd_set_link(nd, ERR_PTR(error));
1003 return NULL;
536baf02
SW
1004 }
1005
a2e0f799
SW
1006 size = (unsigned int)i_size_read(&ip->i_inode);
1007 if (size == 0) {
536baf02 1008 gfs2_consist_inode(ip);
c177c2ac 1009 buf = ERR_PTR(-EIO);
536baf02
SW
1010 goto out;
1011 }
1012
1013 error = gfs2_meta_inode_buffer(ip, &dibh);
c177c2ac
AV
1014 if (error) {
1015 buf = ERR_PTR(error);
536baf02 1016 goto out;
536baf02
SW
1017 }
1018
a2e0f799 1019 x = size + 1;
c177c2ac
AV
1020 buf = kmalloc(x, GFP_NOFS);
1021 if (!buf)
1022 buf = ERR_PTR(-ENOMEM);
1023 else
1024 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
536baf02
SW
1025 brelse(dibh);
1026out:
1027 gfs2_glock_dq_uninit(&i_gh);
c177c2ac
AV
1028 nd_set_link(nd, buf);
1029 return NULL;
b3b94faa
DT
1030}
1031
c177c2ac 1032static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
b3b94faa 1033{
c177c2ac
AV
1034 char *s = nd_get_link(nd);
1035 if (!IS_ERR(s))
1036 kfree(s);
b3b94faa
DT
1037}
1038
1039/**
1040 * gfs2_permission -
1041 * @inode:
1042 * @mask:
1043 * @nd: passed from Linux VFS, ignored by us
1044 *
300c7d75
SW
1045 * This may be called from the VFS directly, or from within GFS2 with the
1046 * inode locked, so we look to see if the glock is already locked and only
1047 * lock the glock if its not already been done.
1048 *
b3b94faa
DT
1049 * Returns: errno
1050 */
1051
f58ba889 1052int gfs2_permission(struct inode *inode, int mask)
b3b94faa 1053{
feaa7bba 1054 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
1055 struct gfs2_holder i_gh;
1056 int error;
300c7d75 1057 int unlock = 0;
b3b94faa 1058
7afd88d9 1059 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
300c7d75
SW
1060 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1061 if (error)
1062 return error;
1063 unlock = 1;
1064 }
b3b94faa 1065
f58ba889
MS
1066 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1067 error = -EACCES;
1068 else
1069 error = generic_permission(inode, mask, gfs2_check_acl);
300c7d75 1070 if (unlock)
b3b94faa 1071 gfs2_glock_dq_uninit(&i_gh);
b3b94faa
DT
1072
1073 return error;
1074}
1075
b3b94faa
DT
1076static int setattr_chown(struct inode *inode, struct iattr *attr)
1077{
feaa7bba
SW
1078 struct gfs2_inode *ip = GFS2_I(inode);
1079 struct gfs2_sbd *sdp = GFS2_SB(inode);
b3b94faa 1080 struct buffer_head *dibh;
cd915493 1081 u32 ouid, ogid, nuid, ngid;
b3b94faa
DT
1082 int error;
1083
2933f925
SW
1084 ouid = inode->i_uid;
1085 ogid = inode->i_gid;
b3b94faa
DT
1086 nuid = attr->ia_uid;
1087 ngid = attr->ia_gid;
1088
1089 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1090 ouid = nuid = NO_QUOTA_CHANGE;
1091 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1092 ogid = ngid = NO_QUOTA_CHANGE;
1093
182fe5ab
CG
1094 if (!gfs2_alloc_get(ip))
1095 return -ENOMEM;
b3b94faa
DT
1096
1097 error = gfs2_quota_lock(ip, nuid, ngid);
1098 if (error)
1099 goto out_alloc;
1100
1101 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1102 error = gfs2_quota_check(ip, nuid, ngid);
1103 if (error)
1104 goto out_gunlock_q;
1105 }
1106
1107 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1108 if (error)
1109 goto out_gunlock_q;
1110
1111 error = gfs2_meta_inode_buffer(ip, &dibh);
1112 if (error)
1113 goto out_end_trans;
1114
1025774c
CH
1115 if ((attr->ia_valid & ATTR_SIZE) &&
1116 attr->ia_size != i_size_read(inode)) {
1117 int error;
1118
1119 error = vmtruncate(inode, attr->ia_size);
1120 gfs2_assert_warn(sdp, !error);
1121 }
1122
1123 setattr_copy(inode, attr);
1124 mark_inode_dirty(inode);
b3b94faa 1125
d4e9c4c3 1126 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 1127 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
1128 brelse(dibh);
1129
1130 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
77658aad
SW
1131 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1132 gfs2_quota_change(ip, -blocks, ouid, ogid);
1133 gfs2_quota_change(ip, blocks, nuid, ngid);
b3b94faa
DT
1134 }
1135
a91ea69f 1136out_end_trans:
b3b94faa 1137 gfs2_trans_end(sdp);
a91ea69f 1138out_gunlock_q:
b3b94faa 1139 gfs2_quota_unlock(ip);
a91ea69f 1140out_alloc:
b3b94faa 1141 gfs2_alloc_put(ip);
b3b94faa
DT
1142 return error;
1143}
1144
1145/**
1146 * gfs2_setattr - Change attributes on an inode
1147 * @dentry: The dentry which is changing
1148 * @attr: The structure describing the change
1149 *
1150 * The VFS layer wants to change one or more of an inodes attributes. Write
1151 * that change out to disk.
1152 *
1153 * Returns: errno
1154 */
1155
1156static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1157{
1158 struct inode *inode = dentry->d_inode;
feaa7bba 1159 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
1160 struct gfs2_holder i_gh;
1161 int error;
1162
b3b94faa
DT
1163 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1164 if (error)
1165 return error;
1166
1167 error = -EPERM;
1168 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1169 goto out;
1170
1171 error = inode_change_ok(inode, attr);
1172 if (error)
1173 goto out;
1174
1175 if (attr->ia_valid & ATTR_SIZE)
ff8f33c8 1176 error = gfs2_setattr_size(inode, attr->ia_size);
b3b94faa
DT
1177 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1178 error = setattr_chown(inode, attr);
1179 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1180 error = gfs2_acl_chmod(ip, attr);
1181 else
1182 error = gfs2_setattr_simple(ip, attr);
1183
a91ea69f 1184out:
b3b94faa 1185 gfs2_glock_dq_uninit(&i_gh);
b3b94faa
DT
1186 if (!error)
1187 mark_inode_dirty(inode);
b3b94faa
DT
1188 return error;
1189}
1190
1191/**
1192 * gfs2_getattr - Read out an inode's attributes
26c1a574 1193 * @mnt: The vfsmount the inode is being accessed from
b3b94faa
DT
1194 * @dentry: The dentry to stat
1195 * @stat: The inode's stats
1196 *
dcf3dd85
SW
1197 * This may be called from the VFS directly, or from within GFS2 with the
1198 * inode locked, so we look to see if the glock is already locked and only
1199 * lock the glock if its not already been done. Note that its the NFS
1200 * readdirplus operation which causes this to be called (from filldir)
1201 * with the glock already held.
1202 *
b3b94faa
DT
1203 * Returns: errno
1204 */
1205
1206static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1207 struct kstat *stat)
1208{
1209 struct inode *inode = dentry->d_inode;
feaa7bba 1210 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
1211 struct gfs2_holder gh;
1212 int error;
dcf3dd85 1213 int unlock = 0;
b3b94faa 1214
7afd88d9 1215 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
dcf3dd85
SW
1216 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1217 if (error)
1218 return error;
1219 unlock = 1;
b3b94faa
DT
1220 }
1221
dcf3dd85 1222 generic_fillattr(inode, stat);
d7c103d0 1223 if (unlock)
dcf3dd85
SW
1224 gfs2_glock_dq_uninit(&gh);
1225
1226 return 0;
b3b94faa
DT
1227}
1228
1229static int gfs2_setxattr(struct dentry *dentry, const char *name,
1230 const void *data, size_t size, int flags)
1231{
feaa7bba 1232 struct inode *inode = dentry->d_inode;
40b78a32
SW
1233 struct gfs2_inode *ip = GFS2_I(inode);
1234 struct gfs2_holder gh;
1235 int ret;
b3b94faa 1236
40b78a32
SW
1237 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1238 ret = gfs2_glock_nq(&gh);
1239 if (ret == 0) {
1240 ret = generic_setxattr(dentry, name, data, size, flags);
1241 gfs2_glock_dq(&gh);
1242 }
1243 gfs2_holder_uninit(&gh);
1244 return ret;
b3b94faa
DT
1245}
1246
1247static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1248 void *data, size_t size)
1249{
40b78a32
SW
1250 struct inode *inode = dentry->d_inode;
1251 struct gfs2_inode *ip = GFS2_I(inode);
1252 struct gfs2_holder gh;
1253 int ret;
b3b94faa 1254
40b78a32
SW
1255 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1256 ret = gfs2_glock_nq(&gh);
1257 if (ret == 0) {
1258 ret = generic_getxattr(dentry, name, data, size);
1259 gfs2_glock_dq(&gh);
1260 }
1261 gfs2_holder_uninit(&gh);
1262 return ret;
b3b94faa
DT
1263}
1264
1265static int gfs2_removexattr(struct dentry *dentry, const char *name)
1266{
40b78a32
SW
1267 struct inode *inode = dentry->d_inode;
1268 struct gfs2_inode *ip = GFS2_I(inode);
1269 struct gfs2_holder gh;
1270 int ret;
b3b94faa 1271
40b78a32
SW
1272 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1273 ret = gfs2_glock_nq(&gh);
1274 if (ret == 0) {
1275 ret = generic_removexattr(dentry, name);
1276 gfs2_glock_dq(&gh);
1277 }
1278 gfs2_holder_uninit(&gh);
1279 return ret;
b3b94faa
DT
1280}
1281
3921120e
BM
1282static void empty_write_end(struct page *page, unsigned from,
1283 unsigned to)
1284{
1285 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
1286
1287 page_zero_new_buffers(page, from, to);
1288 flush_dcache_page(page);
1289 mark_page_accessed(page);
1290
1291 if (!gfs2_is_writeback(ip))
1292 gfs2_page_add_databufs(ip, page, from, to);
1293
1294 block_commit_write(page, from, to);
1295}
1296
1297
1298static int write_empty_blocks(struct page *page, unsigned from, unsigned to)
1299{
1300 unsigned start, end, next;
1301 struct buffer_head *bh, *head;
1302 int error;
1303
1304 if (!page_has_buffers(page)) {
1305 error = block_prepare_write(page, from, to, gfs2_block_map);
1306 if (unlikely(error))
1307 return error;
1308
1309 empty_write_end(page, from, to);
1310 return 0;
1311 }
1312
1313 bh = head = page_buffers(page);
1314 next = end = 0;
1315 while (next < from) {
1316 next += bh->b_size;
1317 bh = bh->b_this_page;
1318 }
1319 start = next;
1320 do {
1321 next += bh->b_size;
1322 if (buffer_mapped(bh)) {
1323 if (end) {
1324 error = block_prepare_write(page, start, end,
1325 gfs2_block_map);
1326 if (unlikely(error))
1327 return error;
1328 empty_write_end(page, start, end);
1329 end = 0;
1330 }
1331 start = next;
1332 }
1333 else
1334 end = next;
1335 bh = bh->b_this_page;
1336 } while (next < to);
1337
1338 if (end) {
1339 error = block_prepare_write(page, start, end, gfs2_block_map);
1340 if (unlikely(error))
1341 return error;
1342 empty_write_end(page, start, end);
1343 }
1344
1345 return 0;
1346}
1347
1348static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
1349 int mode)
1350{
1351 struct gfs2_inode *ip = GFS2_I(inode);
1352 struct buffer_head *dibh;
1353 int error;
1354 u64 start = offset >> PAGE_CACHE_SHIFT;
1355 unsigned int start_offset = offset & ~PAGE_CACHE_MASK;
1356 u64 end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
1357 pgoff_t curr;
1358 struct page *page;
1359 unsigned int end_offset = (offset + len) & ~PAGE_CACHE_MASK;
1360 unsigned int from, to;
1361
1362 if (!end_offset)
1363 end_offset = PAGE_CACHE_SIZE;
1364
1365 error = gfs2_meta_inode_buffer(ip, &dibh);
1366 if (unlikely(error))
1367 goto out;
1368
1369 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1370
1371 if (gfs2_is_stuffed(ip)) {
1372 error = gfs2_unstuff_dinode(ip, NULL);
1373 if (unlikely(error))
1374 goto out;
1375 }
1376
1377 curr = start;
1378 offset = start << PAGE_CACHE_SHIFT;
1379 from = start_offset;
1380 to = PAGE_CACHE_SIZE;
1381 while (curr <= end) {
1382 page = grab_cache_page_write_begin(inode->i_mapping, curr,
1383 AOP_FLAG_NOFS);
1384 if (unlikely(!page)) {
1385 error = -ENOMEM;
1386 goto out;
1387 }
1388
1389 if (curr == end)
1390 to = end_offset;
1391 error = write_empty_blocks(page, from, to);
1392 if (!error && offset + to > inode->i_size &&
1393 !(mode & FALLOC_FL_KEEP_SIZE)) {
1394 i_size_write(inode, offset + to);
1395 }
1396 unlock_page(page);
1397 page_cache_release(page);
1398 if (error)
1399 goto out;
1400 curr++;
1401 offset += PAGE_CACHE_SIZE;
1402 from = 0;
1403 }
1404
1405 gfs2_dinode_out(ip, dibh->b_data);
1406 mark_inode_dirty(inode);
1407
1408 brelse(dibh);
1409
1410out:
1411 return error;
1412}
1413
1414static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
1415 unsigned int *data_blocks, unsigned int *ind_blocks)
1416{
1417 const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1418 unsigned int max_blocks = ip->i_alloc->al_rgd->rd_free_clone;
1419 unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
1420
1421 for (tmp = max_data; tmp > sdp->sd_diptrs;) {
1422 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1423 max_data -= tmp;
1424 }
1425 /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
fe08d5a8 1426 so it might end up with fewer data blocks */
3921120e
BM
1427 if (max_data <= *data_blocks)
1428 return;
1429 *data_blocks = max_data;
1430 *ind_blocks = max_blocks - max_data;
1431 *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
1432 if (*len > max) {
1433 *len = max;
1434 gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
1435 }
1436}
1437
1438static long gfs2_fallocate(struct inode *inode, int mode, loff_t offset,
1439 loff_t len)
1440{
1441 struct gfs2_sbd *sdp = GFS2_SB(inode);
1442 struct gfs2_inode *ip = GFS2_I(inode);
1443 unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
1444 loff_t bytes, max_bytes;
1445 struct gfs2_alloc *al;
1446 int error;
1447 loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
1448 next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
1449
1450 offset = (offset >> sdp->sd_sb.sb_bsize_shift) <<
1451 sdp->sd_sb.sb_bsize_shift;
1452
1453 len = next - offset;
1454 bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
1455 if (!bytes)
1456 bytes = UINT_MAX;
1457
1458 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
1459 error = gfs2_glock_nq(&ip->i_gh);
1460 if (unlikely(error))
1461 goto out_uninit;
1462
1463 if (!gfs2_write_alloc_required(ip, offset, len))
1464 goto out_unlock;
1465
1466 while (len > 0) {
1467 if (len < bytes)
1468 bytes = len;
1469 al = gfs2_alloc_get(ip);
1470 if (!al) {
1471 error = -ENOMEM;
1472 goto out_unlock;
1473 }
1474
1475 error = gfs2_quota_lock_check(ip);
1476 if (error)
1477 goto out_alloc_put;
1478
1479retry:
1480 gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
1481
1482 al->al_requested = data_blocks + ind_blocks;
1483 error = gfs2_inplace_reserve(ip);
1484 if (error) {
1485 if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
1486 bytes >>= 1;
1487 goto retry;
1488 }
1489 goto out_qunlock;
1490 }
1491 max_bytes = bytes;
1492 calc_max_reserv(ip, len, &max_bytes, &data_blocks, &ind_blocks);
1493 al->al_requested = data_blocks + ind_blocks;
1494
1495 rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
1496 RES_RG_HDR + ip->i_alloc->al_rgd->rd_length;
1497 if (gfs2_is_jdata(ip))
1498 rblocks += data_blocks ? data_blocks : 1;
1499
1500 error = gfs2_trans_begin(sdp, rblocks,
1501 PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
1502 if (error)
1503 goto out_trans_fail;
1504
1505 error = fallocate_chunk(inode, offset, max_bytes, mode);
1506 gfs2_trans_end(sdp);
1507
1508 if (error)
1509 goto out_trans_fail;
1510
1511 len -= max_bytes;
1512 offset += max_bytes;
1513 gfs2_inplace_release(ip);
1514 gfs2_quota_unlock(ip);
1515 gfs2_alloc_put(ip);
1516 }
1517 goto out_unlock;
1518
1519out_trans_fail:
1520 gfs2_inplace_release(ip);
1521out_qunlock:
1522 gfs2_quota_unlock(ip);
1523out_alloc_put:
1524 gfs2_alloc_put(ip);
1525out_unlock:
1526 gfs2_glock_dq(&ip->i_gh);
1527out_uninit:
1528 gfs2_holder_uninit(&ip->i_gh);
1529 return error;
1530}
1531
1532
e9079cce
SW
1533static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1534 u64 start, u64 len)
1535{
1536 struct gfs2_inode *ip = GFS2_I(inode);
1537 struct gfs2_holder gh;
1538 int ret;
1539
1540 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1541 if (ret)
1542 return ret;
1543
1544 mutex_lock(&inode->i_mutex);
1545
1546 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1547 if (ret)
1548 goto out;
1549
1550 if (gfs2_is_stuffed(ip)) {
1551 u64 phys = ip->i_no_addr << inode->i_blkbits;
1552 u64 size = i_size_read(inode);
1553 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1554 FIEMAP_EXTENT_DATA_INLINE;
1555 phys += sizeof(struct gfs2_dinode);
1556 phys += start;
1557 if (start + len > size)
1558 len = size - start;
1559 if (start < size)
1560 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1561 len, flags);
1562 if (ret == 1)
1563 ret = 0;
1564 } else {
1565 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1566 gfs2_block_map);
1567 }
1568
1569 gfs2_glock_dq_uninit(&gh);
1570out:
1571 mutex_unlock(&inode->i_mutex);
1572 return ret;
1573}
1574
92e1d5be 1575const struct inode_operations gfs2_file_iops = {
e6305c43 1576 .permission = gfs2_permission,
b3b94faa
DT
1577 .setattr = gfs2_setattr,
1578 .getattr = gfs2_getattr,
1579 .setxattr = gfs2_setxattr,
1580 .getxattr = gfs2_getxattr,
1581 .listxattr = gfs2_listxattr,
1582 .removexattr = gfs2_removexattr,
3921120e 1583 .fallocate = gfs2_fallocate,
e9079cce 1584 .fiemap = gfs2_fiemap,
b3b94faa
DT
1585};
1586
92e1d5be 1587const struct inode_operations gfs2_dir_iops = {
b3b94faa
DT
1588 .create = gfs2_create,
1589 .lookup = gfs2_lookup,
1590 .link = gfs2_link,
1591 .unlink = gfs2_unlink,
1592 .symlink = gfs2_symlink,
1593 .mkdir = gfs2_mkdir,
1594 .rmdir = gfs2_rmdir,
1595 .mknod = gfs2_mknod,
1596 .rename = gfs2_rename,
e6305c43 1597 .permission = gfs2_permission,
b3b94faa
DT
1598 .setattr = gfs2_setattr,
1599 .getattr = gfs2_getattr,
1600 .setxattr = gfs2_setxattr,
1601 .getxattr = gfs2_getxattr,
1602 .listxattr = gfs2_listxattr,
1603 .removexattr = gfs2_removexattr,
e9079cce 1604 .fiemap = gfs2_fiemap,
b3b94faa
DT
1605};
1606
92e1d5be 1607const struct inode_operations gfs2_symlink_iops = {
c177c2ac 1608 .readlink = generic_readlink,
b3b94faa 1609 .follow_link = gfs2_follow_link,
c177c2ac 1610 .put_link = gfs2_put_link,
e6305c43 1611 .permission = gfs2_permission,
b3b94faa
DT
1612 .setattr = gfs2_setattr,
1613 .getattr = gfs2_getattr,
1614 .setxattr = gfs2_setxattr,
1615 .getxattr = gfs2_getxattr,
1616 .listxattr = gfs2_listxattr,
1617 .removexattr = gfs2_removexattr,
e9079cce 1618 .fiemap = gfs2_fiemap,
b3b94faa
DT
1619};
1620