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