Debian Codegraph @

fs/gfs2/inode.c

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
4 * Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
5 */
6
7 #include <linux/slab.h>
8 #include <linux/spinlock.h>
9 #include <linux/completion.h>
10 #include <linux/buffer_head.h>
11 #include <linux/namei.h>
12 #include <linux/mm.h>
13 #include <linux/cred.h>
14 #include <linux/xattr.h>
15 #include <linux/posix_acl.h>
16 #include <linux/gfs2_ondisk.h>
17 #include <linux/crc32.h>
18 #include <linux/iomap.h>
19 #include <linux/security.h>
20 #include <linux/fiemap.h>
21 #include <linux/uaccess.h>
22
23 #include "gfs2.h"
24 #include "incore.h"
25 #include "acl.h"
26 #include "bmap.h"
27 #include "dir.h"
28 #include "xattr.h"
29 #include "glock.h"
30 #include "inode.h"
31 #include "meta_io.h"
32 #include "quota.h"
33 #include "rgrp.h"
34 #include "trans.h"
35 #include "util.h"
36 #include "super.h"
37 #include "glops.h"
38
39 static const struct inode_operations gfs2_file_iops;
40 static const struct inode_operations gfs2_dir_iops;
41 static const struct inode_operations gfs2_symlink_iops;
42
43 /**
44 * gfs2_set_iop - Sets inode operations
45 * @inode: The inode with correct i_mode filled in
46 *
47 * GFS2 lookup code fills in vfs inode contents based on info obtained
48 * from directory entry inside gfs2_inode_lookup().
49 */
50
51 static void gfs2_set_iop(struct inode *inode)
52 {
53 struct gfs2_sbd *sdp = GFS2_SB(inode);
54 umode_t mode = inode->i_mode;
55
56 if (S_ISREG(mode)) {
57 inode->i_op = &gfs2_file_iops;
58 if (gfs2_localflocks(sdp))
59 inode->i_fop = &gfs2_file_fops_nolock;
60 else
61 inode->i_fop = &gfs2_file_fops;
62 } else if (S_ISDIR(mode)) {
63 inode->i_op = &gfs2_dir_iops;
64 if (gfs2_localflocks(sdp))
65 inode->i_fop = &gfs2_dir_fops_nolock;
66 else
67 inode->i_fop = &gfs2_dir_fops;
68 } else if (S_ISLNK(mode)) {
69 inode->i_op = &gfs2_symlink_iops;
70 } else {
71 inode->i_op = &gfs2_file_iops;
72 init_special_inode(inode, inode->i_mode, inode->i_rdev);
73 }
74 }
75
76 static int iget_test(struct inode *inode, void *opaque)
77 {
78 u64 no_addr = *(u64 *)opaque;
79
80 return GFS2_I(inode)->i_no_addr == no_addr;
81 }
82
83 static int iget_set(struct inode *inode, void *opaque)
84 {
85 u64 no_addr = *(u64 *)opaque;
86
87 GFS2_I(inode)->i_no_addr = no_addr;
88 inode->i_ino = no_addr;
89 return 0;
90 }
91
92 void gfs2_setup_inode(struct inode *inode)
93 {
94 gfp_t gfp_mask;
95
96 /*
97 * Ensure all page cache allocations are done from GFP_NOFS context to
98 * prevent direct reclaim recursion back into the filesystem and blowing
99 * stacks or deadlocking.
100 */
101 gfp_mask = mapping_gfp_mask(inode->i_mapping);
102 mapping_set_gfp_mask(inode->i_mapping, gfp_mask & ~__GFP_FS);
103 }
104
105 /**
106 * gfs2_inode_lookup - Lookup an inode
107 * @sb: The super block
108 * @type: The type of the inode
109 * @no_addr: The inode number
110 * @no_formal_ino: The inode generation number
111 * @blktype: Requested block type (GFS2_BLKST_DINODE or GFS2_BLKST_UNLINKED;
112 * GFS2_BLKST_FREE to indicate not to verify)
113 *
114 * If @type is DT_UNKNOWN, the inode type is fetched from disk.
115 *
116 * If @blktype is anything other than GFS2_BLKST_FREE (which is used as a
117 * placeholder because it doesn't otherwise make sense), the on-disk block type
118 * is verified to be @blktype.
119 *
120 * When @no_formal_ino is non-zero, this function will return ERR_PTR(-ESTALE)
121 * if it detects that @no_formal_ino doesn't match the actual inode generation
122 * number. However, it doesn't always know unless @type is DT_UNKNOWN.
123 *
124 * Returns: A VFS inode, or an error
125 */
126
127 struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
128 u64 no_addr, u64 no_formal_ino,
129 unsigned int blktype)
130 {
131 struct inode *inode;
132 struct gfs2_inode *ip;
133 struct gfs2_holder i_gh;
134 int error;
135
136 gfs2_holder_mark_uninitialized(&i_gh);
137 inode = iget5_locked(sb, no_addr, iget_test, iget_set, &no_addr);
138 if (!inode)
139 return ERR_PTR(-ENOMEM);
140
141 ip = GFS2_I(inode);
142
143 if (inode_state_read_once(inode) & I_NEW) {
144 struct gfs2_sbd *sdp = GFS2_SB(inode);
145 struct gfs2_glock *io_gl;
146 int extra_flags = 0;
147
148 gfs2_setup_inode(inode);
149 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE,
150 &ip->i_gl);
151 if (unlikely(error))
152 goto fail;
153
154 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE,
155 &io_gl);
156 if (unlikely(error))
157 goto fail;
158
159 /*
160 * The only caller that sets @blktype to GFS2_BLKST_UNLINKED is
161 * delete_work_func(). Make sure not to cancel the delete work
162 * from within itself here.
163 */
164 if (blktype == GFS2_BLKST_UNLINKED)
165 extra_flags |= LM_FLAG_TRY;
166 else
167 gfs2_cancel_delete_work(io_gl);
168 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED,
169 GL_EXACT | GL_NOPID | extra_flags,
170 &ip->i_iopen_gh);
171 gfs2_glock_put(io_gl);
172 if (unlikely(error))
173 goto fail;
174
175 if (type == DT_UNKNOWN || blktype != GFS2_BLKST_FREE) {
176 /*
177 * The GL_SKIP flag indicates to skip reading the inode
178 * block. We read the inode when instantiating it
179 * after possibly checking the block type.
180 */
181 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE,
182 GL_SKIP, &i_gh);
183 if (error)
184 goto fail;
185
186 error = -ESTALE;
187 if (no_formal_ino &&
188 gfs2_inode_already_deleted(ip->i_gl, no_formal_ino))
189 goto fail;
190
191 if (blktype != GFS2_BLKST_FREE) {
192 error = gfs2_check_blk_type(sdp, no_addr,
193 blktype);
194 if (error)
195 goto fail;
196 }
197 }
198
199 set_bit(GLF_INSTANTIATE_NEEDED, &ip->i_gl->gl_flags);
200
201 /* Lowest possible timestamp; will be overwritten in gfs2_dinode_in. */
202 inode_set_atime(inode,
203 1LL << (8 * sizeof(inode_get_atime_sec(inode)) - 1),
204 0);
205
206 glock_set_object(ip->i_gl, ip);
207
208 if (type == DT_UNKNOWN) {
209 /* Inode glock must be locked already */
210 error = gfs2_instantiate(&i_gh);
211 if (error) {
212 glock_clear_object(ip->i_gl, ip);
213 goto fail;
214 }
215 } else {
216 ip->i_no_formal_ino = no_formal_ino;
217 inode->i_mode = DT2IF(type);
218 }
219
220 if (gfs2_holder_initialized(&i_gh))
221 gfs2_glock_dq_uninit(&i_gh);
222 glock_set_object(ip->i_iopen_gh.gh_gl, ip);
223
224 gfs2_set_iop(inode);
225 unlock_new_inode(inode);
226 }
227
228 if (no_formal_ino && ip->i_no_formal_ino &&
229 no_formal_ino != ip->i_no_formal_ino) {
230 iput(inode);
231 return ERR_PTR(-ESTALE);
232 }
233
234 return inode;
235
236 fail:
237 if (error == GLR_TRYFAILED)
238 error = -EAGAIN;
239 if (gfs2_holder_initialized(&ip->i_iopen_gh))
240 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
241 if (gfs2_holder_initialized(&i_gh))
242 gfs2_glock_dq_uninit(&i_gh);
243 if (ip->i_gl) {
244 gfs2_glock_put(ip->i_gl);
245 ip->i_gl = NULL;
246 }
247 iget_failed(inode);
248 return ERR_PTR(error);
249 }
250
251 /**
252 * gfs2_lookup_by_inum - look up an inode by inode number
253 * @sdp: The super block
254 * @no_addr: The inode number
255 * @no_formal_ino: The inode generation number (0 for any)
256 * @blktype: Requested block type (see gfs2_inode_lookup)
257 */
258 struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
259 u64 no_formal_ino, unsigned int blktype)
260 {
261 struct super_block *sb = sdp->sd_vfs;
262 struct inode *inode;
263 int error;
264
265 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, no_formal_ino,
266 blktype);
267 if (IS_ERR(inode))
268 return inode;
269
270 if (no_formal_ino) {
271 error = -EIO;
272 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
273 goto fail_iput;
274 }
275 return inode;
276
277 fail_iput:
278 iput(inode);
279 return ERR_PTR(error);
280 }
281
282
283 /**
284 * gfs2_lookup_meta - Look up an inode in a metadata directory
285 * @dip: The directory
286 * @name: The name of the inode
287 */
288 struct inode *gfs2_lookup_meta(struct inode *dip, const char *name)
289 {
290 struct qstr qstr;
291 struct inode *inode;
292
293 gfs2_str2qstr(&qstr, name);
294 inode = gfs2_lookupi(dip, &qstr, 1);
295 if (IS_ERR_OR_NULL(inode))
296 return inode ? inode : ERR_PTR(-ENOENT);
297
298 /*
299 * Must not call back into the filesystem when allocating
300 * pages in the metadata inode's address space.
301 */
302 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
303
304 return inode;
305 }
306
307
308 /**
309 * gfs2_lookupi - Look up a filename in a directory and return its inode
310 * @dir: The inode of the directory containing the inode to look-up
311 * @name: The name of the inode to look for
312 * @is_root: If 1, ignore the caller's permissions
313 *
314 * This can be called via the VFS filldir function when NFS is doing
315 * a readdirplus and the inode which its intending to stat isn't
316 * already in cache. In this case we must not take the directory glock
317 * again, since the readdir call will have already taken that lock.
318 *
319 * Returns: errno
320 */
321
322 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
323 int is_root)
324 {
325 struct super_block *sb = dir->i_sb;
326 struct gfs2_inode *dip = GFS2_I(dir);
327 struct gfs2_holder d_gh;
328 int error = 0;
329 struct inode *inode = NULL;
330
331 gfs2_holder_mark_uninitialized(&d_gh);
332 if (!name->len || name->len > GFS2_FNAMESIZE)
333 return ERR_PTR(-ENAMETOOLONG);
334
335 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
336 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
337 dir == d_inode(sb->s_root))) {
338 igrab(dir);
339 return dir;
340 }
341
342 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
343 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
344 if (error)
345 return ERR_PTR(error);
346 }
347
348 if (!is_root) {
349 error = gfs2_permission(&nop_mnt_idmap, dir, MAY_EXEC);
350 if (error)
351 goto out;
352 }
353
354 inode = gfs2_dir_search(dir, name, false);
355 if (IS_ERR(inode))
356 error = PTR_ERR(inode);
357 out:
358 if (gfs2_holder_initialized(&d_gh))
359 gfs2_glock_dq_uninit(&d_gh);
360 if (error == -ENOENT)
361 return NULL;
362 return inode ? inode : ERR_PTR(error);
363 }
364
365 /**
366 * create_ok - OK to create a new on-disk inode here?
367 * @dip: Directory in which dinode is to be created
368 * @name: Name of new dinode
369 * @mode:
370 *
371 * Returns: errno
372 */
373
374 static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
375 umode_t mode)
376 {
377 int error;
378
379 error = gfs2_permission(&nop_mnt_idmap, &dip->i_inode,
380 MAY_WRITE | MAY_EXEC);
381 if (error)
382 return error;
383
384 /* Don't create entries in an unlinked directory */
385 if (!dip->i_inode.i_nlink)
386 return -ENOENT;
387
388 if (dip->i_entries == (u32)-1)
389 return -EFBIG;
390 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
391 return -EMLINK;
392
393 return 0;
394 }
395
396 static void munge_mode_uid_gid(const struct gfs2_inode *dip,
397 struct inode *inode)
398 {
399 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
400 (dip->i_inode.i_mode & S_ISUID) &&
401 !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
402 if (S_ISDIR(inode->i_mode))
403 inode->i_mode |= S_ISUID;
404 else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
405 inode->i_mode &= ~07111;
406 inode->i_uid = dip->i_inode.i_uid;
407 } else
408 inode->i_uid = current_fsuid();
409
410 if (dip->i_inode.i_mode & S_ISGID) {
411 if (S_ISDIR(inode->i_mode))
412 inode->i_mode |= S_ISGID;
413 inode->i_gid = dip->i_inode.i_gid;
414 } else
415 inode->i_gid = current_fsgid();
416 }
417
418 static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
419 {
420 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
421 struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, };
422 int error;
423
424 error = gfs2_quota_lock_check(ip, &ap);
425 if (error)
426 goto out;
427
428 error = gfs2_inplace_reserve(ip, &ap);
429 if (error)
430 goto out_quota;
431
432 error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0);
433 if (error)
434 goto out_ipreserv;
435
436 error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1);
437 if (error)
438 goto out_trans_end;
439
440 ip->i_no_formal_ino = ip->i_generation;
441 ip->i_inode.i_ino = ip->i_no_addr;
442 ip->i_goal = ip->i_no_addr;
443 if (*dblocks > 1)
444 ip->i_eattr = ip->i_no_addr + 1;
445
446 out_trans_end:
447 gfs2_trans_end(sdp);
448 out_ipreserv:
449 gfs2_inplace_release(ip);
450 out_quota:
451 gfs2_quota_unlock(ip);
452 out:
453 return error;
454 }
455
456 static void gfs2_final_release_pages(struct gfs2_inode *ip)
457 {
458 struct inode *inode = &ip->i_inode;
459 struct gfs2_glock *gl = ip->i_gl;
460
461 /* This can only happen during incomplete inode creation. */
462 if (unlikely(!gl))
463 return;
464
465 truncate_inode_pages(gfs2_glock2aspace(gl), 0);
466 truncate_inode_pages(&inode->i_data, 0);
467
468 if (atomic_read(&gl->gl_revokes) == 0) {
469 clear_bit(GLF_LFLUSH, &gl->gl_flags);
470 clear_bit(GLF_DIRTY, &gl->gl_flags);
471 }
472 }
473
474 int gfs2_dinode_dealloc(struct gfs2_inode *ip)
475 {
476 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
477 struct gfs2_rgrpd *rgd;
478 struct gfs2_holder gh;
479 int error;
480
481 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
482 gfs2_consist_inode(ip);
483 return -EIO;
484 }
485
486 gfs2_rindex_update(sdp);
487
488 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
489 if (error)
490 return error;
491
492 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
493 if (!rgd) {
494 gfs2_consist_inode(ip);
495 error = -EIO;
496 goto out_qs;
497 }
498
499 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
500 LM_FLAG_NODE_SCOPE, &gh);
501 if (error)
502 goto out_qs;
503
504 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA,
505 sdp->sd_jdesc->jd_blocks);
506 if (error)
507 goto out_rg_gunlock;
508
509 gfs2_free_di(rgd, ip);
510
511 gfs2_final_release_pages(ip);
512
513 gfs2_trans_end(sdp);
514
515 out_rg_gunlock:
516 gfs2_glock_dq_uninit(&gh);
517 out_qs:
518 gfs2_quota_unhold(ip);
519 return error;
520 }
521
522 static void gfs2_init_dir(struct buffer_head *dibh,
523 const struct gfs2_inode *parent)
524 {
525 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
526 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
527
528 gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
529 dent->de_inum = di->di_num; /* already GFS2 endian */
530 dent->de_type = cpu_to_be16(DT_DIR);
531
532 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
533 gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
534 gfs2_inum_out(parent, dent);
535 dent->de_type = cpu_to_be16(DT_DIR);
536
537 }
538
539 /**
540 * gfs2_init_xattr - Initialise an xattr block for a new inode
541 * @ip: The inode in question
542 *
543 * This sets up an empty xattr block for a new inode, ready to
544 * take any ACLs, LSM xattrs, etc.
545 */
546
547 static void gfs2_init_xattr(struct gfs2_inode *ip)
548 {
549 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
550 struct buffer_head *bh;
551 struct gfs2_ea_header *ea;
552
553 bh = gfs2_meta_new(ip->i_gl, ip->i_eattr);
554 gfs2_trans_add_meta(ip->i_gl, bh);
555 gfs2_metatype_set(bh, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
556 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
557
558 ea = GFS2_EA_BH2FIRST(bh);
559 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
560 ea->ea_type = GFS2_EATYPE_UNUSED;
561 ea->ea_flags = GFS2_EAFLAG_LAST;
562
563 brelse(bh);
564 }
565
566 /**
567 * init_dinode - Fill in a new dinode structure
568 * @dip: The directory this inode is being created in
569 * @ip: The inode
570 * @symname: The symlink destination (if a symlink)
571 *
572 */
573
574 static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
575 const char *symname)
576 {
577 struct gfs2_dinode *di;
578 struct buffer_head *dibh;
579
580 dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
581 gfs2_trans_add_meta(ip->i_gl, dibh);
582 di = (struct gfs2_dinode *)dibh->b_data;
583 gfs2_dinode_out(ip, di);
584
585 di->di_major = cpu_to_be32(imajor(&ip->i_inode));
586 di->di_minor = cpu_to_be32(iminor(&ip->i_inode));
587 di->__pad1 = 0;
588 di->__pad2 = 0;
589 di->__pad3 = 0;
590 memset(&di->__pad4, 0, sizeof(di->__pad4));
591 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
592 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
593
594 switch(ip->i_inode.i_mode & S_IFMT) {
595 case S_IFDIR:
596 gfs2_init_dir(dibh, dip);
597 break;
598 case S_IFLNK:
599 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
600 break;
601 }
602
603 set_buffer_uptodate(dibh);
604 brelse(dibh);
605 }
606
607 /**
608 * gfs2_trans_da_blks - Calculate number of blocks to link inode
609 * @dip: The directory we are linking into
610 * @da: The dir add information
611 * @nr_inodes: The number of inodes involved
612 *
613 * This calculate the number of blocks we need to reserve in a
614 * transaction to link @nr_inodes into a directory. In most cases
615 * @nr_inodes will be 2 (the directory plus the inode being linked in)
616 * but in case of rename, 4 may be required.
617 *
618 * Returns: Number of blocks
619 */
620
621 static unsigned gfs2_trans_da_blks(const struct gfs2_inode *dip,
622 const struct gfs2_diradd *da,
623 unsigned nr_inodes)
624 {
625 return da->nr_blocks + gfs2_rg_blocks(dip, da->nr_blocks) +
626 (nr_inodes * RES_DINODE) + RES_QUOTA + RES_STATFS;
627 }
628
629 static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
630 struct gfs2_inode *ip, struct gfs2_diradd *da)
631 {
632 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
633 struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
634 int error;
635
636 if (da->nr_blocks) {
637 error = gfs2_quota_lock_check(dip, &ap);
638 if (error)
639 goto fail_quota_locks;
640
641 error = gfs2_inplace_reserve(dip, &ap);
642 if (error)
643 goto fail_quota_locks;
644
645 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
646 if (error)
647 goto fail_ipreserv;
648 } else {
649 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
650 if (error)
651 goto fail_quota_locks;
652 }
653
654 error = gfs2_dir_add(&dip->i_inode, name, ip, da);
655
656 gfs2_trans_end(sdp);
657 fail_ipreserv:
658 gfs2_inplace_release(dip);
659 fail_quota_locks:
660 gfs2_quota_unlock(dip);
661 return error;
662 }
663
664 static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
665 void *fs_info)
666 {
667 const struct xattr *xattr;
668 int err = 0;
669
670 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
671 err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
672 xattr->value_len, 0,
673 GFS2_EATYPE_SECURITY);
674 if (err < 0)
675 break;
676 }
677 return err;
678 }
679
680 /**
681 * gfs2_create_inode - Create a new inode
682 * @dir: The parent directory
683 * @dentry: The new dentry
684 * @file: If non-NULL, the file which is being opened
685 * @mode: The permissions on the new inode
686 * @dev: For device nodes, this is the device number
687 * @symname: For symlinks, this is the link destination
688 * @size: The initial size of the inode (ignored for directories)
689 * @excl: Force fail if inode exists
690 *
691 * FIXME: Change to allocate the disk blocks and write them out in the same
692 * transaction. That way, we can no longer end up in a situation in which an
693 * inode is allocated, the node crashes, and the block looks like a valid
694 * inode. (With atomic creates in place, we will also no longer need to zero
695 * the link count and dirty the inode here on failure.)
696 *
697 * Returns: 0 on success, or error code
698 */
699
700 static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
701 struct file *file,
702 umode_t mode, dev_t dev, const char *symname,
703 unsigned int size, int excl)
704 {
705 const struct qstr *name = &dentry->d_name;
706 struct posix_acl *default_acl, *acl;
707 struct gfs2_holder d_gh, gh;
708 struct inode *inode = NULL;
709 struct gfs2_inode *dip = GFS2_I(dir), *ip;
710 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
711 struct gfs2_glock *io_gl;
712 int error, dealloc_error;
713 u32 aflags = 0;
714 unsigned blocks = 1;
715 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
716 bool xattr_initialized = false;
717
718 if (!name->len || name->len > GFS2_FNAMESIZE)
719 return -ENAMETOOLONG;
720
721 error = gfs2_qa_get(dip);
722 if (error)
723 return error;
724
725 error = gfs2_rindex_update(sdp);
726 if (error)
727 goto fail;
728
729 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, &d_gh);
730 if (error)
731 goto fail;
732 gfs2_holder_mark_uninitialized(&gh);
733
734 error = create_ok(dip, name, mode);
735 if (error)
736 goto fail_gunlock;
737
738 inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
739 error = PTR_ERR(inode);
740 if (!IS_ERR(inode)) {
741 if (S_ISDIR(inode->i_mode)) {
742 iput(inode);
743 inode = NULL;
744 error = -EISDIR;
745 goto fail_gunlock;
746 }
747 d_instantiate(dentry, inode);
748 error = 0;
749 if (file) {
750 if (S_ISREG(inode->i_mode))
751 error = finish_open(file, dentry, gfs2_open_common);
752 else
753 error = finish_no_open(file, NULL);
754 }
755 gfs2_glock_dq_uninit(&d_gh);
756 goto fail;
757 } else if (error != -ENOENT) {
758 goto fail_gunlock;
759 }
760
761 error = gfs2_diradd_alloc_required(dir, name, &da);
762 if (error < 0)
763 goto fail_gunlock;
764
765 inode = new_inode(sdp->sd_vfs);
766 error = -ENOMEM;
767 if (!inode)
768 goto fail_gunlock;
769 gfs2_setup_inode(inode);
770 ip = GFS2_I(inode);
771
772 error = posix_acl_create(dir, &mode, &default_acl, &acl);
773 if (error)
774 goto fail_gunlock;
775
776 error = gfs2_qa_get(ip);
777 if (error)
778 goto fail_free_acls;
779
780 inode->i_mode = mode;
781 set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
782 inode->i_rdev = dev;
783 inode->i_size = size;
784 simple_inode_init_ts(inode);
785 munge_mode_uid_gid(dip, inode);
786 check_and_update_goal(dip);
787 ip->i_goal = dip->i_goal;
788 ip->i_diskflags = 0;
789 ip->i_eattr = 0;
790 ip->i_height = 0;
791 ip->i_depth = 0;
792 ip->i_entries = 0;
793 ip->i_no_addr = 0; /* Temporarily zero until real addr is assigned */
794
795 switch(mode & S_IFMT) {
796 case S_IFREG:
797 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
798 gfs2_tune_get(sdp, gt_new_files_jdata))
799 ip->i_diskflags |= GFS2_DIF_JDATA;
800 gfs2_set_aops(inode);
801 break;
802 case S_IFDIR:
803 ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
804 ip->i_diskflags |= GFS2_DIF_JDATA;
805 ip->i_entries = 2;
806 break;
807 }
808
809 /* Force SYSTEM flag on all files and subdirs of a SYSTEM directory */
810 if (dip->i_diskflags & GFS2_DIF_SYSTEM)
811 ip->i_diskflags |= GFS2_DIF_SYSTEM;
812
813 gfs2_set_inode_flags(inode);
814
815 if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) ||
816 (dip->i_diskflags & GFS2_DIF_TOPDIR))
817 aflags |= GFS2_AF_ORLOV;
818
819 if (default_acl || acl)
820 blocks++;
821
822 error = alloc_dinode(ip, aflags, &blocks);
823 if (error)
824 goto fail_free_inode;
825
826 gfs2_set_inode_blocks(inode, blocks);
827
828 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
829 if (error)
830 goto fail_dealloc_inode;
831
832 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
833 if (error)
834 goto fail_dealloc_inode;
835 gfs2_cancel_delete_work(io_gl);
836 io_gl->gl_no_formal_ino = ip->i_no_formal_ino;
837
838 retry:
839 error = insert_inode_locked4(inode, ip->i_no_addr, iget_test, &ip->i_no_addr);
840 if (error == -EBUSY)
841 goto retry;
842 if (error)
843 goto fail_gunlock2;
844
845 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT | GL_NOPID,
846 &ip->i_iopen_gh);
847 if (error)
848 goto fail_gunlock2;
849
850 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, &gh);
851 if (error)
852 goto fail_gunlock3;
853 clear_bit(GLF_INSTANTIATE_NEEDED, &ip->i_gl->gl_flags);
854
855 error = gfs2_trans_begin(sdp, blocks, 0);
856 if (error)
857 goto fail_gunlock3;
858
859 if (blocks > 1) {
860 gfs2_init_xattr(ip);
861 xattr_initialized = true;
862 }
863 init_dinode(dip, ip, symname);
864 gfs2_trans_end(sdp);
865
866 glock_set_object(ip->i_gl, ip);
867 glock_set_object(io_gl, ip);
868 gfs2_set_iop(inode);
869
870 if (default_acl) {
871 error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
872 if (error)
873 goto fail_gunlock4;
874 posix_acl_release(default_acl);
875 default_acl = NULL;
876 }
877 if (acl) {
878 error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
879 if (error)
880 goto fail_gunlock4;
881 posix_acl_release(acl);
882 acl = NULL;
883 }
884
885 error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
886 &gfs2_initxattrs, NULL);
887 if (error)
888 goto fail_gunlock4;
889
890 error = link_dinode(dip, name, ip, &da);
891 if (error)
892 goto fail_gunlock4;
893
894 mark_inode_dirty(inode);
895 d_instantiate_new(dentry, inode);
896 /* After instantiate, errors should result in evict which will destroy
897 * both inode and iopen glocks properly. */
898 if (file) {
899 file->f_mode |= FMODE_CREATED;
900 error = finish_open(file, dentry, gfs2_open_common);
901 }
902 gfs2_glock_dq_uninit(&d_gh);
903 gfs2_qa_put(ip);
904 gfs2_glock_dq_uninit(&gh);
905 gfs2_glock_put(io_gl);
906 gfs2_qa_put(dip);
907 return error;
908
909 fail_gunlock4:
910 glock_clear_object(ip->i_gl, ip);
911 glock_clear_object(io_gl, ip);
912 fail_gunlock3:
913 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
914 fail_gunlock2:
915 gfs2_glock_put(io_gl);
916 fail_dealloc_inode:
917 dealloc_error = 0;
918 if (ip->i_eattr)
919 dealloc_error = gfs2_ea_dealloc(ip, xattr_initialized);
920 clear_nlink(inode);
921 mark_inode_dirty(inode);
922 if (!dealloc_error)
923 dealloc_error = gfs2_dinode_dealloc(ip);
924 if (dealloc_error)
925 fs_warn(sdp, "%s: %d\n", __func__, dealloc_error);
926 ip->i_no_addr = 0;
927 fail_free_inode:
928 if (ip->i_gl) {
929 gfs2_glock_put(ip->i_gl);
930 ip->i_gl = NULL;
931 }
932 gfs2_rs_deltree(&ip->i_res);
933 gfs2_qa_put(ip);
934 fail_free_acls:
935 posix_acl_release(default_acl);
936 posix_acl_release(acl);
937 fail_gunlock:
938 gfs2_dir_no_add(&da);
939 gfs2_glock_dq_uninit(&d_gh);
940 if (!IS_ERR_OR_NULL(inode)) {
941 if (inode_state_read_once(inode) & I_NEW)
942 iget_failed(inode);
943 else
944 iput(inode);
945 }
946 if (gfs2_holder_initialized(&gh))
947 gfs2_glock_dq_uninit(&gh);
948 fail:
949 gfs2_qa_put(dip);
950 return error;
951 }
952
953 /**
954 * gfs2_create - Create a file
955 * @idmap: idmap of the mount the inode was found from
956 * @dir: The directory in which to create the file
957 * @dentry: The dentry of the new file
958 * @mode: The mode of the new file
959 * @excl: Force fail if inode exists
960 *
961 * Returns: errno
962 */
963
964 static int gfs2_create(struct mnt_idmap *idmap, struct inode *dir,
965 struct dentry *dentry, umode_t mode, bool excl)
966 {
967 return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl);
968 }
969
970 /**
971 * __gfs2_lookup - Look up a filename in a directory and return its inode
972 * @dir: The directory inode
973 * @dentry: The dentry of the new inode
974 * @file: File to be opened
975 *
976 *
977 * Returns: errno
978 */
979
980 static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
981 struct file *file)
982 {
983 struct inode *inode;
984 struct dentry *d;
985 struct gfs2_holder gh;
986 struct gfs2_glock *gl;
987 int error;
988
989 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
990 if (inode == NULL) {
991 d_add(dentry, NULL);
992 return NULL;
993 }
994 if (IS_ERR(inode))
995 return ERR_CAST(inode);
996
997 gl = GFS2_I(inode)->i_gl;
998 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
999 if (error) {
1000 iput(inode);
1001 return ERR_PTR(error);
1002 }
1003
1004 d = d_splice_alias(inode, dentry);
1005 if (IS_ERR(d)) {
1006 gfs2_glock_dq_uninit(&gh);
1007 return d;
1008 }
1009 if (file && S_ISREG(inode->i_mode))
1010 error = finish_open(file, dentry, gfs2_open_common);
1011
1012 gfs2_glock_dq_uninit(&gh);
1013 if (error) {
1014 dput(d);
1015 return ERR_PTR(error);
1016 }
1017 return d;
1018 }
1019
1020 static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
1021 unsigned flags)
1022 {
1023 return __gfs2_lookup(dir, dentry, NULL);
1024 }
1025
1026 /**
1027 * gfs2_link - Link to a file
1028 * @old_dentry: The inode to link
1029 * @dir: Add link to this directory
1030 * @dentry: The name of the link
1031 *
1032 * Link the inode in "old_dentry" into the directory "dir" with the
1033 * name in "dentry".
1034 *
1035 * Returns: errno
1036 */
1037
1038 static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
1039 struct dentry *dentry)
1040 {
1041 struct gfs2_inode *dip = GFS2_I(dir);
1042 struct gfs2_sbd *sdp = GFS2_SB(dir);
1043 struct inode *inode = d_inode(old_dentry);
1044 struct gfs2_inode *ip = GFS2_I(inode);
1045 struct gfs2_holder d_gh, gh;
1046 struct buffer_head *dibh;
1047 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
1048 int error;
1049
1050 if (S_ISDIR(inode->i_mode))
1051 return -EPERM;
1052
1053 error = gfs2_qa_get(dip);
1054 if (error)
1055 return error;
1056
1057 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, &d_gh);
1058 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1059
1060 error = gfs2_glock_nq(&d_gh);
1061 if (error)
1062 goto out_parent;
1063
1064 error = gfs2_glock_nq(&gh);
1065 if (error)
1066 goto out_child;
1067
1068 error = -ENOENT;
1069 if (inode->i_nlink == 0)
1070 goto out_gunlock;
1071
1072 error = gfs2_permission(&nop_mnt_idmap, dir, MAY_WRITE | MAY_EXEC);
1073 if (error)
1074 goto out_gunlock;
1075
1076 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
1077 switch (error) {
1078 case -ENOENT:
1079 break;
1080 case 0:
1081 error = -EEXIST;
1082 goto out_gunlock;
1083 default:
1084 goto out_gunlock;
1085 }
1086
1087 error = -EINVAL;
1088 if (!dip->i_inode.i_nlink)
1089 goto out_gunlock;
1090 error = -EFBIG;
1091 if (dip->i_entries == (u32)-1)
1092 goto out_gunlock;
1093 error = -EPERM;
1094 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1095 goto out_gunlock;
1096 error = -EMLINK;
1097 if (ip->i_inode.i_nlink == (u32)-1)
1098 goto out_gunlock;
1099
1100 error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
1101 if (error < 0)
1102 goto out_gunlock;
1103
1104 if (da.nr_blocks) {
1105 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
1106 error = gfs2_quota_lock_check(dip, &ap);
1107 if (error)
1108 goto out_gunlock;
1109
1110 error = gfs2_inplace_reserve(dip, &ap);
1111 if (error)
1112 goto out_gunlock_q;
1113
1114 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
1115 if (error)
1116 goto out_ipres;
1117 } else {
1118 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
1119 if (error)
1120 goto out_ipres;
1121 }
1122
1123 error = gfs2_meta_inode_buffer(ip, &dibh);
1124 if (error)
1125 goto out_end_trans;
1126
1127 error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
1128 if (error)
1129 goto out_brelse;
1130
1131 gfs2_trans_add_meta(ip->i_gl, dibh);
1132 inc_nlink(&ip->i_inode);
1133 inode_set_ctime_current(&ip->i_inode);
1134 ihold(inode);
1135 d_instantiate(dentry, inode);
1136 mark_inode_dirty(inode);
1137
1138 out_brelse:
1139 brelse(dibh);
1140 out_end_trans:
1141 gfs2_trans_end(sdp);
1142 out_ipres:
1143 if (da.nr_blocks)
1144 gfs2_inplace_release(dip);
1145 out_gunlock_q:
1146 if (da.nr_blocks)
1147 gfs2_quota_unlock(dip);
1148 out_gunlock:
1149 gfs2_dir_no_add(&da);
1150 gfs2_glock_dq(&gh);
1151 out_child:
1152 gfs2_glock_dq(&d_gh);
1153 out_parent:
1154 gfs2_qa_put(dip);
1155 gfs2_holder_uninit(&d_gh);
1156 gfs2_holder_uninit(&gh);
1157 return error;
1158 }
1159
1160 /*
1161 * gfs2_unlink_ok - check to see that a inode is still in a directory
1162 * @dip: the directory
1163 * @name: the name of the file
1164 * @ip: the inode
1165 *
1166 * Assumes that the lock on (at least) @dip is held.
1167 *
1168 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1169 */
1170
1171 static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
1172 const struct gfs2_inode *ip)
1173 {
1174 int error;
1175
1176 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1177 return -EPERM;
1178
1179 if ((dip->i_inode.i_mode & S_ISVTX) &&
1180 !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
1181 !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
1182 return -EPERM;
1183
1184 if (IS_APPEND(&dip->i_inode))
1185 return -EPERM;
1186
1187 error = gfs2_permission(&nop_mnt_idmap, &dip->i_inode,
1188 MAY_WRITE | MAY_EXEC);
1189 if (error)
1190 return error;
1191
1192 return gfs2_dir_check(&dip->i_inode, name, ip);
1193 }
1194
1195 /**
1196 * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
1197 * @dip: The parent directory
1198 * @dentry: The dentry to unlink
1199 *
1200 * Called with all the locks and in a transaction. This will only be
1201 * called for a directory after it has been checked to ensure it is empty.
1202 *
1203 * Returns: 0 on success, or an error
1204 */
1205
1206 static int gfs2_unlink_inode(struct gfs2_inode *dip,
1207 const struct dentry *dentry)
1208 {
1209 struct inode *inode = d_inode(dentry);
1210 struct gfs2_inode *ip = GFS2_I(inode);
1211 int error;
1212
1213 error = gfs2_dir_del(dip, dentry);
1214 if (error)
1215 return error;
1216
1217 ip->i_entries = 0;
1218 inode_set_ctime_current(inode);
1219 if (S_ISDIR(inode->i_mode))
1220 clear_nlink(inode);
1221 else
1222 drop_nlink(inode);
1223 mark_inode_dirty(inode);
1224 if (inode->i_nlink == 0)
1225 gfs2_unlink_di(inode);
1226 return 0;
1227 }
1228
1229
1230 /**
1231 * gfs2_unlink - Unlink an inode (this does rmdir as well)
1232 * @dir: The inode of the directory containing the inode to unlink
1233 * @dentry: The file itself
1234 *
1235 * This routine uses the type of the inode as a flag to figure out
1236 * whether this is an unlink or an rmdir.
1237 *
1238 * Returns: errno
1239 */
1240
1241 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
1242 {
1243 struct gfs2_inode *dip = GFS2_I(dir);
1244 struct gfs2_sbd *sdp = GFS2_SB(dir);
1245 struct inode *inode = d_inode(dentry);
1246 struct gfs2_inode *ip = GFS2_I(inode);
1247 struct gfs2_holder d_gh, r_gh, gh;
1248 struct gfs2_rgrpd *rgd;
1249 int error;
1250
1251 error = gfs2_rindex_update(sdp);
1252 if (error)
1253 return error;
1254
1255 error = -EROFS;
1256
1257 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, &d_gh);
1258 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1259
1260 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
1261 if (!rgd)
1262 goto out_inodes;
1263
1264 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, LM_FLAG_NODE_SCOPE, &r_gh);
1265
1266
1267 error = gfs2_glock_nq(&d_gh);
1268 if (error)
1269 goto out_parent;
1270
1271 error = gfs2_glock_nq(&gh);
1272 if (error)
1273 goto out_child;
1274
1275 error = -ENOENT;
1276 if (inode->i_nlink == 0)
1277 goto out_rgrp;
1278
1279 if (S_ISDIR(inode->i_mode)) {
1280 error = -ENOTEMPTY;
1281 if (ip->i_entries > 2 || inode->i_nlink > 2)
1282 goto out_rgrp;
1283 }
1284
1285 error = gfs2_glock_nq(&r_gh); /* rgrp */
1286 if (error)
1287 goto out_rgrp;
1288
1289 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
1290 if (error)
1291 goto out_gunlock;
1292
1293 error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
1294 if (error)
1295 goto out_gunlock;
1296
1297 error = gfs2_unlink_inode(dip, dentry);
1298 gfs2_trans_end(sdp);
1299
1300 out_gunlock:
1301 gfs2_glock_dq(&r_gh);
1302 out_rgrp:
1303 gfs2_glock_dq(&gh);
1304 out_child:
1305 gfs2_glock_dq(&d_gh);
1306 out_parent:
1307 gfs2_holder_uninit(&r_gh);
1308 out_inodes:
1309 gfs2_holder_uninit(&gh);
1310 gfs2_holder_uninit(&d_gh);
1311 return error;
1312 }
1313
1314 /**
1315 * gfs2_symlink - Create a symlink
1316 * @idmap: idmap of the mount the inode was found from
1317 * @dir: The directory to create the symlink in
1318 * @dentry: The dentry to put the symlink in
1319 * @symname: The thing which the link points to
1320 *
1321 * Returns: errno
1322 */
1323
1324 static int gfs2_symlink(struct mnt_idmap *idmap, struct inode *dir,
1325 struct dentry *dentry, const char *symname)
1326 {
1327 unsigned int size;
1328
1329 size = strlen(symname);
1330 if (size >= gfs2_max_stuffed_size(GFS2_I(dir)))
1331 return -ENAMETOOLONG;
1332
1333 return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
1334 }
1335
1336 /**
1337 * gfs2_mkdir - Make a directory
1338 * @idmap: idmap of the mount the inode was found from
1339 * @dir: The parent directory of the new one
1340 * @dentry: The dentry of the new directory
1341 * @mode: The mode of the new directory
1342 *
1343 * Returns: the dentry, or ERR_PTR(errno)
1344 */
1345
1346 static struct dentry *gfs2_mkdir(struct mnt_idmap *idmap, struct inode *dir,
1347 struct dentry *dentry, umode_t mode)
1348 {
1349 unsigned dsize = gfs2_max_stuffed_size(GFS2_I(dir));
1350
1351 return ERR_PTR(gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0));
1352 }
1353
1354 /**
1355 * gfs2_mknod - Make a special file
1356 * @idmap: idmap of the mount the inode was found from
1357 * @dir: The directory in which the special file will reside
1358 * @dentry: The dentry of the special file
1359 * @mode: The mode of the special file
1360 * @dev: The device specification of the special file
1361 *
1362 */
1363
1364 static int gfs2_mknod(struct mnt_idmap *idmap, struct inode *dir,
1365 struct dentry *dentry, umode_t mode, dev_t dev)
1366 {
1367 return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0);
1368 }
1369
1370 /**
1371 * gfs2_atomic_open - Atomically open a file
1372 * @dir: The directory
1373 * @dentry: The proposed new entry
1374 * @file: The proposed new struct file
1375 * @flags: open flags
1376 * @mode: File mode
1377 *
1378 * Returns: error code or 0 for success
1379 */
1380
1381 static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
1382 struct file *file, unsigned flags,
1383 umode_t mode)
1384 {
1385 bool excl = !!(flags & O_EXCL);
1386
1387 if (d_in_lookup(dentry)) {
1388 struct dentry *d = __gfs2_lookup(dir, dentry, file);
1389 if (file->f_mode & FMODE_OPENED) {
1390 if (IS_ERR(d))
1391 return PTR_ERR(d);
1392 dput(d);
1393 return excl && (flags & O_CREAT) ? -EEXIST : 0;
1394 }
1395 if (d || d_really_is_positive(dentry))
1396 return finish_no_open(file, d);
1397 }
1398 if (!(flags & O_CREAT))
1399 return -ENOENT;
1400
1401 return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl);
1402 }
1403
1404 /*
1405 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1406 * @this: move this
1407 * @to: to here
1408 *
1409 * Follow @to back to the root and make sure we don't encounter @this
1410 * Assumes we already hold the rename lock.
1411 *
1412 * Returns: errno
1413 */
1414
1415 static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1416 {
1417 struct inode *dir = &to->i_inode;
1418 struct super_block *sb = dir->i_sb;
1419 struct inode *tmp;
1420 int error = 0;
1421
1422 igrab(dir);
1423
1424 for (;;) {
1425 if (dir == &this->i_inode) {
1426 error = -EINVAL;
1427 break;
1428 }
1429 if (dir == d_inode(sb->s_root)) {
1430 error = 0;
1431 break;
1432 }
1433
1434 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
1435 if (!tmp) {
1436 error = -ENOENT;
1437 break;
1438 }
1439 if (IS_ERR(tmp)) {
1440 error = PTR_ERR(tmp);
1441 break;
1442 }
1443
1444 iput(dir);
1445 dir = tmp;
1446 }
1447
1448 iput(dir);
1449
1450 return error;
1451 }
1452
1453 /**
1454 * update_moved_ino - Update an inode that's being moved
1455 * @ip: The inode being moved
1456 * @ndip: The parent directory of the new filename
1457 * @dir_rename: True of ip is a directory
1458 *
1459 * Returns: errno
1460 */
1461
1462 static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip,
1463 int dir_rename)
1464 {
1465 if (dir_rename)
1466 return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
1467
1468 inode_set_ctime_current(&ip->i_inode);
1469 mark_inode_dirty_sync(&ip->i_inode);
1470 return 0;
1471 }
1472
1473
1474 /**
1475 * gfs2_rename - Rename a file
1476 * @odir: Parent directory of old file name
1477 * @odentry: The old dentry of the file
1478 * @ndir: Parent directory of new file name
1479 * @ndentry: The new dentry of the file
1480 *
1481 * Returns: errno
1482 */
1483
1484 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
1485 struct inode *ndir, struct dentry *ndentry)
1486 {
1487 struct gfs2_inode *odip = GFS2_I(odir);
1488 struct gfs2_inode *ndip = GFS2_I(ndir);
1489 struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
1490 struct gfs2_inode *nip = NULL;
1491 struct gfs2_sbd *sdp = GFS2_SB(odir);
1492 struct gfs2_holder ghs[4], r_gh, rd_gh;
1493 struct gfs2_rgrpd *nrgd;
1494 unsigned int num_gh;
1495 int dir_rename = 0;
1496 struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
1497 unsigned int retries = 0, x;
1498 int error;
1499
1500 gfs2_holder_mark_uninitialized(&r_gh);
1501 gfs2_holder_mark_uninitialized(&rd_gh);
1502 if (d_really_is_positive(ndentry)) {
1503 nip = GFS2_I(d_inode(ndentry));
1504 if (ip == nip)
1505 return 0;
1506 }
1507
1508 error = gfs2_rindex_update(sdp);
1509 if (error)
1510 return error;
1511
1512 error = gfs2_qa_get(ndip);
1513 if (error)
1514 return error;
1515
1516 if (odip != ndip) {
1517 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1518 0, &r_gh);
1519 if (error)
1520 goto out;
1521
1522 if (S_ISDIR(ip->i_inode.i_mode)) {
1523 dir_rename = 1;
1524 /* don't move a directory into its subdir */
1525 error = gfs2_ok_to_move(ip, ndip);
1526 if (error)
1527 goto out_gunlock_r;
1528 }
1529 }
1530
1531 num_gh = 1;
1532 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
1533 if (odip != ndip) {
1534 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE,GL_ASYNC,
1535 ghs + num_gh);
1536 num_gh++;
1537 }
1538 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1539 num_gh++;
1540
1541 if (nip) {
1542 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1543 ghs + num_gh);
1544 num_gh++;
1545 }
1546
1547 again:
1548 for (x = 0; x < num_gh; x++) {
1549 error = gfs2_glock_nq(ghs + x);
1550 if (error)
1551 goto out_gunlock;
1552 }
1553 error = gfs2_glock_async_wait(num_gh, ghs, retries);
1554 if (error == -ESTALE) {
1555 retries++;
1556 goto again;
1557 }
1558 if (error)
1559 goto out_gunlock;
1560
1561 if (nip) {
1562 /* Grab the resource group glock for unlink flag twiddling.
1563 * This is the case where the target dinode already exists
1564 * so we unlink before doing the rename.
1565 */
1566 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
1567 if (!nrgd) {
1568 error = -ENOENT;
1569 goto out_gunlock;
1570 }
1571 error = gfs2_glock_nq_init(nrgd->rd_gl, LM_ST_EXCLUSIVE,
1572 LM_FLAG_NODE_SCOPE, &rd_gh);
1573 if (error)
1574 goto out_gunlock;
1575 }
1576
1577 error = -ENOENT;
1578 if (ip->i_inode.i_nlink == 0)
1579 goto out_gunlock;
1580
1581 /* Check out the old directory */
1582
1583 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
1584 if (error)
1585 goto out_gunlock;
1586
1587 /* Check out the new directory */
1588
1589 if (nip) {
1590 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1591 if (error)
1592 goto out_gunlock;
1593
1594 if (nip->i_inode.i_nlink == 0) {
1595 error = -EAGAIN;
1596 goto out_gunlock;
1597 }
1598
1599 if (S_ISDIR(nip->i_inode.i_mode)) {
1600 if (nip->i_entries < 2) {
1601 gfs2_consist_inode(nip);
1602 error = -EIO;
1603 goto out_gunlock;
1604 }
1605 if (nip->i_entries > 2) {
1606 error = -ENOTEMPTY;
1607 goto out_gunlock;
1608 }
1609 }
1610 } else {
1611 error = gfs2_permission(&nop_mnt_idmap, ndir,
1612 MAY_WRITE | MAY_EXEC);
1613 if (error)
1614 goto out_gunlock;
1615
1616 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
1617 switch (error) {
1618 case -ENOENT:
1619 error = 0;
1620 break;
1621 case 0:
1622 error = -EEXIST;
1623 goto out_gunlock;
1624 default:
1625 goto out_gunlock;
1626 }
1627
1628 if (odip != ndip) {
1629 if (!ndip->i_inode.i_nlink) {
1630 error = -ENOENT;
1631 goto out_gunlock;
1632 }
1633 if (ndip->i_entries == (u32)-1) {
1634 error = -EFBIG;
1635 goto out_gunlock;
1636 }
1637 if (S_ISDIR(ip->i_inode.i_mode) &&
1638 ndip->i_inode.i_nlink == (u32)-1) {
1639 error = -EMLINK;
1640 goto out_gunlock;
1641 }
1642 }
1643 }
1644
1645 /* Check out the dir to be renamed */
1646
1647 if (dir_rename) {
1648 error = gfs2_permission(&nop_mnt_idmap, d_inode(odentry),
1649 MAY_WRITE);
1650 if (error)
1651 goto out_gunlock;
1652 }
1653
1654 if (nip == NULL) {
1655 error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
1656 if (error)
1657 goto out_gunlock;
1658 }
1659
1660 if (da.nr_blocks) {
1661 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
1662 error = gfs2_quota_lock_check(ndip, &ap);
1663 if (error)
1664 goto out_gunlock;
1665
1666 error = gfs2_inplace_reserve(ndip, &ap);
1667 if (error)
1668 goto out_gunlock_q;
1669
1670 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
1671 4 * RES_LEAF + 4, 0);
1672 if (error)
1673 goto out_ipreserv;
1674 } else {
1675 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
1676 5 * RES_LEAF + 4, 0);
1677 if (error)
1678 goto out_gunlock;
1679 }
1680
1681 /* Remove the target file, if it exists */
1682
1683 if (nip)
1684 error = gfs2_unlink_inode(ndip, ndentry);
1685
1686 error = update_moved_ino(ip, ndip, dir_rename);
1687 if (error)
1688 goto out_end_trans;
1689
1690 error = gfs2_dir_del(odip, odentry);
1691 if (error)
1692 goto out_end_trans;
1693
1694 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
1695 if (error)
1696 goto out_end_trans;
1697
1698 out_end_trans:
1699 gfs2_trans_end(sdp);
1700 out_ipreserv:
1701 if (da.nr_blocks)
1702 gfs2_inplace_release(ndip);
1703 out_gunlock_q:
1704 if (da.nr_blocks)
1705 gfs2_quota_unlock(ndip);
1706 out_gunlock:
1707 gfs2_dir_no_add(&da);
1708 if (gfs2_holder_initialized(&rd_gh))
1709 gfs2_glock_dq_uninit(&rd_gh);
1710
1711 while (x--) {
1712 if (gfs2_holder_queued(ghs + x))
1713 gfs2_glock_dq(ghs + x);
1714 gfs2_holder_uninit(ghs + x);
1715 }
1716 out_gunlock_r:
1717 if (gfs2_holder_initialized(&r_gh))
1718 gfs2_glock_dq_uninit(&r_gh);
1719 out:
1720 gfs2_qa_put(ndip);
1721 return error;
1722 }
1723
1724 /**
1725 * gfs2_exchange - exchange two files
1726 * @odir: Parent directory of old file name
1727 * @odentry: The old dentry of the file
1728 * @ndir: Parent directory of new file name
1729 * @ndentry: The new dentry of the file
1730 * @flags: The rename flags
1731 *
1732 * Returns: errno
1733 */
1734
1735 static int gfs2_exchange(struct inode *odir, struct dentry *odentry,
1736 struct inode *ndir, struct dentry *ndentry,
1737 unsigned int flags)
1738 {
1739 struct gfs2_inode *odip = GFS2_I(odir);
1740 struct gfs2_inode *ndip = GFS2_I(ndir);
1741 struct gfs2_inode *oip = GFS2_I(odentry->d_inode);
1742 struct gfs2_inode *nip = GFS2_I(ndentry->d_inode);
1743 struct gfs2_sbd *sdp = GFS2_SB(odir);
1744 struct gfs2_holder ghs[4], r_gh;
1745 unsigned int num_gh;
1746 unsigned int retries = 0, x;
1747 umode_t old_mode = oip->i_inode.i_mode;
1748 umode_t new_mode = nip->i_inode.i_mode;
1749 int error;
1750
1751 gfs2_holder_mark_uninitialized(&r_gh);
1752 error = gfs2_rindex_update(sdp);
1753 if (error)
1754 return error;
1755
1756 if (odip != ndip) {
1757 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1758 0, &r_gh);
1759 if (error)
1760 goto out;
1761
1762 if (S_ISDIR(old_mode)) {
1763 /* don't move a directory into its subdir */
1764 error = gfs2_ok_to_move(oip, ndip);
1765 if (error)
1766 goto out_gunlock_r;
1767 }
1768
1769 if (S_ISDIR(new_mode)) {
1770 /* don't move a directory into its subdir */
1771 error = gfs2_ok_to_move(nip, odip);
1772 if (error)
1773 goto out_gunlock_r;
1774 }
1775 }
1776
1777 num_gh = 1;
1778 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
1779 if (odip != ndip) {
1780 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1781 ghs + num_gh);
1782 num_gh++;
1783 }
1784 gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1785 num_gh++;
1786
1787 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1788 num_gh++;
1789
1790 again:
1791 for (x = 0; x < num_gh; x++) {
1792 error = gfs2_glock_nq(ghs + x);
1793 if (error)
1794 goto out_gunlock;
1795 }
1796
1797 error = gfs2_glock_async_wait(num_gh, ghs, retries);
1798 if (error == -ESTALE) {
1799 retries++;
1800 goto again;
1801 }
1802 if (error)
1803 goto out_gunlock;
1804
1805 error = -ENOENT;
1806 if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0)
1807 goto out_gunlock;
1808
1809 error = gfs2_unlink_ok(odip, &odentry->d_name, oip);
1810 if (error)
1811 goto out_gunlock;
1812 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1813 if (error)
1814 goto out_gunlock;
1815
1816 if (S_ISDIR(old_mode)) {
1817 error = gfs2_permission(&nop_mnt_idmap, odentry->d_inode,
1818 MAY_WRITE);
1819 if (error)
1820 goto out_gunlock;
1821 }
1822 if (S_ISDIR(new_mode)) {
1823 error = gfs2_permission(&nop_mnt_idmap, ndentry->d_inode,
1824 MAY_WRITE);
1825 if (error)
1826 goto out_gunlock;
1827 }
1828 error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0);
1829 if (error)
1830 goto out_gunlock;
1831
1832 error = update_moved_ino(oip, ndip, S_ISDIR(old_mode));
1833 if (error)
1834 goto out_end_trans;
1835
1836 error = update_moved_ino(nip, odip, S_ISDIR(new_mode));
1837 if (error)
1838 goto out_end_trans;
1839
1840 error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip,
1841 IF2DT(old_mode));
1842 if (error)
1843 goto out_end_trans;
1844
1845 error = gfs2_dir_mvino(odip, &odentry->d_name, nip,
1846 IF2DT(new_mode));
1847 if (error)
1848 goto out_end_trans;
1849
1850 if (odip != ndip) {
1851 if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) {
1852 inc_nlink(&odip->i_inode);
1853 drop_nlink(&ndip->i_inode);
1854 } else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) {
1855 inc_nlink(&ndip->i_inode);
1856 drop_nlink(&odip->i_inode);
1857 }
1858 }
1859 mark_inode_dirty(&ndip->i_inode);
1860 if (odip != ndip)
1861 mark_inode_dirty(&odip->i_inode);
1862
1863 out_end_trans:
1864 gfs2_trans_end(sdp);
1865 out_gunlock:
1866 while (x--) {
1867 if (gfs2_holder_queued(ghs + x))
1868 gfs2_glock_dq(ghs + x);
1869 gfs2_holder_uninit(ghs + x);
1870 }
1871 out_gunlock_r:
1872 if (gfs2_holder_initialized(&r_gh))
1873 gfs2_glock_dq_uninit(&r_gh);
1874 out:
1875 return error;
1876 }
1877
1878 static int gfs2_rename2(struct mnt_idmap *idmap, struct inode *odir,
1879 struct dentry *odentry, struct inode *ndir,
1880 struct dentry *ndentry, unsigned int flags)
1881 {
1882 flags &= ~RENAME_NOREPLACE;
1883
1884 if (flags & ~RENAME_EXCHANGE)
1885 return -EINVAL;
1886
1887 if (flags & RENAME_EXCHANGE)
1888 return gfs2_exchange(odir, odentry, ndir, ndentry, flags);
1889
1890 return gfs2_rename(odir, odentry, ndir, ndentry);
1891 }
1892
1893 /**
1894 * gfs2_get_link - Follow a symbolic link
1895 * @dentry: The dentry of the link
1896 * @inode: The inode of the link
1897 * @done: destructor for return value
1898 *
1899 * This can handle symlinks of any size.
1900 *
1901 * Returns: 0 on success or error code
1902 */
1903
1904 static const char *gfs2_get_link(struct dentry *dentry,
1905 struct inode *inode,
1906 struct delayed_call *done)
1907 {
1908 struct gfs2_inode *ip = GFS2_I(inode);
1909 struct gfs2_holder i_gh;
1910 struct buffer_head *dibh;
1911 unsigned int size;
1912 char *buf;
1913 int error;
1914
1915 if (!dentry)
1916 return ERR_PTR(-ECHILD);
1917
1918 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1919 error = gfs2_glock_nq(&i_gh);
1920 if (error) {
1921 gfs2_holder_uninit(&i_gh);
1922 return ERR_PTR(error);
1923 }
1924
1925 size = (unsigned int)i_size_read(&ip->i_inode);
1926 if (size == 0) {
1927 gfs2_consist_inode(ip);
1928 buf = ERR_PTR(-EIO);
1929 goto out;
1930 }
1931
1932 error = gfs2_meta_inode_buffer(ip, &dibh);
1933 if (error) {
1934 buf = ERR_PTR(error);
1935 goto out;
1936 }
1937
1938 buf = kzalloc(size + 1, GFP_NOFS);
1939 if (!buf)
1940 buf = ERR_PTR(-ENOMEM);
1941 else
1942 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
1943 brelse(dibh);
1944 out:
1945 gfs2_glock_dq_uninit(&i_gh);
1946 if (!IS_ERR(buf))
1947 set_delayed_call(done, kfree_link, buf);
1948 return buf;
1949 }
1950
1951 /**
1952 * gfs2_permission
1953 * @idmap: idmap of the mount the inode was found from
1954 * @inode: The inode
1955 * @mask: The mask to be tested
1956 *
1957 * This may be called from the VFS directly, or from within GFS2 with the
1958 * inode locked, so we look to see if the glock is already locked and only
1959 * lock the glock if its not already been done.
1960 *
1961 * Returns: errno
1962 */
1963
1964 int gfs2_permission(struct mnt_idmap *idmap, struct inode *inode,
1965 int mask)
1966 {
1967 int may_not_block = mask & MAY_NOT_BLOCK;
1968 struct gfs2_inode *ip;
1969 struct gfs2_holder i_gh;
1970 struct gfs2_glock *gl;
1971 int error;
1972
1973 gfs2_holder_mark_uninitialized(&i_gh);
1974 ip = GFS2_I(inode);
1975 gl = rcu_dereference_check(ip->i_gl, !may_not_block);
1976 if (unlikely(!gl)) {
1977 /* inode is getting torn down, must be RCU mode */
1978 WARN_ON_ONCE(!may_not_block);
1979 return -ECHILD;
1980 }
1981 if (gfs2_glock_is_locked_by_me(gl) == NULL) {
1982 if (may_not_block)
1983 return -ECHILD;
1984 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1985 if (error)
1986 return error;
1987 }
1988
1989 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1990 error = -EPERM;
1991 else
1992 error = generic_permission(&nop_mnt_idmap, inode, mask);
1993 if (gfs2_holder_initialized(&i_gh))
1994 gfs2_glock_dq_uninit(&i_gh);
1995
1996 return error;
1997 }
1998
1999 static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
2000 {
2001 setattr_copy(&nop_mnt_idmap, inode, attr);
2002 mark_inode_dirty(inode);
2003 return 0;
2004 }
2005
2006 static int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
2007 {
2008 int error;
2009
2010 if (current->journal_info)
2011 return __gfs2_setattr_simple(inode, attr);
2012
2013 error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
2014 if (error)
2015 return error;
2016
2017 error = __gfs2_setattr_simple(inode, attr);
2018 gfs2_trans_end(GFS2_SB(inode));
2019 return error;
2020 }
2021
2022 static int setattr_chown(struct inode *inode, struct iattr *attr)
2023 {
2024 struct gfs2_inode *ip = GFS2_I(inode);
2025 struct gfs2_sbd *sdp = GFS2_SB(inode);
2026 kuid_t ouid, nuid;
2027 kgid_t ogid, ngid;
2028 int error;
2029 struct gfs2_alloc_parms ap = {};
2030
2031 ouid = inode->i_uid;
2032 ogid = inode->i_gid;
2033 nuid = attr->ia_uid;
2034 ngid = attr->ia_gid;
2035
2036 if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
2037 ouid = nuid = NO_UID_QUOTA_CHANGE;
2038 if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
2039 ogid = ngid = NO_GID_QUOTA_CHANGE;
2040 error = gfs2_qa_get(ip);
2041 if (error)
2042 return error;
2043
2044 error = gfs2_rindex_update(sdp);
2045 if (error)
2046 goto out;
2047
2048 error = gfs2_quota_lock(ip, nuid, ngid);
2049 if (error)
2050 goto out;
2051
2052 ap.target = gfs2_get_inode_blocks(&ip->i_inode);
2053
2054 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
2055 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
2056 error = gfs2_quota_check(ip, nuid, ngid, &ap);
2057 if (error)
2058 goto out_gunlock_q;
2059 }
2060
2061 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
2062 if (error)
2063 goto out_gunlock_q;
2064
2065 error = gfs2_setattr_simple(inode, attr);
2066 if (error)
2067 goto out_end_trans;
2068
2069 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
2070 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
2071 gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
2072 gfs2_quota_change(ip, ap.target, nuid, ngid);
2073 }
2074
2075 out_end_trans:
2076 gfs2_trans_end(sdp);
2077 out_gunlock_q:
2078 gfs2_quota_unlock(ip);
2079 out:
2080 gfs2_qa_put(ip);
2081 return error;
2082 }
2083
2084 /**
2085 * gfs2_setattr - Change attributes on an inode
2086 * @idmap: idmap of the mount the inode was found from
2087 * @dentry: The dentry which is changing
2088 * @attr: The structure describing the change
2089 *
2090 * The VFS layer wants to change one or more of an inodes attributes. Write
2091 * that change out to disk.
2092 *
2093 * Returns: errno
2094 */
2095
2096 static int gfs2_setattr(struct mnt_idmap *idmap,
2097 struct dentry *dentry, struct iattr *attr)
2098 {
2099 struct inode *inode = d_inode(dentry);
2100 struct gfs2_inode *ip = GFS2_I(inode);
2101 struct gfs2_holder i_gh;
2102 int error;
2103
2104 error = gfs2_qa_get(ip);
2105 if (error)
2106 return error;
2107
2108 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
2109 if (error)
2110 goto out;
2111
2112 error = may_setattr(&nop_mnt_idmap, inode, attr->ia_valid);
2113 if (error)
2114 goto error;
2115
2116 error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
2117 if (error)
2118 goto error;
2119
2120 if (attr->ia_valid & ATTR_SIZE)
2121 error = gfs2_setattr_size(inode, attr->ia_size);
2122 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
2123 error = setattr_chown(inode, attr);
2124 else {
2125 error = gfs2_setattr_simple(inode, attr);
2126 if (!error && attr->ia_valid & ATTR_MODE)
2127 error = posix_acl_chmod(&nop_mnt_idmap, dentry,
2128 inode->i_mode);
2129 }
2130
2131 error:
2132 if (!error)
2133 mark_inode_dirty(inode);
2134 gfs2_glock_dq_uninit(&i_gh);
2135 out:
2136 gfs2_qa_put(ip);
2137 return error;
2138 }
2139
2140 /**
2141 * gfs2_getattr - Read out an inode's attributes
2142 * @idmap: idmap of the mount the inode was found from
2143 * @path: Object to query
2144 * @stat: The inode's stats
2145 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
2146 * @flags: AT_STATX_xxx setting
2147 *
2148 * This may be called from the VFS directly, or from within GFS2 with the
2149 * inode locked, so we look to see if the glock is already locked and only
2150 * lock the glock if its not already been done. Note that its the NFS
2151 * readdirplus operation which causes this to be called (from filldir)
2152 * with the glock already held.
2153 *
2154 * Returns: errno
2155 */
2156
2157 static int gfs2_getattr(struct mnt_idmap *idmap,
2158 const struct path *path, struct kstat *stat,
2159 u32 request_mask, unsigned int flags)
2160 {
2161 struct inode *inode = d_inode(path->dentry);
2162 struct gfs2_inode *ip = GFS2_I(inode);
2163 struct gfs2_holder gh;
2164 u32 gfsflags;
2165 int error;
2166
2167 gfs2_holder_mark_uninitialized(&gh);
2168 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
2169 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
2170 if (error)
2171 return error;
2172 }
2173
2174 gfsflags = ip->i_diskflags;
2175 if (gfsflags & GFS2_DIF_APPENDONLY)
2176 stat->attributes |= STATX_ATTR_APPEND;
2177 if (gfsflags & GFS2_DIF_IMMUTABLE)
2178 stat->attributes |= STATX_ATTR_IMMUTABLE;
2179
2180 stat->attributes_mask |= (STATX_ATTR_APPEND |
2181 STATX_ATTR_COMPRESSED |
2182 STATX_ATTR_ENCRYPTED |
2183 STATX_ATTR_IMMUTABLE |
2184 STATX_ATTR_NODUMP);
2185
2186 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2187
2188 if (gfs2_holder_initialized(&gh))
2189 gfs2_glock_dq_uninit(&gh);
2190
2191 return 0;
2192 }
2193
2194 static bool fault_in_fiemap(struct fiemap_extent_info *fi)
2195 {
2196 struct fiemap_extent __user *dest = fi->fi_extents_start;
2197 size_t size = sizeof(*dest) * fi->fi_extents_max;
2198
2199 return fault_in_safe_writeable((char __user *)dest, size) == 0;
2200 }
2201
2202 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2203 u64 start, u64 len)
2204 {
2205 struct gfs2_inode *ip = GFS2_I(inode);
2206 struct gfs2_holder gh;
2207 int ret;
2208
2209 inode_lock_shared(inode);
2210
2211 retry:
2212 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2213 if (ret)
2214 goto out;
2215
2216 pagefault_disable();
2217 ret = iomap_fiemap(inode, fieinfo, start, len, &gfs2_iomap_ops);
2218 pagefault_enable();
2219
2220 gfs2_glock_dq_uninit(&gh);
2221
2222 if (ret == -EFAULT && fault_in_fiemap(fieinfo)) {
2223 fieinfo->fi_extents_mapped = 0;
2224 goto retry;
2225 }
2226
2227 out:
2228 inode_unlock_shared(inode);
2229 return ret;
2230 }
2231
2232 loff_t gfs2_seek_data(struct file *file, loff_t offset)
2233 {
2234 struct inode *inode = file->f_mapping->host;
2235 struct gfs2_inode *ip = GFS2_I(inode);
2236 struct gfs2_holder gh;
2237 loff_t ret;
2238
2239 inode_lock_shared(inode);
2240 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2241 if (!ret)
2242 ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops);
2243 gfs2_glock_dq_uninit(&gh);
2244 inode_unlock_shared(inode);
2245
2246 if (ret < 0)
2247 return ret;
2248 return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2249 }
2250
2251 loff_t gfs2_seek_hole(struct file *file, loff_t offset)
2252 {
2253 struct inode *inode = file->f_mapping->host;
2254 struct gfs2_inode *ip = GFS2_I(inode);
2255 struct gfs2_holder gh;
2256 loff_t ret;
2257
2258 inode_lock_shared(inode);
2259 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2260 if (!ret)
2261 ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops);
2262 gfs2_glock_dq_uninit(&gh);
2263 inode_unlock_shared(inode);
2264
2265 if (ret < 0)
2266 return ret;
2267 return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2268 }
2269
2270 static int gfs2_update_time(struct inode *inode, enum fs_update_time type,
2271 unsigned int flags)
2272 {
2273 struct gfs2_inode *ip = GFS2_I(inode);
2274 struct gfs2_glock *gl = ip->i_gl;
2275 struct gfs2_holder *gh;
2276 int error;
2277
2278 if (flags & IOCB_NOWAIT)
2279 return -EAGAIN;
2280
2281 gh = gfs2_glock_is_locked_by_me(gl);
2282 if (gh && gl->gl_state != LM_ST_EXCLUSIVE) {
2283 gfs2_glock_dq(gh);
2284 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, gh);
2285 error = gfs2_glock_nq(gh);
2286 if (error)
2287 return error;
2288 }
2289 return generic_update_time(inode, type, flags);
2290 }
2291
2292 static const struct inode_operations gfs2_file_iops = {
2293 .permission = gfs2_permission,
2294 .setattr = gfs2_setattr,
2295 .getattr = gfs2_getattr,
2296 .listxattr = gfs2_listxattr,
2297 .fiemap = gfs2_fiemap,
2298 .get_inode_acl = gfs2_get_acl,
2299 .set_acl = gfs2_set_acl,
2300 .update_time = gfs2_update_time,
2301 .fileattr_get = gfs2_fileattr_get,
2302 .fileattr_set = gfs2_fileattr_set,
2303 };
2304
2305 static const struct inode_operations gfs2_dir_iops = {
2306 .create = gfs2_create,
2307 .lookup = gfs2_lookup,
2308 .link = gfs2_link,
2309 .unlink = gfs2_unlink,
2310 .symlink = gfs2_symlink,
2311 .mkdir = gfs2_mkdir,
2312 .rmdir = gfs2_unlink,
2313 .mknod = gfs2_mknod,
2314 .rename = gfs2_rename2,
2315 .permission = gfs2_permission,
2316 .setattr = gfs2_setattr,
2317 .getattr = gfs2_getattr,
2318 .listxattr = gfs2_listxattr,
2319 .fiemap = gfs2_fiemap,
2320 .get_inode_acl = gfs2_get_acl,
2321 .set_acl = gfs2_set_acl,
2322 .update_time = gfs2_update_time,
2323 .atomic_open = gfs2_atomic_open,
2324 .fileattr_get = gfs2_fileattr_get,
2325 .fileattr_set = gfs2_fileattr_set,
2326 };
2327
2328 static const struct inode_operations gfs2_symlink_iops = {
2329 .get_link = gfs2_get_link,
2330 .permission = gfs2_permission,
2331 .setattr = gfs2_setattr,
2332 .getattr = gfs2_getattr,
2333 .listxattr = gfs2_listxattr,
2334 .fiemap = gfs2_fiemap,
2335 };
2336