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