Convert ERR_PTR(PTR_ERR(p)) instances to ERR_CAST(p)
[GitHub/mt8127/android_kernel_alcatel_ttab.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 63 gfs2_trans_end(sdp);
6dbd8224 64 if (dip->i_alloc->al_rgd)
b3b94faa
DT
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 ||
afd0942d 72 (nd && (nd->intent.open.flags & O_EXCL))) {
b3b94faa 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))
e231c2ee 114 return ERR_CAST(inode);
b3b94faa 115
9656b2c1
SW
116 if (inode) {
117 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
118 struct gfs2_holder gh;
119 int error;
120 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
121 if (error) {
122 iput(inode);
123 return ERR_PTR(error);
124 }
125 gfs2_glock_dq_uninit(&gh);
b3b94faa 126 return d_splice_alias(inode, dentry);
9656b2c1 127 }
b3b94faa
DT
128 d_add(dentry, inode);
129
130 return NULL;
131}
132
133/**
134 * gfs2_link - Link to a file
135 * @old_dentry: The inode to link
136 * @dir: Add link to this directory
137 * @dentry: The name of the link
138 *
139 * Link the inode in "old_dentry" into the directory "dir" with the
140 * name in "dentry".
141 *
142 * Returns: errno
143 */
144
145static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
146 struct dentry *dentry)
147{
feaa7bba
SW
148 struct gfs2_inode *dip = GFS2_I(dir);
149 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa 150 struct inode *inode = old_dentry->d_inode;
feaa7bba 151 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
152 struct gfs2_holder ghs[2];
153 int alloc_required;
154 int error;
155
b60623c2 156 if (S_ISDIR(inode->i_mode))
b3b94faa
DT
157 return -EPERM;
158
159 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
160 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
161
162 error = gfs2_glock_nq_m(2, ghs);
163 if (error)
164 goto out;
165
faf450ef 166 error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
b3b94faa
DT
167 if (error)
168 goto out_gunlock;
169
dbb7cae2 170 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
b3b94faa
DT
171 switch (error) {
172 case -ENOENT:
173 break;
174 case 0:
175 error = -EEXIST;
176 default:
177 goto out_gunlock;
178 }
179
180 error = -EINVAL;
4f56110a 181 if (!dip->i_inode.i_nlink)
b3b94faa
DT
182 goto out_gunlock;
183 error = -EFBIG;
cd915493 184 if (dip->i_di.di_entries == (u32)-1)
b3b94faa
DT
185 goto out_gunlock;
186 error = -EPERM;
187 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
188 goto out_gunlock;
189 error = -EINVAL;
4f56110a 190 if (!ip->i_inode.i_nlink)
b3b94faa
DT
191 goto out_gunlock;
192 error = -EMLINK;
4f56110a 193 if (ip->i_inode.i_nlink == (u32)-1)
b3b94faa
DT
194 goto out_gunlock;
195
c752666c
SW
196 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
197 if (error < 0)
b3b94faa 198 goto out_gunlock;
c752666c 199 error = 0;
b3b94faa
DT
200
201 if (alloc_required) {
202 struct gfs2_alloc *al = gfs2_alloc_get(dip);
203
204 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
205 if (error)
206 goto out_alloc;
207
2933f925 208 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
b3b94faa
DT
209 if (error)
210 goto out_gunlock_q;
211
212 al->al_requested = sdp->sd_max_dirres;
213
214 error = gfs2_inplace_reserve(dip);
215 if (error)
216 goto out_gunlock_q;
217
1b50259b 218 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
bb8d8a6f 219 al->al_rgd->rd_length +
b3b94faa
DT
220 2 * RES_DINODE + RES_STATFS +
221 RES_QUOTA, 0);
222 if (error)
223 goto out_ipres;
224 } else {
225 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
226 if (error)
227 goto out_ipres;
228 }
229
dbb7cae2 230 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
b3b94faa
DT
231 if (error)
232 goto out_end_trans;
233
234 error = gfs2_change_nlink(ip, +1);
235
feaa7bba 236out_end_trans:
b3b94faa 237 gfs2_trans_end(sdp);
feaa7bba 238out_ipres:
b3b94faa
DT
239 if (alloc_required)
240 gfs2_inplace_release(dip);
feaa7bba 241out_gunlock_q:
b3b94faa
DT
242 if (alloc_required)
243 gfs2_quota_unlock(dip);
feaa7bba 244out_alloc:
b3b94faa
DT
245 if (alloc_required)
246 gfs2_alloc_put(dip);
feaa7bba 247out_gunlock:
b3b94faa 248 gfs2_glock_dq_m(2, ghs);
feaa7bba 249out:
b3b94faa
DT
250 gfs2_holder_uninit(ghs);
251 gfs2_holder_uninit(ghs + 1);
b3b94faa 252 if (!error) {
29937ac6 253 atomic_inc(&inode->i_count);
b3b94faa
DT
254 d_instantiate(dentry, inode);
255 mark_inode_dirty(inode);
256 }
b3b94faa
DT
257 return error;
258}
259
260/**
261 * gfs2_unlink - Unlink a file
262 * @dir: The inode of the directory containing the file to unlink
263 * @dentry: The file itself
264 *
265 * Unlink a file. Call gfs2_unlinki()
266 *
267 * Returns: errno
268 */
269
270static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
271{
feaa7bba
SW
272 struct gfs2_inode *dip = GFS2_I(dir);
273 struct gfs2_sbd *sdp = GFS2_SB(dir);
274 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
ddee7608
RC
275 struct gfs2_holder ghs[3];
276 struct gfs2_rgrpd *rgd;
277 struct gfs2_holder ri_gh;
b3b94faa
DT
278 int error;
279
ddee7608
RC
280 error = gfs2_rindex_hold(sdp, &ri_gh);
281 if (error)
282 return error;
283
b3b94faa 284 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
ddee7608 285 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
b3b94faa 286
dbb7cae2 287 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
ddee7608
RC
288 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
289
290
8497a46e 291 error = gfs2_glock_nq(ghs); /* parent */
b3b94faa 292 if (error)
8497a46e
SW
293 goto out_parent;
294
295 error = gfs2_glock_nq(ghs + 1); /* child */
296 if (error)
297 goto out_child;
298
299 error = gfs2_glock_nq(ghs + 2); /* rgrp */
300 if (error)
301 goto out_rgrp;
b3b94faa
DT
302
303 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
304 if (error)
8497a46e 305 goto out_rgrp;
b3b94faa 306
feaa7bba 307 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
b3b94faa 308 if (error)
8497a46e 309 goto out_rgrp;
b3b94faa 310
feaa7bba
SW
311 error = gfs2_dir_del(dip, &dentry->d_name);
312 if (error)
313 goto out_end_trans;
b3b94faa 314
feaa7bba 315 error = gfs2_change_nlink(ip, -1);
b3b94faa 316
feaa7bba
SW
317out_end_trans:
318 gfs2_trans_end(sdp);
8497a46e
SW
319 gfs2_glock_dq(ghs + 2);
320out_rgrp:
ddee7608 321 gfs2_holder_uninit(ghs + 2);
8497a46e
SW
322 gfs2_glock_dq(ghs + 1);
323out_child:
324 gfs2_holder_uninit(ghs + 1);
325 gfs2_glock_dq(ghs);
326out_parent:
327 gfs2_holder_uninit(ghs);
ddee7608 328 gfs2_glock_dq_uninit(&ri_gh);
b3b94faa
DT
329 return error;
330}
331
332/**
333 * gfs2_symlink - Create a symlink
334 * @dir: The directory to create the symlink in
335 * @dentry: The dentry to put the symlink in
336 * @symname: The thing which the link points to
337 *
338 * Returns: errno
339 */
340
341static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
342 const char *symname)
343{
feaa7bba
SW
344 struct gfs2_inode *dip = GFS2_I(dir), *ip;
345 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
346 struct gfs2_holder ghs[2];
347 struct inode *inode;
348 struct buffer_head *dibh;
349 int size;
350 int error;
351
b3b94faa
DT
352 /* Must be stuffed with a null terminator for gfs2_follow_link() */
353 size = strlen(symname);
354 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
355 return -ENAMETOOLONG;
356
357 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
358
e7f14f4d 359 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
7359a19c 360 if (IS_ERR(inode)) {
b3b94faa 361 gfs2_holder_uninit(ghs);
7359a19c 362 return PTR_ERR(inode);
b3b94faa
DT
363 }
364
5c676f6d 365 ip = ghs[1].gh_gl->gl_object;
b3b94faa
DT
366
367 ip->i_di.di_size = size;
368
369 error = gfs2_meta_inode_buffer(ip, &dibh);
370
371 if (!gfs2_assert_withdraw(sdp, !error)) {
539e5d6b 372 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
373 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
374 size);
375 brelse(dibh);
376 }
377
378 gfs2_trans_end(sdp);
6dbd8224 379 if (dip->i_alloc->al_rgd)
b3b94faa
DT
380 gfs2_inplace_release(dip);
381 gfs2_quota_unlock(dip);
382 gfs2_alloc_put(dip);
383
384 gfs2_glock_dq_uninit_m(2, ghs);
385
b3b94faa
DT
386 d_instantiate(dentry, inode);
387 mark_inode_dirty(inode);
388
389 return 0;
390}
391
392/**
393 * gfs2_mkdir - Make a directory
394 * @dir: The parent directory of the new one
395 * @dentry: The dentry of the new directory
396 * @mode: The mode of the new directory
397 *
398 * Returns: errno
399 */
400
401static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
402{
feaa7bba
SW
403 struct gfs2_inode *dip = GFS2_I(dir), *ip;
404 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
405 struct gfs2_holder ghs[2];
406 struct inode *inode;
407 struct buffer_head *dibh;
408 int error;
409
b3b94faa
DT
410 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
411
e7f14f4d 412 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
7359a19c 413 if (IS_ERR(inode)) {
b3b94faa 414 gfs2_holder_uninit(ghs);
7359a19c 415 return PTR_ERR(inode);
b3b94faa
DT
416 }
417
5c676f6d 418 ip = ghs[1].gh_gl->gl_object;
b3b94faa 419
4f56110a 420 ip->i_inode.i_nlink = 2;
b3b94faa
DT
421 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
422 ip->i_di.di_flags |= GFS2_DIF_JDATA;
b3b94faa
DT
423 ip->i_di.di_entries = 2;
424
425 error = gfs2_meta_inode_buffer(ip, &dibh);
426
427 if (!gfs2_assert_withdraw(sdp, !error)) {
428 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
c752666c 429 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
71b86f56 430 struct qstr str;
b3b94faa 431
71b86f56 432 gfs2_str2qstr(&str, ".");
c752666c
SW
433 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
434 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
b3b94faa 435 dent->de_inum = di->di_num; /* already GFS2 endian */
7ecdb70a 436 dent->de_type = cpu_to_be16(DT_DIR);
b3b94faa
DT
437 di->di_entries = cpu_to_be32(1);
438
71b86f56 439 gfs2_str2qstr(&str, "..");
c752666c
SW
440 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
441 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
b3b94faa 442
dbb7cae2 443 gfs2_inum_out(dip, dent);
7ecdb70a 444 dent->de_type = cpu_to_be16(DT_DIR);
b3b94faa 445
539e5d6b 446 gfs2_dinode_out(ip, di);
b3b94faa
DT
447
448 brelse(dibh);
449 }
450
451 error = gfs2_change_nlink(dip, +1);
452 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
453
454 gfs2_trans_end(sdp);
6dbd8224 455 if (dip->i_alloc->al_rgd)
b3b94faa
DT
456 gfs2_inplace_release(dip);
457 gfs2_quota_unlock(dip);
458 gfs2_alloc_put(dip);
459
460 gfs2_glock_dq_uninit_m(2, ghs);
461
b3b94faa
DT
462 d_instantiate(dentry, inode);
463 mark_inode_dirty(inode);
464
465 return 0;
466}
467
468/**
469 * gfs2_rmdir - Remove a directory
470 * @dir: The parent directory of the directory to be removed
471 * @dentry: The dentry of the directory to remove
472 *
473 * Remove a directory. Call gfs2_rmdiri()
474 *
475 * Returns: errno
476 */
477
478static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
479{
feaa7bba
SW
480 struct gfs2_inode *dip = GFS2_I(dir);
481 struct gfs2_sbd *sdp = GFS2_SB(dir);
482 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
ddee7608
RC
483 struct gfs2_holder ghs[3];
484 struct gfs2_rgrpd *rgd;
485 struct gfs2_holder ri_gh;
b3b94faa
DT
486 int error;
487
ddee7608
RC
488
489 error = gfs2_rindex_hold(sdp, &ri_gh);
490 if (error)
491 return error;
b3b94faa
DT
492 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
493 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
494
dbb7cae2 495 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
ddee7608
RC
496 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
497
498 error = gfs2_glock_nq_m(3, ghs);
b3b94faa
DT
499 if (error)
500 goto out;
501
502 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
503 if (error)
504 goto out_gunlock;
505
506 if (ip->i_di.di_entries < 2) {
507 if (gfs2_consist_inode(ip))
4cc14f0b 508 gfs2_dinode_print(ip);
b3b94faa
DT
509 error = -EIO;
510 goto out_gunlock;
511 }
512 if (ip->i_di.di_entries > 2) {
513 error = -ENOTEMPTY;
514 goto out_gunlock;
515 }
516
feaa7bba 517 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
b3b94faa
DT
518 if (error)
519 goto out_gunlock;
520
feaa7bba 521 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
b3b94faa
DT
522
523 gfs2_trans_end(sdp);
524
a91ea69f 525out_gunlock:
ddee7608 526 gfs2_glock_dq_m(3, ghs);
a91ea69f 527out:
b3b94faa
DT
528 gfs2_holder_uninit(ghs);
529 gfs2_holder_uninit(ghs + 1);
ddee7608
RC
530 gfs2_holder_uninit(ghs + 2);
531 gfs2_glock_dq_uninit(&ri_gh);
b3b94faa
DT
532 return error;
533}
534
535/**
536 * gfs2_mknod - Make a special file
537 * @dir: The directory in which the special file will reside
538 * @dentry: The dentry of the special file
539 * @mode: The mode of the special file
540 * @rdev: The device specification of the special file
541 *
542 */
543
544static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
545 dev_t dev)
546{
e7f14f4d 547 struct gfs2_inode *dip = GFS2_I(dir);
feaa7bba 548 struct gfs2_sbd *sdp = GFS2_SB(dir);
b3b94faa
DT
549 struct gfs2_holder ghs[2];
550 struct inode *inode;
b3b94faa
DT
551
552 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
553
e7f14f4d 554 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
7359a19c 555 if (IS_ERR(inode)) {
b3b94faa 556 gfs2_holder_uninit(ghs);
7359a19c 557 return PTR_ERR(inode);
b3b94faa
DT
558 }
559
b3b94faa 560 gfs2_trans_end(sdp);
6dbd8224 561 if (dip->i_alloc->al_rgd)
b3b94faa
DT
562 gfs2_inplace_release(dip);
563 gfs2_quota_unlock(dip);
564 gfs2_alloc_put(dip);
565
566 gfs2_glock_dq_uninit_m(2, ghs);
567
b3b94faa
DT
568 d_instantiate(dentry, inode);
569 mark_inode_dirty(inode);
570
571 return 0;
572}
573
574/**
575 * gfs2_rename - Rename a file
576 * @odir: Parent directory of old file name
577 * @odentry: The old dentry of the file
578 * @ndir: Parent directory of new file name
579 * @ndentry: The new dentry of the file
580 *
581 * Returns: errno
582 */
583
584static int gfs2_rename(struct inode *odir, struct dentry *odentry,
585 struct inode *ndir, struct dentry *ndentry)
586{
feaa7bba
SW
587 struct gfs2_inode *odip = GFS2_I(odir);
588 struct gfs2_inode *ndip = GFS2_I(ndir);
589 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
b3b94faa 590 struct gfs2_inode *nip = NULL;
feaa7bba 591 struct gfs2_sbd *sdp = GFS2_SB(odir);
ddee7608
RC
592 struct gfs2_holder ghs[5], r_gh;
593 struct gfs2_rgrpd *nrgd;
b3b94faa
DT
594 unsigned int num_gh;
595 int dir_rename = 0;
596 int alloc_required;
597 unsigned int x;
598 int error;
599
b3b94faa 600 if (ndentry->d_inode) {
feaa7bba 601 nip = GFS2_I(ndentry->d_inode);
b3b94faa
DT
602 if (ip == nip)
603 return 0;
604 }
605
b3b94faa
DT
606 /* Make sure we aren't trying to move a dirctory into it's subdir */
607
b60623c2 608 if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
b3b94faa
DT
609 dir_rename = 1;
610
b60623c2 611 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
b3b94faa
DT
612 &r_gh);
613 if (error)
614 goto out;
615
616 error = gfs2_ok_to_move(ip, ndip);
617 if (error)
618 goto out_gunlock_r;
619 }
620
d9d1ca30 621 num_gh = 1;
b3b94faa 622 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
d9d1ca30
SW
623 if (odip != ndip) {
624 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
625 num_gh++;
626 }
627 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
628 num_gh++;
b3b94faa 629
d9d1ca30
SW
630 if (nip) {
631 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
632 num_gh++;
ddee7608
RC
633 /* grab the resource lock for unlink flag twiddling
634 * this is the case of the target file already existing
635 * so we unlink before doing the rename
636 */
dbb7cae2 637 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
ddee7608
RC
638 if (nrgd)
639 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
d9d1ca30 640 }
b3b94faa
DT
641
642 error = gfs2_glock_nq_m(num_gh, ghs);
643 if (error)
644 goto out_uninit;
645
646 /* Check out the old directory */
647
648 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
649 if (error)
650 goto out_gunlock;
651
652 /* Check out the new directory */
653
654 if (nip) {
655 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
656 if (error)
657 goto out_gunlock;
658
b60623c2 659 if (S_ISDIR(nip->i_inode.i_mode)) {
b3b94faa
DT
660 if (nip->i_di.di_entries < 2) {
661 if (gfs2_consist_inode(nip))
4cc14f0b 662 gfs2_dinode_print(nip);
b3b94faa
DT
663 error = -EIO;
664 goto out_gunlock;
665 }
666 if (nip->i_di.di_entries > 2) {
667 error = -ENOTEMPTY;
668 goto out_gunlock;
669 }
670 }
671 } else {
faf450ef 672 error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
b3b94faa
DT
673 if (error)
674 goto out_gunlock;
675
dbb7cae2 676 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
b3b94faa
DT
677 switch (error) {
678 case -ENOENT:
679 error = 0;
680 break;
681 case 0:
682 error = -EEXIST;
683 default:
684 goto out_gunlock;
685 };
686
687 if (odip != ndip) {
4f56110a 688 if (!ndip->i_inode.i_nlink) {
b3b94faa
DT
689 error = -EINVAL;
690 goto out_gunlock;
691 }
cd915493 692 if (ndip->i_di.di_entries == (u32)-1) {
b3b94faa
DT
693 error = -EFBIG;
694 goto out_gunlock;
695 }
b60623c2 696 if (S_ISDIR(ip->i_inode.i_mode) &&
4f56110a 697 ndip->i_inode.i_nlink == (u32)-1) {
b3b94faa
DT
698 error = -EMLINK;
699 goto out_gunlock;
700 }
701 }
702 }
703
704 /* Check out the dir to be renamed */
705
706 if (dir_rename) {
faf450ef 707 error = permission(odentry->d_inode, MAY_WRITE, NULL);
b3b94faa
DT
708 if (error)
709 goto out_gunlock;
710 }
711
c752666c
SW
712 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
713 if (error < 0)
b3b94faa 714 goto out_gunlock;
c752666c 715 error = 0;
b3b94faa
DT
716
717 if (alloc_required) {
718 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
719
720 error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
721 if (error)
722 goto out_alloc;
723
2933f925 724 error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid);
b3b94faa
DT
725 if (error)
726 goto out_gunlock_q;
727
728 al->al_requested = sdp->sd_max_dirres;
729
730 error = gfs2_inplace_reserve(ndip);
731 if (error)
732 goto out_gunlock_q;
733
fe1bdedc 734 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
bb8d8a6f 735 al->al_rgd->rd_length +
b3b94faa 736 4 * RES_DINODE + 4 * RES_LEAF +
87d21e07 737 RES_STATFS + RES_QUOTA + 4, 0);
b3b94faa
DT
738 if (error)
739 goto out_ipreserv;
740 } else {
741 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
87d21e07 742 5 * RES_LEAF + 4, 0);
b3b94faa
DT
743 if (error)
744 goto out_gunlock;
745 }
746
747 /* Remove the target file, if it exists */
748
749 if (nip) {
b60623c2 750 if (S_ISDIR(nip->i_inode.i_mode))
feaa7bba
SW
751 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
752 else {
753 error = gfs2_dir_del(ndip, &ndentry->d_name);
754 if (error)
755 goto out_end_trans;
87d21e07 756 error = gfs2_change_nlink(nip, -1);
feaa7bba 757 }
b3b94faa
DT
758 if (error)
759 goto out_end_trans;
760 }
761
762 if (dir_rename) {
763 struct qstr name;
71b86f56 764 gfs2_str2qstr(&name, "..");
b3b94faa
DT
765
766 error = gfs2_change_nlink(ndip, +1);
767 if (error)
768 goto out_end_trans;
769 error = gfs2_change_nlink(odip, -1);
770 if (error)
771 goto out_end_trans;
772
ffed8ab3 773 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
b3b94faa
DT
774 if (error)
775 goto out_end_trans;
776 } else {
777 struct buffer_head *dibh;
778 error = gfs2_meta_inode_buffer(ip, &dibh);
779 if (error)
780 goto out_end_trans;
4bd91ba1 781 ip->i_inode.i_ctime = CURRENT_TIME;
d4e9c4c3 782 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 783 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
784 brelse(dibh);
785 }
786
787 error = gfs2_dir_del(odip, &odentry->d_name);
788 if (error)
789 goto out_end_trans;
790
dbb7cae2 791 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
b3b94faa
DT
792 if (error)
793 goto out_end_trans;
794
feaa7bba 795out_end_trans:
b3b94faa 796 gfs2_trans_end(sdp);
feaa7bba 797out_ipreserv:
b3b94faa
DT
798 if (alloc_required)
799 gfs2_inplace_release(ndip);
feaa7bba 800out_gunlock_q:
b3b94faa
DT
801 if (alloc_required)
802 gfs2_quota_unlock(ndip);
feaa7bba 803out_alloc:
b3b94faa
DT
804 if (alloc_required)
805 gfs2_alloc_put(ndip);
feaa7bba 806out_gunlock:
b3b94faa 807 gfs2_glock_dq_m(num_gh, ghs);
feaa7bba 808out_uninit:
b3b94faa
DT
809 for (x = 0; x < num_gh; x++)
810 gfs2_holder_uninit(ghs + x);
feaa7bba 811out_gunlock_r:
b3b94faa
DT
812 if (dir_rename)
813 gfs2_glock_dq_uninit(&r_gh);
feaa7bba 814out:
b3b94faa
DT
815 return error;
816}
817
818/**
819 * gfs2_readlink - Read the value of a symlink
820 * @dentry: the symlink
821 * @buf: the buffer to read the symlink data into
822 * @size: the size of the buffer
823 *
824 * Returns: errno
825 */
826
827static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
828 int user_size)
829{
feaa7bba 830 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
b3b94faa
DT
831 char array[GFS2_FAST_NAME_SIZE], *buf = array;
832 unsigned int len = GFS2_FAST_NAME_SIZE;
833 int error;
834
b3b94faa
DT
835 error = gfs2_readlinki(ip, &buf, &len);
836 if (error)
837 return error;
838
839 if (user_size > len - 1)
840 user_size = len - 1;
841
842 if (copy_to_user(user_buf, buf, user_size))
843 error = -EFAULT;
844 else
845 error = user_size;
846
847 if (buf != array)
848 kfree(buf);
849
850 return error;
851}
852
853/**
854 * gfs2_follow_link - Follow a symbolic link
855 * @dentry: The dentry of the link
856 * @nd: Data that we pass to vfs_follow_link()
857 *
858 * This can handle symlinks of any size. It is optimised for symlinks
859 * under GFS2_FAST_NAME_SIZE.
860 *
861 * Returns: 0 on success or error code
862 */
863
864static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
865{
feaa7bba 866 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
b3b94faa
DT
867 char array[GFS2_FAST_NAME_SIZE], *buf = array;
868 unsigned int len = GFS2_FAST_NAME_SIZE;
869 int error;
870
b3b94faa
DT
871 error = gfs2_readlinki(ip, &buf, &len);
872 if (!error) {
873 error = vfs_follow_link(nd, buf);
874 if (buf != array)
875 kfree(buf);
876 }
877
878 return ERR_PTR(error);
879}
880
881/**
882 * gfs2_permission -
883 * @inode:
884 * @mask:
885 * @nd: passed from Linux VFS, ignored by us
886 *
300c7d75
SW
887 * This may be called from the VFS directly, or from within GFS2 with the
888 * inode locked, so we look to see if the glock is already locked and only
889 * lock the glock if its not already been done.
890 *
b3b94faa
DT
891 * Returns: errno
892 */
893
894static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
895{
feaa7bba 896 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
897 struct gfs2_holder i_gh;
898 int error;
300c7d75 899 int unlock = 0;
b3b94faa 900
300c7d75
SW
901 if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
902 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
903 if (error)
904 return error;
905 unlock = 1;
906 }
b3b94faa 907
77386e1f 908 error = generic_permission(inode, mask, gfs2_check_acl);
300c7d75 909 if (unlock)
b3b94faa 910 gfs2_glock_dq_uninit(&i_gh);
b3b94faa
DT
911
912 return error;
913}
914
915static int setattr_size(struct inode *inode, struct iattr *attr)
916{
feaa7bba 917 struct gfs2_inode *ip = GFS2_I(inode);
16615be1 918 struct gfs2_sbd *sdp = GFS2_SB(inode);
b3b94faa
DT
919 int error;
920
921 if (attr->ia_size != ip->i_di.di_size) {
16615be1 922 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
b3b94faa
DT
923 if (error)
924 return error;
16615be1
SW
925 error = vmtruncate(inode, attr->ia_size);
926 gfs2_trans_end(sdp);
927 if (error)
928 return error;
b3b94faa
DT
929 }
930
aa6a85a9 931 error = gfs2_truncatei(ip, attr->ia_size);
090ffaa5
WC
932 if (error && (inode->i_size != ip->i_di.di_size))
933 i_size_write(inode, ip->i_di.di_size);
b3b94faa
DT
934
935 return error;
936}
937
938static int setattr_chown(struct inode *inode, struct iattr *attr)
939{
feaa7bba
SW
940 struct gfs2_inode *ip = GFS2_I(inode);
941 struct gfs2_sbd *sdp = GFS2_SB(inode);
b3b94faa 942 struct buffer_head *dibh;
cd915493 943 u32 ouid, ogid, nuid, ngid;
b3b94faa
DT
944 int error;
945
2933f925
SW
946 ouid = inode->i_uid;
947 ogid = inode->i_gid;
b3b94faa
DT
948 nuid = attr->ia_uid;
949 ngid = attr->ia_gid;
950
951 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
952 ouid = nuid = NO_QUOTA_CHANGE;
953 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
954 ogid = ngid = NO_QUOTA_CHANGE;
955
956 gfs2_alloc_get(ip);
957
958 error = gfs2_quota_lock(ip, nuid, ngid);
959 if (error)
960 goto out_alloc;
961
962 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
963 error = gfs2_quota_check(ip, nuid, ngid);
964 if (error)
965 goto out_gunlock_q;
966 }
967
968 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
969 if (error)
970 goto out_gunlock_q;
971
972 error = gfs2_meta_inode_buffer(ip, &dibh);
973 if (error)
974 goto out_end_trans;
975
976 error = inode_setattr(inode, attr);
977 gfs2_assert_warn(sdp, !error);
b3b94faa 978
d4e9c4c3 979 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
539e5d6b 980 gfs2_dinode_out(ip, dibh->b_data);
b3b94faa
DT
981 brelse(dibh);
982
983 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
af18ddb8
SW
984 gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid);
985 gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid);
b3b94faa
DT
986 }
987
a91ea69f 988out_end_trans:
b3b94faa 989 gfs2_trans_end(sdp);
a91ea69f 990out_gunlock_q:
b3b94faa 991 gfs2_quota_unlock(ip);
a91ea69f 992out_alloc:
b3b94faa 993 gfs2_alloc_put(ip);
b3b94faa
DT
994 return error;
995}
996
997/**
998 * gfs2_setattr - Change attributes on an inode
999 * @dentry: The dentry which is changing
1000 * @attr: The structure describing the change
1001 *
1002 * The VFS layer wants to change one or more of an inodes attributes. Write
1003 * that change out to disk.
1004 *
1005 * Returns: errno
1006 */
1007
1008static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1009{
1010 struct inode *inode = dentry->d_inode;
feaa7bba 1011 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
1012 struct gfs2_holder i_gh;
1013 int error;
1014
b3b94faa
DT
1015 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1016 if (error)
1017 return error;
1018
1019 error = -EPERM;
1020 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1021 goto out;
1022
1023 error = inode_change_ok(inode, attr);
1024 if (error)
1025 goto out;
1026
1027 if (attr->ia_valid & ATTR_SIZE)
1028 error = setattr_size(inode, attr);
1029 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1030 error = setattr_chown(inode, attr);
1031 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1032 error = gfs2_acl_chmod(ip, attr);
1033 else
1034 error = gfs2_setattr_simple(ip, attr);
1035
a91ea69f 1036out:
b3b94faa 1037 gfs2_glock_dq_uninit(&i_gh);
b3b94faa
DT
1038 if (!error)
1039 mark_inode_dirty(inode);
b3b94faa
DT
1040 return error;
1041}
1042
1043/**
1044 * gfs2_getattr - Read out an inode's attributes
26c1a574 1045 * @mnt: The vfsmount the inode is being accessed from
b3b94faa
DT
1046 * @dentry: The dentry to stat
1047 * @stat: The inode's stats
1048 *
dcf3dd85
SW
1049 * This may be called from the VFS directly, or from within GFS2 with the
1050 * inode locked, so we look to see if the glock is already locked and only
1051 * lock the glock if its not already been done. Note that its the NFS
1052 * readdirplus operation which causes this to be called (from filldir)
1053 * with the glock already held.
1054 *
b3b94faa
DT
1055 * Returns: errno
1056 */
1057
1058static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1059 struct kstat *stat)
1060{
1061 struct inode *inode = dentry->d_inode;
feaa7bba 1062 struct gfs2_inode *ip = GFS2_I(inode);
b3b94faa
DT
1063 struct gfs2_holder gh;
1064 int error;
dcf3dd85 1065 int unlock = 0;
b3b94faa 1066
dcf3dd85
SW
1067 if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
1068 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1069 if (error)
1070 return error;
1071 unlock = 1;
b3b94faa
DT
1072 }
1073
dcf3dd85 1074 generic_fillattr(inode, stat);
d7c103d0 1075 if (unlock)
dcf3dd85
SW
1076 gfs2_glock_dq_uninit(&gh);
1077
1078 return 0;
b3b94faa
DT
1079}
1080
1081static int gfs2_setxattr(struct dentry *dentry, const char *name,
1082 const void *data, size_t size, int flags)
1083{
feaa7bba 1084 struct inode *inode = dentry->d_inode;
b3b94faa
DT
1085 struct gfs2_ea_request er;
1086
b3b94faa
DT
1087 memset(&er, 0, sizeof(struct gfs2_ea_request));
1088 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1089 if (er.er_type == GFS2_EATYPE_UNUSED)
1090 return -EOPNOTSUPP;
1091 er.er_data = (char *)data;
1092 er.er_name_len = strlen(er.er_name);
1093 er.er_data_len = size;
1094 er.er_flags = flags;
1095
feaa7bba 1096 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
b3b94faa 1097
feaa7bba 1098 return gfs2_ea_set(GFS2_I(inode), &er);
b3b94faa
DT
1099}
1100
1101static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1102 void *data, size_t size)
1103{
1104 struct gfs2_ea_request er;
1105
b3b94faa
DT
1106 memset(&er, 0, sizeof(struct gfs2_ea_request));
1107 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1108 if (er.er_type == GFS2_EATYPE_UNUSED)
1109 return -EOPNOTSUPP;
1110 er.er_data = data;
1111 er.er_name_len = strlen(er.er_name);
1112 er.er_data_len = size;
1113
feaa7bba 1114 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1115}
1116
1117static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1118{
1119 struct gfs2_ea_request er;
1120
b3b94faa
DT
1121 memset(&er, 0, sizeof(struct gfs2_ea_request));
1122 er.er_data = (size) ? buffer : NULL;
1123 er.er_data_len = size;
1124
feaa7bba 1125 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1126}
1127
1128static int gfs2_removexattr(struct dentry *dentry, const char *name)
1129{
1130 struct gfs2_ea_request er;
1131
b3b94faa
DT
1132 memset(&er, 0, sizeof(struct gfs2_ea_request));
1133 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1134 if (er.er_type == GFS2_EATYPE_UNUSED)
1135 return -EOPNOTSUPP;
1136 er.er_name_len = strlen(er.er_name);
1137
feaa7bba 1138 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
b3b94faa
DT
1139}
1140
92e1d5be 1141const struct inode_operations gfs2_file_iops = {
b3b94faa
DT
1142 .permission = gfs2_permission,
1143 .setattr = gfs2_setattr,
1144 .getattr = gfs2_getattr,
1145 .setxattr = gfs2_setxattr,
1146 .getxattr = gfs2_getxattr,
1147 .listxattr = gfs2_listxattr,
1148 .removexattr = gfs2_removexattr,
1149};
1150
92e1d5be 1151const struct inode_operations gfs2_dev_iops = {
b3b94faa
DT
1152 .permission = gfs2_permission,
1153 .setattr = gfs2_setattr,
1154 .getattr = gfs2_getattr,
1155 .setxattr = gfs2_setxattr,
1156 .getxattr = gfs2_getxattr,
1157 .listxattr = gfs2_listxattr,
1158 .removexattr = gfs2_removexattr,
1159};
1160
92e1d5be 1161const struct inode_operations gfs2_dir_iops = {
b3b94faa
DT
1162 .create = gfs2_create,
1163 .lookup = gfs2_lookup,
1164 .link = gfs2_link,
1165 .unlink = gfs2_unlink,
1166 .symlink = gfs2_symlink,
1167 .mkdir = gfs2_mkdir,
1168 .rmdir = gfs2_rmdir,
1169 .mknod = gfs2_mknod,
1170 .rename = gfs2_rename,
1171 .permission = gfs2_permission,
1172 .setattr = gfs2_setattr,
1173 .getattr = gfs2_getattr,
1174 .setxattr = gfs2_setxattr,
1175 .getxattr = gfs2_getxattr,
1176 .listxattr = gfs2_listxattr,
1177 .removexattr = gfs2_removexattr,
1178};
1179
92e1d5be 1180const struct inode_operations gfs2_symlink_iops = {
b3b94faa
DT
1181 .readlink = gfs2_readlink,
1182 .follow_link = gfs2_follow_link,
1183 .permission = gfs2_permission,
1184 .setattr = gfs2_setattr,
1185 .getattr = gfs2_getattr,
1186 .setxattr = gfs2_setxattr,
1187 .getxattr = gfs2_getxattr,
1188 .listxattr = gfs2_listxattr,
1189 .removexattr = gfs2_removexattr,
1190};
1191