blob: 2e215e8c3c88e57d6ed17ba6cc5cb22420e99af6 [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;
Bob Peterson8c5ca112019-08-06 13:52:21 -040041 int error, valid = 0;
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))
Bob Peterson8c5ca112019-08-06 13:52:21 -040054 goto out;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010055 ip = GFS2_I(inode);
56 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000057
Bob Peterson8c5ca112019-08-06 13:52:21 -040058 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL) {
59 valid = 1;
60 goto out;
61 }
Steven Whitehousec7526662006-03-20 12:30:04 -050062
Steven Whitehouse7afd88d2008-02-22 16:07:18 +000063 had_lock = (gfs2_glock_is_locked_by_me(dip->i_gl) != NULL);
Wendy Cheng549ae0a2007-02-06 03:52:16 -050064 if (!had_lock) {
65 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
66 if (error)
Bob Peterson8c5ca112019-08-06 13:52:21 -040067 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +000068 }
69
Bob Peterson8c5ca112019-08-06 13:52:21 -040070 error = gfs2_dir_check(d_inode(parent), &dentry->d_name, ip);
71 valid = inode ? !error : (error == -ENOENT);
72
Wendy Cheng549ae0a2007-02-06 03:52:16 -050073 if (!had_lock)
74 gfs2_glock_dq_uninit(&d_gh);
Bob Peterson8c5ca112019-08-06 13:52:21 -040075out:
David Teiglandb3b94fa2006-01-16 16:50:04 +000076 dput(parent);
Bob Peterson8c5ca112019-08-06 13:52:21 -040077 return valid;
David Teiglandb3b94fa2006-01-16 16:50:04 +000078}
79
Linus Torvaldsda53be12013-05-21 15:22:44 -070080static int gfs2_dhash(const struct dentry *dentry, struct qstr *str)
Steven Whitehousec7526662006-03-20 12:30:04 -050081{
82 str->hash = gfs2_disk_hash(str->name, str->len);
83 return 0;
84}
85
Nick Pigginfe15ce42011-01-07 17:49:23 +110086static int gfs2_dentry_delete(const struct dentry *dentry)
Wengang Wang970343c2009-08-18 14:25:03 +080087{
88 struct gfs2_inode *ginode;
89
David Howells2b0143b2015-03-17 22:25:59 +000090 if (d_really_is_negative(dentry))
Wengang Wang970343c2009-08-18 14:25:03 +080091 return 0;
92
David Howells2b0143b2015-03-17 22:25:59 +000093 ginode = GFS2_I(d_inode(dentry));
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -050094 if (!gfs2_holder_initialized(&ginode->i_iopen_gh))
Wengang Wang970343c2009-08-18 14:25:03 +080095 return 0;
96
97 if (test_bit(GLF_DEMOTE, &ginode->i_iopen_gh.gh_gl->gl_flags))
98 return 1;
99
100 return 0;
101}
102
Al Viro92cecbb2009-02-20 06:00:05 +0000103const struct dentry_operations gfs2_dops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000104 .d_revalidate = gfs2_drevalidate,
Steven Whitehousec7526662006-03-20 12:30:04 -0500105 .d_hash = gfs2_dhash,
Wengang Wang970343c2009-08-18 14:25:03 +0800106 .d_delete = gfs2_dentry_delete,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000107};
108