Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * namei.c - NILFS pathname lookup operations. |
| 3 | * |
| 4 | * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
Ryusuke Konishi | 4b420ab | 2016-05-23 16:23:09 -0700 | [diff] [blame] | 16 | * Modified for NILFS by Amagai Yoshiji and Ryusuke Konishi. |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 17 | */ |
| 18 | /* |
| 19 | * linux/fs/ext2/namei.c |
| 20 | * |
| 21 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 22 | * Remy Card (card@masi.ibp.fr) |
| 23 | * Laboratoire MASI - Institut Blaise Pascal |
| 24 | * Universite Pierre et Marie Curie (Paris VI) |
| 25 | * |
| 26 | * from |
| 27 | * |
| 28 | * linux/fs/minix/namei.c |
| 29 | * |
| 30 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 31 | * |
| 32 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 33 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
| 34 | */ |
| 35 | |
| 36 | #include <linux/pagemap.h> |
| 37 | #include "nilfs.h" |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 38 | #include "export.h" |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 39 | |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 40 | #define NILFS_FID_SIZE_NON_CONNECTABLE \ |
| 41 | (offsetof(struct nilfs_fid, parent_gen) / 4) |
| 42 | #define NILFS_FID_SIZE_CONNECTABLE (sizeof(struct nilfs_fid) / 4) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 43 | |
| 44 | static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode) |
| 45 | { |
| 46 | int err = nilfs_add_link(dentry, inode); |
Ryusuke Konishi | 4ad364c | 2016-05-23 16:23:25 -0700 | [diff] [blame] | 47 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 48 | if (!err) { |
| 49 | d_instantiate(dentry, inode); |
Ryusuke Konishi | 705304a | 2014-12-10 15:54:34 -0800 | [diff] [blame] | 50 | unlock_new_inode(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | inode_dec_link_count(inode); |
Ryusuke Konishi | 705304a | 2014-12-10 15:54:34 -0800 | [diff] [blame] | 54 | unlock_new_inode(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 55 | iput(inode); |
| 56 | return err; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Methods themselves. |
| 61 | */ |
| 62 | |
| 63 | static struct dentry * |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 64 | nilfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 65 | { |
| 66 | struct inode *inode; |
| 67 | ino_t ino; |
| 68 | |
| 69 | if (dentry->d_name.len > NILFS_NAME_LEN) |
| 70 | return ERR_PTR(-ENAMETOOLONG); |
| 71 | |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 72 | ino = nilfs_inode_by_name(dir, &dentry->d_name); |
Al Viro | a904937 | 2011-07-08 21:20:11 -0400 | [diff] [blame] | 73 | inode = ino ? nilfs_iget(dir->i_sb, NILFS_I(dir)->i_root, ino) : NULL; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 74 | return d_splice_alias(inode, dentry); |
| 75 | } |
| 76 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 77 | /* |
| 78 | * By the time this is called, we already have created |
| 79 | * the directory cache entry for the new file, but it |
| 80 | * is so far negative - it has no inode. |
| 81 | * |
| 82 | * If the create succeeds, we fill in the inode information |
| 83 | * with d_instantiate(). |
| 84 | */ |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 85 | static int nilfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 86 | bool excl) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 87 | { |
| 88 | struct inode *inode; |
| 89 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 90 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 91 | |
| 92 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 93 | if (err) |
| 94 | return err; |
| 95 | inode = nilfs_new_inode(dir, mode); |
| 96 | err = PTR_ERR(inode); |
| 97 | if (!IS_ERR(inode)) { |
| 98 | inode->i_op = &nilfs_file_inode_operations; |
| 99 | inode->i_fop = &nilfs_file_operations; |
| 100 | inode->i_mapping->a_ops = &nilfs_aops; |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 101 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 102 | err = nilfs_add_nondir(dentry, inode); |
| 103 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 104 | if (!err) |
| 105 | err = nilfs_transaction_commit(dir->i_sb); |
| 106 | else |
| 107 | nilfs_transaction_abort(dir->i_sb); |
| 108 | |
| 109 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static int |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 113 | nilfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 114 | { |
| 115 | struct inode *inode; |
| 116 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 117 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 118 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 119 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 120 | if (err) |
| 121 | return err; |
| 122 | inode = nilfs_new_inode(dir, mode); |
| 123 | err = PTR_ERR(inode); |
| 124 | if (!IS_ERR(inode)) { |
| 125 | init_special_inode(inode, inode->i_mode, rdev); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 126 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 127 | err = nilfs_add_nondir(dentry, inode); |
| 128 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 129 | if (!err) |
| 130 | err = nilfs_transaction_commit(dir->i_sb); |
| 131 | else |
| 132 | nilfs_transaction_abort(dir->i_sb); |
| 133 | |
| 134 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static int nilfs_symlink(struct inode *dir, struct dentry *dentry, |
| 138 | const char *symname) |
| 139 | { |
| 140 | struct nilfs_transaction_info ti; |
| 141 | struct super_block *sb = dir->i_sb; |
Ryusuke Konishi | 0c6c44c | 2016-05-23 16:23:39 -0700 | [diff] [blame] | 142 | unsigned int l = strlen(symname) + 1; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 143 | struct inode *inode; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 144 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 145 | |
| 146 | if (l > sb->s_blocksize) |
| 147 | return -ENAMETOOLONG; |
| 148 | |
| 149 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 150 | if (err) |
| 151 | return err; |
| 152 | |
| 153 | inode = nilfs_new_inode(dir, S_IFLNK | S_IRWXUGO); |
| 154 | err = PTR_ERR(inode); |
| 155 | if (IS_ERR(inode)) |
| 156 | goto out; |
| 157 | |
| 158 | /* slow symlink */ |
| 159 | inode->i_op = &nilfs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 160 | inode_nohighmem(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 161 | inode->i_mapping->a_ops = &nilfs_aops; |
| 162 | err = page_symlink(inode, symname, l); |
| 163 | if (err) |
| 164 | goto out_fail; |
| 165 | |
| 166 | /* mark_inode_dirty(inode); */ |
Jiro SEKIBA | 9ca941d | 2009-11-27 19:41:06 +0900 | [diff] [blame] | 167 | /* page_symlink() do this */ |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 168 | |
| 169 | err = nilfs_add_nondir(dentry, inode); |
| 170 | out: |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 171 | if (!err) |
| 172 | err = nilfs_transaction_commit(dir->i_sb); |
| 173 | else |
| 174 | nilfs_transaction_abort(dir->i_sb); |
| 175 | |
| 176 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 177 | |
| 178 | out_fail: |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 179 | drop_nlink(inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 180 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | 705304a | 2014-12-10 15:54:34 -0800 | [diff] [blame] | 181 | unlock_new_inode(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 182 | iput(inode); |
| 183 | goto out; |
| 184 | } |
| 185 | |
| 186 | static int nilfs_link(struct dentry *old_dentry, struct inode *dir, |
| 187 | struct dentry *dentry) |
| 188 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 189 | struct inode *inode = d_inode(old_dentry); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 190 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 191 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 192 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 193 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 194 | if (err) |
| 195 | return err; |
| 196 | |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 197 | inode->i_ctime = current_time(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 198 | inode_inc_link_count(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 199 | ihold(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 200 | |
Ryusuke Konishi | 705304a | 2014-12-10 15:54:34 -0800 | [diff] [blame] | 201 | err = nilfs_add_link(dentry, inode); |
| 202 | if (!err) { |
| 203 | d_instantiate(dentry, inode); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 204 | err = nilfs_transaction_commit(dir->i_sb); |
Ryusuke Konishi | 705304a | 2014-12-10 15:54:34 -0800 | [diff] [blame] | 205 | } else { |
| 206 | inode_dec_link_count(inode); |
| 207 | iput(inode); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 208 | nilfs_transaction_abort(dir->i_sb); |
Ryusuke Konishi | 705304a | 2014-12-10 15:54:34 -0800 | [diff] [blame] | 209 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 210 | |
| 211 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 214 | static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 215 | { |
| 216 | struct inode *inode; |
| 217 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 218 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 219 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 220 | err = nilfs_transaction_begin(dir->i_sb, &ti, 1); |
| 221 | if (err) |
| 222 | return err; |
| 223 | |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 224 | inc_nlink(dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 225 | |
| 226 | inode = nilfs_new_inode(dir, S_IFDIR | mode); |
| 227 | err = PTR_ERR(inode); |
| 228 | if (IS_ERR(inode)) |
| 229 | goto out_dir; |
| 230 | |
| 231 | inode->i_op = &nilfs_dir_inode_operations; |
| 232 | inode->i_fop = &nilfs_dir_operations; |
| 233 | inode->i_mapping->a_ops = &nilfs_aops; |
| 234 | |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 235 | inc_nlink(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 236 | |
| 237 | err = nilfs_make_empty(inode, dir); |
| 238 | if (err) |
| 239 | goto out_fail; |
| 240 | |
| 241 | err = nilfs_add_link(dentry, inode); |
| 242 | if (err) |
| 243 | goto out_fail; |
| 244 | |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 245 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 246 | d_instantiate(dentry, inode); |
Ryusuke Konishi | 705304a | 2014-12-10 15:54:34 -0800 | [diff] [blame] | 247 | unlock_new_inode(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 248 | out: |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 249 | if (!err) |
| 250 | err = nilfs_transaction_commit(dir->i_sb); |
| 251 | else |
| 252 | nilfs_transaction_abort(dir->i_sb); |
| 253 | |
| 254 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 255 | |
| 256 | out_fail: |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 257 | drop_nlink(inode); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 258 | drop_nlink(inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 259 | nilfs_mark_inode_dirty(inode); |
Ryusuke Konishi | 705304a | 2014-12-10 15:54:34 -0800 | [diff] [blame] | 260 | unlock_new_inode(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 261 | iput(inode); |
| 262 | out_dir: |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 263 | drop_nlink(dir); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 264 | nilfs_mark_inode_dirty(dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 265 | goto out; |
| 266 | } |
| 267 | |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 268 | static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 269 | { |
| 270 | struct inode *inode; |
| 271 | struct nilfs_dir_entry *de; |
| 272 | struct page *page; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 273 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 274 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 275 | err = -ENOENT; |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 276 | de = nilfs_find_entry(dir, &dentry->d_name, &page); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 277 | if (!de) |
| 278 | goto out; |
| 279 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 280 | inode = d_inode(dentry); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 281 | err = -EIO; |
| 282 | if (le64_to_cpu(de->inode) != inode->i_ino) |
| 283 | goto out; |
| 284 | |
| 285 | if (!inode->i_nlink) { |
Ryusuke Konishi | d6517de | 2016-08-02 14:05:14 -0700 | [diff] [blame] | 286 | nilfs_msg(inode->i_sb, KERN_WARNING, |
| 287 | "deleting nonexistent file (ino=%lu), %d", |
| 288 | inode->i_ino, inode->i_nlink); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 289 | set_nlink(inode, 1); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 290 | } |
| 291 | err = nilfs_delete_entry(de, page); |
| 292 | if (err) |
| 293 | goto out; |
| 294 | |
| 295 | inode->i_ctime = dir->i_ctime; |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 296 | drop_nlink(inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 297 | err = 0; |
| 298 | out: |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 299 | return err; |
| 300 | } |
| 301 | |
| 302 | static int nilfs_unlink(struct inode *dir, struct dentry *dentry) |
| 303 | { |
| 304 | struct nilfs_transaction_info ti; |
| 305 | int err; |
| 306 | |
| 307 | err = nilfs_transaction_begin(dir->i_sb, &ti, 0); |
| 308 | if (err) |
| 309 | return err; |
| 310 | |
| 311 | err = nilfs_do_unlink(dir, dentry); |
| 312 | |
| 313 | if (!err) { |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 314 | nilfs_mark_inode_dirty(dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 315 | nilfs_mark_inode_dirty(d_inode(dentry)); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 316 | err = nilfs_transaction_commit(dir->i_sb); |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 317 | } else |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 318 | nilfs_transaction_abort(dir->i_sb); |
| 319 | |
| 320 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static int nilfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 324 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 325 | struct inode *inode = d_inode(dentry); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 326 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 327 | int err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 328 | |
| 329 | err = nilfs_transaction_begin(dir->i_sb, &ti, 0); |
| 330 | if (err) |
| 331 | return err; |
| 332 | |
| 333 | err = -ENOTEMPTY; |
| 334 | if (nilfs_empty_dir(inode)) { |
Jiro SEKIBA | 4cd76c3 | 2009-11-27 19:41:10 +0900 | [diff] [blame] | 335 | err = nilfs_do_unlink(dir, dentry); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 336 | if (!err) { |
| 337 | inode->i_size = 0; |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 338 | drop_nlink(inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 339 | nilfs_mark_inode_dirty(inode); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 340 | drop_nlink(dir); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 341 | nilfs_mark_inode_dirty(dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 344 | if (!err) |
| 345 | err = nilfs_transaction_commit(dir->i_sb); |
| 346 | else |
| 347 | nilfs_transaction_abort(dir->i_sb); |
| 348 | |
| 349 | return err; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Miklos Szeredi | f03b8ad | 2016-09-27 11:03:57 +0200 | [diff] [blame] | 353 | struct inode *new_dir, struct dentry *new_dentry, |
| 354 | unsigned int flags) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 355 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 356 | struct inode *old_inode = d_inode(old_dentry); |
| 357 | struct inode *new_inode = d_inode(new_dentry); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 358 | struct page *dir_page = NULL; |
| 359 | struct nilfs_dir_entry *dir_de = NULL; |
| 360 | struct page *old_page; |
| 361 | struct nilfs_dir_entry *old_de; |
| 362 | struct nilfs_transaction_info ti; |
| 363 | int err; |
| 364 | |
Miklos Szeredi | f03b8ad | 2016-09-27 11:03:57 +0200 | [diff] [blame] | 365 | if (flags & ~RENAME_NOREPLACE) |
| 366 | return -EINVAL; |
| 367 | |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 368 | err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1); |
| 369 | if (unlikely(err)) |
| 370 | return err; |
| 371 | |
| 372 | err = -ENOENT; |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 373 | old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 374 | if (!old_de) |
| 375 | goto out; |
| 376 | |
| 377 | if (S_ISDIR(old_inode->i_mode)) { |
| 378 | err = -EIO; |
| 379 | dir_de = nilfs_dotdot(old_inode, &dir_page); |
| 380 | if (!dir_de) |
| 381 | goto out_old; |
| 382 | } |
| 383 | |
| 384 | if (new_inode) { |
| 385 | struct page *new_page; |
| 386 | struct nilfs_dir_entry *new_de; |
| 387 | |
| 388 | err = -ENOTEMPTY; |
| 389 | if (dir_de && !nilfs_empty_dir(new_inode)) |
| 390 | goto out_dir; |
| 391 | |
| 392 | err = -ENOENT; |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 393 | new_de = nilfs_find_entry(new_dir, &new_dentry->d_name, &new_page); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 394 | if (!new_de) |
| 395 | goto out_dir; |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 396 | nilfs_set_link(new_dir, new_de, new_page, old_inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 397 | nilfs_mark_inode_dirty(new_dir); |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 398 | new_inode->i_ctime = current_time(new_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 399 | if (dir_de) |
| 400 | drop_nlink(new_inode); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 401 | drop_nlink(new_inode); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 402 | nilfs_mark_inode_dirty(new_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 403 | } else { |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 404 | err = nilfs_add_link(new_dentry, old_inode); |
Al Viro | 30eb43d | 2011-03-02 12:01:13 -0500 | [diff] [blame] | 405 | if (err) |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 406 | goto out_dir; |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 407 | if (dir_de) { |
| 408 | inc_nlink(new_dir); |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 409 | nilfs_mark_inode_dirty(new_dir); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 410 | } |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Like most other Unix systems, set the ctime for inodes on a |
| 415 | * rename. |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 416 | */ |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 417 | old_inode->i_ctime = current_time(old_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 418 | |
| 419 | nilfs_delete_entry(old_de, old_page); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 420 | |
| 421 | if (dir_de) { |
| 422 | nilfs_set_link(old_inode, dir_de, dir_page, new_dir); |
Jiro SEKIBA | 565de40 | 2009-11-27 19:41:08 +0900 | [diff] [blame] | 423 | drop_nlink(old_dir); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 424 | } |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 425 | nilfs_mark_inode_dirty(old_dir); |
| 426 | nilfs_mark_inode_dirty(old_inode); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 427 | |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 428 | err = nilfs_transaction_commit(old_dir->i_sb); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 429 | return err; |
| 430 | |
| 431 | out_dir: |
| 432 | if (dir_de) { |
| 433 | kunmap(dir_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 434 | put_page(dir_page); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 435 | } |
| 436 | out_old: |
| 437 | kunmap(old_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 438 | put_page(old_page); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 439 | out: |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 440 | nilfs_transaction_abort(old_dir->i_sb); |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 441 | return err; |
| 442 | } |
| 443 | |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 444 | /* |
| 445 | * Export operations |
| 446 | */ |
| 447 | static struct dentry *nilfs_get_parent(struct dentry *child) |
| 448 | { |
| 449 | unsigned long ino; |
| 450 | struct inode *inode; |
Linus Torvalds | 26fe575 | 2012-05-10 13:14:12 -0700 | [diff] [blame] | 451 | struct qstr dotdot = QSTR_INIT("..", 2); |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 452 | struct nilfs_root *root; |
| 453 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 454 | ino = nilfs_inode_by_name(d_inode(child), &dotdot); |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 455 | if (!ino) |
| 456 | return ERR_PTR(-ENOENT); |
| 457 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 458 | root = NILFS_I(d_inode(child))->i_root; |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 459 | |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 460 | inode = nilfs_iget(child->d_sb, root, ino); |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 461 | if (IS_ERR(inode)) |
| 462 | return ERR_CAST(inode); |
| 463 | |
| 464 | return d_obtain_alias(inode); |
| 465 | } |
| 466 | |
| 467 | static struct dentry *nilfs_get_dentry(struct super_block *sb, u64 cno, |
| 468 | u64 ino, u32 gen) |
| 469 | { |
| 470 | struct nilfs_root *root; |
| 471 | struct inode *inode; |
| 472 | |
| 473 | if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO) |
| 474 | return ERR_PTR(-ESTALE); |
| 475 | |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 476 | root = nilfs_lookup_root(sb->s_fs_info, cno); |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 477 | if (!root) |
| 478 | return ERR_PTR(-ESTALE); |
| 479 | |
| 480 | inode = nilfs_iget(sb, root, ino); |
| 481 | nilfs_put_root(root); |
| 482 | |
| 483 | if (IS_ERR(inode)) |
| 484 | return ERR_CAST(inode); |
| 485 | if (gen && inode->i_generation != gen) { |
| 486 | iput(inode); |
| 487 | return ERR_PTR(-ESTALE); |
| 488 | } |
| 489 | return d_obtain_alias(inode); |
| 490 | } |
| 491 | |
| 492 | static struct dentry *nilfs_fh_to_dentry(struct super_block *sb, struct fid *fh, |
| 493 | int fh_len, int fh_type) |
| 494 | { |
| 495 | struct nilfs_fid *fid = (struct nilfs_fid *)fh; |
| 496 | |
NeilBrown | f73c2f1 | 2015-06-25 15:03:45 -0700 | [diff] [blame] | 497 | if (fh_len < NILFS_FID_SIZE_NON_CONNECTABLE || |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 498 | (fh_type != FILEID_NILFS_WITH_PARENT && |
| 499 | fh_type != FILEID_NILFS_WITHOUT_PARENT)) |
| 500 | return NULL; |
| 501 | |
| 502 | return nilfs_get_dentry(sb, fid->cno, fid->ino, fid->gen); |
| 503 | } |
| 504 | |
| 505 | static struct dentry *nilfs_fh_to_parent(struct super_block *sb, struct fid *fh, |
| 506 | int fh_len, int fh_type) |
| 507 | { |
| 508 | struct nilfs_fid *fid = (struct nilfs_fid *)fh; |
| 509 | |
NeilBrown | f73c2f1 | 2015-06-25 15:03:45 -0700 | [diff] [blame] | 510 | if (fh_len < NILFS_FID_SIZE_CONNECTABLE || |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 511 | fh_type != FILEID_NILFS_WITH_PARENT) |
| 512 | return NULL; |
| 513 | |
| 514 | return nilfs_get_dentry(sb, fid->cno, fid->parent_ino, fid->parent_gen); |
| 515 | } |
| 516 | |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 517 | static int nilfs_encode_fh(struct inode *inode, __u32 *fh, int *lenp, |
| 518 | struct inode *parent) |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 519 | { |
| 520 | struct nilfs_fid *fid = (struct nilfs_fid *)fh; |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 521 | struct nilfs_root *root = NILFS_I(inode)->i_root; |
| 522 | int type; |
| 523 | |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 524 | if (parent && *lenp < NILFS_FID_SIZE_CONNECTABLE) { |
| 525 | *lenp = NILFS_FID_SIZE_CONNECTABLE; |
Namjae Jeon | 94e07a75 | 2013-02-17 15:48:11 +0900 | [diff] [blame] | 526 | return FILEID_INVALID; |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 527 | } |
| 528 | if (*lenp < NILFS_FID_SIZE_NON_CONNECTABLE) { |
| 529 | *lenp = NILFS_FID_SIZE_NON_CONNECTABLE; |
Namjae Jeon | 94e07a75 | 2013-02-17 15:48:11 +0900 | [diff] [blame] | 530 | return FILEID_INVALID; |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 531 | } |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 532 | |
| 533 | fid->cno = root->cno; |
| 534 | fid->ino = inode->i_ino; |
| 535 | fid->gen = inode->i_generation; |
| 536 | |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 537 | if (parent) { |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 538 | fid->parent_ino = parent->i_ino; |
| 539 | fid->parent_gen = parent->i_generation; |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 540 | type = FILEID_NILFS_WITH_PARENT; |
| 541 | *lenp = NILFS_FID_SIZE_CONNECTABLE; |
| 542 | } else { |
| 543 | type = FILEID_NILFS_WITHOUT_PARENT; |
| 544 | *lenp = NILFS_FID_SIZE_NON_CONNECTABLE; |
| 545 | } |
| 546 | |
| 547 | return type; |
| 548 | } |
| 549 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 550 | const struct inode_operations nilfs_dir_inode_operations = { |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 551 | .create = nilfs_create, |
| 552 | .lookup = nilfs_lookup, |
| 553 | .link = nilfs_link, |
| 554 | .unlink = nilfs_unlink, |
| 555 | .symlink = nilfs_symlink, |
| 556 | .mkdir = nilfs_mkdir, |
| 557 | .rmdir = nilfs_rmdir, |
| 558 | .mknod = nilfs_mknod, |
| 559 | .rename = nilfs_rename, |
| 560 | .setattr = nilfs_setattr, |
| 561 | .permission = nilfs_permission, |
Ryusuke Konishi | 622daaff | 2010-12-26 16:38:43 +0900 | [diff] [blame] | 562 | .fiemap = nilfs_fiemap, |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 563 | }; |
| 564 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 565 | const struct inode_operations nilfs_special_inode_operations = { |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 566 | .setattr = nilfs_setattr, |
| 567 | .permission = nilfs_permission, |
| 568 | }; |
| 569 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 570 | const struct inode_operations nilfs_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 571 | .get_link = page_get_link, |
Ryusuke Konishi | dc3d3b8 | 2010-08-15 23:33:57 +0900 | [diff] [blame] | 572 | .permission = nilfs_permission, |
Ryusuke Konishi | d250065 | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 573 | }; |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 574 | |
| 575 | const struct export_operations nilfs_export_ops = { |
| 576 | .encode_fh = nilfs_encode_fh, |
| 577 | .fh_to_dentry = nilfs_fh_to_dentry, |
| 578 | .fh_to_parent = nilfs_fh_to_parent, |
| 579 | .get_parent = nilfs_get_parent, |
| 580 | }; |