blob: a7bb76e9a82b111e1c6870082fe3ffd7ab3bef3b [file] [log] [blame]
Thomas Gleixner7336d0e2019-05-31 01:09:56 -07001// SPDX-License-Identifier: GPL-2.0-only
David Teiglandb3b94fa2006-01-16 16:50:04 +00002/*
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04004 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00005 */
6
David Teiglandb3b94fa2006-01-16 16:50:04 +00007#include <linux/spinlock.h>
8#include <linux/completion.h>
9#include <linux/buffer_head.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050010#include <linux/gfs2_ondisk.h>
Nick Piggin34286d62011-01-07 17:49:57 +110011#include <linux/namei.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050012#include <linux/crc32.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000013
14#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050015#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000016#include "dir.h"
17#include "glock.h"
Steven Whitehouseb2760582008-10-14 16:05:55 +010018#include "super.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include "util.h"
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010020#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000021
22/**
23 * gfs2_drevalidate - Check directory lookup consistency
24 * @dentry: the mapping to check
Al Viro0b728e12012-06-10 16:03:43 -040025 * @flags: lookup flags
David Teiglandb3b94fa2006-01-16 16:50:04 +000026 *
27 * Check to make sure the lookup necessary to arrive at this inode from its
28 * parent is still good.
29 *
30 * Returns: 1 if the dentry is ok, 0 if it isn't
31 */
32
Al Viro0b728e12012-06-10 16:03:43 -040033static int gfs2_drevalidate(struct dentry *dentry, unsigned int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +000034{
Nick Piggin34286d62011-01-07 17:49:57 +110035 struct dentry *parent;
36 struct gfs2_sbd *sdp;
37 struct gfs2_inode *dip;
38 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +000039 struct gfs2_holder d_gh;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010040 struct gfs2_inode *ip = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000041 int error;
Steven Whitehouse7afd88d2008-02-22 16:07:18 +000042 int had_lock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +000043
Al Viro0b728e12012-06-10 16:03:43 -040044 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +110045 return -ECHILD;
46
47 parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +000048 sdp = GFS2_SB(d_inode(parent));
49 dip = GFS2_I(d_inode(parent));
50 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +110051
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010052 if (inode) {
53 if (is_bad_inode(inode))
54 goto invalid;
55 ip = GFS2_I(inode);
56 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000057
Steven Whitehousec2048b002010-09-23 14:00:31 +010058 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
Steven Whitehousec7526662006-03-20 12:30:04 -050059 goto valid;
60
Steven Whitehouse7afd88d2008-02-22 16:07:18 +000061 had_lock = (gfs2_glock_is_locked_by_me(dip->i_gl) != NULL);
Wendy Cheng549ae0a2007-02-06 03:52:16 -050062 if (!had_lock) {
63 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
64 if (error)
65 goto fail;
66 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000067
David Howells2b0143b2015-03-17 22:25:59 +000068 error = gfs2_dir_check(d_inode(parent), &dentry->d_name, ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +000069 switch (error) {
70 case 0:
71 if (!inode)
72 goto invalid_gunlock;
73 break;
74 case -ENOENT:
75 if (!inode)
76 goto valid_gunlock;
77 goto invalid_gunlock;
78 default:
79 goto fail_gunlock;
80 }
81
Steven Whitehousea91ea692006-09-04 12:04:26 -040082valid_gunlock:
Wendy Cheng549ae0a2007-02-06 03:52:16 -050083 if (!had_lock)
84 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -040085valid:
David Teiglandb3b94fa2006-01-16 16:50:04 +000086 dput(parent);
87 return 1;
88
Steven Whitehousea91ea692006-09-04 12:04:26 -040089invalid_gunlock:
Wendy Cheng549ae0a2007-02-06 03:52:16 -050090 if (!had_lock)
91 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -040092invalid:
David Teiglandb3b94fa2006-01-16 16:50:04 +000093 dput(parent);
94 return 0;
95
Steven Whitehousea91ea692006-09-04 12:04:26 -040096fail_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +000097 gfs2_glock_dq_uninit(&d_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -040098fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +000099 dput(parent);
100 return 0;
101}
102
Linus Torvaldsda53be12013-05-21 15:22:44 -0700103static int gfs2_dhash(const struct dentry *dentry, struct qstr *str)
Steven Whitehousec7526662006-03-20 12:30:04 -0500104{
105 str->hash = gfs2_disk_hash(str->name, str->len);
106 return 0;
107}
108
Nick Pigginfe15ce42011-01-07 17:49:23 +1100109static int gfs2_dentry_delete(const struct dentry *dentry)
Wengang Wang970343c2009-08-18 14:25:03 +0800110{
111 struct gfs2_inode *ginode;
112
David Howells2b0143b2015-03-17 22:25:59 +0000113 if (d_really_is_negative(dentry))
Wengang Wang970343c2009-08-18 14:25:03 +0800114 return 0;
115
David Howells2b0143b2015-03-17 22:25:59 +0000116 ginode = GFS2_I(d_inode(dentry));
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500117 if (!gfs2_holder_initialized(&ginode->i_iopen_gh))
Wengang Wang970343c2009-08-18 14:25:03 +0800118 return 0;
119
120 if (test_bit(GLF_DEMOTE, &ginode->i_iopen_gh.gh_gl->gl_flags))
121 return 1;
122
123 return 0;
124}
125
Al Viro92cecbb2009-02-20 06:00:05 +0000126const struct dentry_operations gfs2_dops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127 .d_revalidate = gfs2_drevalidate,
Steven Whitehousec7526662006-03-20 12:30:04 -0500128 .d_hash = gfs2_dhash,
Wengang Wang970343c2009-08-18 14:25:03 +0800129 .d_delete = gfs2_dentry_delete,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000130};
131