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