blob: 1ec8ae5995a59c94d4dd5b6f1f41054b461b7c59 [file] [log] [blame]
Ryusuke Konishid2500652009-04-06 19:01:34 -07001/*
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 Konishi4b420ab2016-05-23 16:23:09 -070016 * Modified for NILFS by Amagai Yoshiji and Ryusuke Konishi.
Ryusuke Konishid2500652009-04-06 19:01:34 -070017 */
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 Konishi8e656fd2010-08-27 00:23:02 +090038#include "export.h"
Ryusuke Konishid2500652009-04-06 19:01:34 -070039
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +090040#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 Konishid2500652009-04-06 19:01:34 -070043
44static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode)
45{
46 int err = nilfs_add_link(dentry, inode);
Ryusuke Konishi4ad364c2016-05-23 16:23:25 -070047
Ryusuke Konishid2500652009-04-06 19:01:34 -070048 if (!err) {
49 d_instantiate(dentry, inode);
Ryusuke Konishi705304a2014-12-10 15:54:34 -080050 unlock_new_inode(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -070051 return 0;
52 }
53 inode_dec_link_count(inode);
Ryusuke Konishi705304a2014-12-10 15:54:34 -080054 unlock_new_inode(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -070055 iput(inode);
56 return err;
57}
58
59/*
60 * Methods themselves.
61 */
62
63static struct dentry *
Al Viro00cd8dd2012-06-10 17:13:09 -040064nilfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Ryusuke Konishid2500652009-04-06 19:01:34 -070065{
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 Viro03190032010-01-31 21:02:09 -050072 ino = nilfs_inode_by_name(dir, &dentry->d_name);
Al Viroa9049372011-07-08 21:20:11 -040073 inode = ino ? nilfs_iget(dir->i_sb, NILFS_I(dir)->i_root, ino) : NULL;
Ryusuke Konishid2500652009-04-06 19:01:34 -070074 return d_splice_alias(inode, dentry);
75}
76
Ryusuke Konishid2500652009-04-06 19:01:34 -070077/*
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 Viro4acdaf22011-07-26 01:42:34 -040085static int nilfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -040086 bool excl)
Ryusuke Konishid2500652009-04-06 19:01:34 -070087{
88 struct inode *inode;
89 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -070090 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -070091
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 SEKIBAabdb3182009-11-27 19:41:14 +0900101 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700102 err = nilfs_add_nondir(dentry, inode);
103 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700104 if (!err)
105 err = nilfs_transaction_commit(dir->i_sb);
106 else
107 nilfs_transaction_abort(dir->i_sb);
108
109 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700110}
111
112static int
Al Viro1a67aaf2011-07-26 01:52:52 -0400113nilfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700114{
115 struct inode *inode;
116 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700117 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700118
Ryusuke Konishid2500652009-04-06 19:01:34 -0700119 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 SEKIBAabdb3182009-11-27 19:41:14 +0900126 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700127 err = nilfs_add_nondir(dentry, inode);
128 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700129 if (!err)
130 err = nilfs_transaction_commit(dir->i_sb);
131 else
132 nilfs_transaction_abort(dir->i_sb);
133
134 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700135}
136
137static 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 Konishi0c6c44c2016-05-23 16:23:39 -0700142 unsigned int l = strlen(symname) + 1;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700143 struct inode *inode;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700144 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700145
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 Viro21fc61c2015-11-17 01:07:57 -0500160 inode_nohighmem(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700161 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 SEKIBA9ca941d2009-11-27 19:41:06 +0900167 /* page_symlink() do this */
Ryusuke Konishid2500652009-04-06 19:01:34 -0700168
169 err = nilfs_add_nondir(dentry, inode);
170out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700171 if (!err)
172 err = nilfs_transaction_commit(dir->i_sb);
173 else
174 nilfs_transaction_abort(dir->i_sb);
175
176 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700177
178out_fail:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900179 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900180 nilfs_mark_inode_dirty(inode);
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800181 unlock_new_inode(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700182 iput(inode);
183 goto out;
184}
185
186static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
187 struct dentry *dentry)
188{
David Howells2b0143b2015-03-17 22:25:59 +0000189 struct inode *inode = d_inode(old_dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700190 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700191 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700192
Ryusuke Konishid2500652009-04-06 19:01:34 -0700193 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
194 if (err)
195 return err;
196
197 inode->i_ctime = CURRENT_TIME;
198 inode_inc_link_count(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400199 ihold(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700200
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800201 err = nilfs_add_link(dentry, inode);
202 if (!err) {
203 d_instantiate(dentry, inode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700204 err = nilfs_transaction_commit(dir->i_sb);
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800205 } else {
206 inode_dec_link_count(inode);
207 iput(inode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700208 nilfs_transaction_abort(dir->i_sb);
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800209 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700210
211 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700212}
213
Al Viro18bb1db2011-07-26 01:41:39 -0400214static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700215{
216 struct inode *inode;
217 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700218 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700219
Ryusuke Konishid2500652009-04-06 19:01:34 -0700220 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
221 if (err)
222 return err;
223
Jiro SEKIBA565de402009-11-27 19:41:08 +0900224 inc_nlink(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700225
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 SEKIBA565de402009-11-27 19:41:08 +0900235 inc_nlink(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700236
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 SEKIBAabdb3182009-11-27 19:41:14 +0900245 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700246 d_instantiate(dentry, inode);
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800247 unlock_new_inode(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700248out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700249 if (!err)
250 err = nilfs_transaction_commit(dir->i_sb);
251 else
252 nilfs_transaction_abort(dir->i_sb);
253
254 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700255
256out_fail:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900257 drop_nlink(inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900258 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900259 nilfs_mark_inode_dirty(inode);
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800260 unlock_new_inode(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700261 iput(inode);
262out_dir:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900263 drop_nlink(dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900264 nilfs_mark_inode_dirty(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700265 goto out;
266}
267
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900268static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700269{
270 struct inode *inode;
271 struct nilfs_dir_entry *de;
272 struct page *page;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700273 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700274
Ryusuke Konishid2500652009-04-06 19:01:34 -0700275 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500276 de = nilfs_find_entry(dir, &dentry->d_name, &page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700277 if (!de)
278 goto out;
279
David Howells2b0143b2015-03-17 22:25:59 +0000280 inode = d_inode(dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700281 err = -EIO;
282 if (le64_to_cpu(de->inode) != inode->i_ino)
283 goto out;
284
285 if (!inode->i_nlink) {
286 nilfs_warning(inode->i_sb, __func__,
Ryusuke Konishi06f4abf2016-05-23 16:23:31 -0700287 "deleting nonexistent file (%lu), %d",
Ryusuke Konishid2500652009-04-06 19:01:34 -0700288 inode->i_ino, inode->i_nlink);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200289 set_nlink(inode, 1);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700290 }
291 err = nilfs_delete_entry(de, page);
292 if (err)
293 goto out;
294
295 inode->i_ctime = dir->i_ctime;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900296 drop_nlink(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700297 err = 0;
298out:
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900299 return err;
300}
301
302static 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 SEKIBAabdb3182009-11-27 19:41:14 +0900314 nilfs_mark_inode_dirty(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000315 nilfs_mark_inode_dirty(d_inode(dentry));
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700316 err = nilfs_transaction_commit(dir->i_sb);
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900317 } else
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700318 nilfs_transaction_abort(dir->i_sb);
319
320 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700321}
322
323static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
324{
David Howells2b0143b2015-03-17 22:25:59 +0000325 struct inode *inode = d_inode(dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700326 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700327 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700328
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 SEKIBA4cd76c32009-11-27 19:41:10 +0900335 err = nilfs_do_unlink(dir, dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700336 if (!err) {
337 inode->i_size = 0;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900338 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900339 nilfs_mark_inode_dirty(inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900340 drop_nlink(dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900341 nilfs_mark_inode_dirty(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700342 }
343 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700344 if (!err)
345 err = nilfs_transaction_commit(dir->i_sb);
346 else
347 nilfs_transaction_abort(dir->i_sb);
348
349 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700350}
351
352static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry,
353 struct inode *new_dir, struct dentry *new_dentry)
354{
David Howells2b0143b2015-03-17 22:25:59 +0000355 struct inode *old_inode = d_inode(old_dentry);
356 struct inode *new_inode = d_inode(new_dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700357 struct page *dir_page = NULL;
358 struct nilfs_dir_entry *dir_de = NULL;
359 struct page *old_page;
360 struct nilfs_dir_entry *old_de;
361 struct nilfs_transaction_info ti;
362 int err;
363
364 err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
365 if (unlikely(err))
366 return err;
367
368 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500369 old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700370 if (!old_de)
371 goto out;
372
373 if (S_ISDIR(old_inode->i_mode)) {
374 err = -EIO;
375 dir_de = nilfs_dotdot(old_inode, &dir_page);
376 if (!dir_de)
377 goto out_old;
378 }
379
380 if (new_inode) {
381 struct page *new_page;
382 struct nilfs_dir_entry *new_de;
383
384 err = -ENOTEMPTY;
385 if (dir_de && !nilfs_empty_dir(new_inode))
386 goto out_dir;
387
388 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500389 new_de = nilfs_find_entry(new_dir, &new_dentry->d_name, &new_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700390 if (!new_de)
391 goto out_dir;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700392 nilfs_set_link(new_dir, new_de, new_page, old_inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900393 nilfs_mark_inode_dirty(new_dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700394 new_inode->i_ctime = CURRENT_TIME;
395 if (dir_de)
396 drop_nlink(new_inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900397 drop_nlink(new_inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900398 nilfs_mark_inode_dirty(new_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700399 } else {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700400 err = nilfs_add_link(new_dentry, old_inode);
Al Viro30eb43d2011-03-02 12:01:13 -0500401 if (err)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700402 goto out_dir;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900403 if (dir_de) {
404 inc_nlink(new_dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900405 nilfs_mark_inode_dirty(new_dir);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900406 }
Ryusuke Konishid2500652009-04-06 19:01:34 -0700407 }
408
409 /*
410 * Like most other Unix systems, set the ctime for inodes on a
411 * rename.
Ryusuke Konishid2500652009-04-06 19:01:34 -0700412 */
413 old_inode->i_ctime = CURRENT_TIME;
414
415 nilfs_delete_entry(old_de, old_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700416
417 if (dir_de) {
418 nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900419 drop_nlink(old_dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700420 }
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900421 nilfs_mark_inode_dirty(old_dir);
422 nilfs_mark_inode_dirty(old_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700423
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700424 err = nilfs_transaction_commit(old_dir->i_sb);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700425 return err;
426
427out_dir:
428 if (dir_de) {
429 kunmap(dir_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300430 put_page(dir_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700431 }
432out_old:
433 kunmap(old_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300434 put_page(old_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700435out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700436 nilfs_transaction_abort(old_dir->i_sb);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700437 return err;
438}
439
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900440/*
441 * Export operations
442 */
443static struct dentry *nilfs_get_parent(struct dentry *child)
444{
445 unsigned long ino;
446 struct inode *inode;
Linus Torvalds26fe5752012-05-10 13:14:12 -0700447 struct qstr dotdot = QSTR_INIT("..", 2);
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900448 struct nilfs_root *root;
449
David Howells2b0143b2015-03-17 22:25:59 +0000450 ino = nilfs_inode_by_name(d_inode(child), &dotdot);
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900451 if (!ino)
452 return ERR_PTR(-ENOENT);
453
David Howells2b0143b2015-03-17 22:25:59 +0000454 root = NILFS_I(d_inode(child))->i_root;
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900455
Al Virofc640052016-04-10 01:33:30 -0400456 inode = nilfs_iget(child->d_sb, root, ino);
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900457 if (IS_ERR(inode))
458 return ERR_CAST(inode);
459
460 return d_obtain_alias(inode);
461}
462
463static struct dentry *nilfs_get_dentry(struct super_block *sb, u64 cno,
464 u64 ino, u32 gen)
465{
466 struct nilfs_root *root;
467 struct inode *inode;
468
469 if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO)
470 return ERR_PTR(-ESTALE);
471
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900472 root = nilfs_lookup_root(sb->s_fs_info, cno);
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900473 if (!root)
474 return ERR_PTR(-ESTALE);
475
476 inode = nilfs_iget(sb, root, ino);
477 nilfs_put_root(root);
478
479 if (IS_ERR(inode))
480 return ERR_CAST(inode);
481 if (gen && inode->i_generation != gen) {
482 iput(inode);
483 return ERR_PTR(-ESTALE);
484 }
485 return d_obtain_alias(inode);
486}
487
488static struct dentry *nilfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
489 int fh_len, int fh_type)
490{
491 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
492
NeilBrownf73c2f12015-06-25 15:03:45 -0700493 if (fh_len < NILFS_FID_SIZE_NON_CONNECTABLE ||
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900494 (fh_type != FILEID_NILFS_WITH_PARENT &&
495 fh_type != FILEID_NILFS_WITHOUT_PARENT))
496 return NULL;
497
498 return nilfs_get_dentry(sb, fid->cno, fid->ino, fid->gen);
499}
500
501static struct dentry *nilfs_fh_to_parent(struct super_block *sb, struct fid *fh,
502 int fh_len, int fh_type)
503{
504 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
505
NeilBrownf73c2f12015-06-25 15:03:45 -0700506 if (fh_len < NILFS_FID_SIZE_CONNECTABLE ||
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900507 fh_type != FILEID_NILFS_WITH_PARENT)
508 return NULL;
509
510 return nilfs_get_dentry(sb, fid->cno, fid->parent_ino, fid->parent_gen);
511}
512
Al Virob0b03822012-04-02 14:34:06 -0400513static int nilfs_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
514 struct inode *parent)
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900515{
516 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900517 struct nilfs_root *root = NILFS_I(inode)->i_root;
518 int type;
519
Al Virob0b03822012-04-02 14:34:06 -0400520 if (parent && *lenp < NILFS_FID_SIZE_CONNECTABLE) {
521 *lenp = NILFS_FID_SIZE_CONNECTABLE;
Namjae Jeon94e07a752013-02-17 15:48:11 +0900522 return FILEID_INVALID;
Al Virob0b03822012-04-02 14:34:06 -0400523 }
524 if (*lenp < NILFS_FID_SIZE_NON_CONNECTABLE) {
525 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
Namjae Jeon94e07a752013-02-17 15:48:11 +0900526 return FILEID_INVALID;
Al Virob0b03822012-04-02 14:34:06 -0400527 }
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900528
529 fid->cno = root->cno;
530 fid->ino = inode->i_ino;
531 fid->gen = inode->i_generation;
532
Al Virob0b03822012-04-02 14:34:06 -0400533 if (parent) {
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900534 fid->parent_ino = parent->i_ino;
535 fid->parent_gen = parent->i_generation;
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900536 type = FILEID_NILFS_WITH_PARENT;
537 *lenp = NILFS_FID_SIZE_CONNECTABLE;
538 } else {
539 type = FILEID_NILFS_WITHOUT_PARENT;
540 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
541 }
542
543 return type;
544}
545
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700546const struct inode_operations nilfs_dir_inode_operations = {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700547 .create = nilfs_create,
548 .lookup = nilfs_lookup,
549 .link = nilfs_link,
550 .unlink = nilfs_unlink,
551 .symlink = nilfs_symlink,
552 .mkdir = nilfs_mkdir,
553 .rmdir = nilfs_rmdir,
554 .mknod = nilfs_mknod,
555 .rename = nilfs_rename,
556 .setattr = nilfs_setattr,
557 .permission = nilfs_permission,
Ryusuke Konishi622daaff2010-12-26 16:38:43 +0900558 .fiemap = nilfs_fiemap,
Ryusuke Konishid2500652009-04-06 19:01:34 -0700559};
560
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700561const struct inode_operations nilfs_special_inode_operations = {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700562 .setattr = nilfs_setattr,
563 .permission = nilfs_permission,
564};
565
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700566const struct inode_operations nilfs_symlink_inode_operations = {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700567 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -0500568 .get_link = page_get_link,
Ryusuke Konishidc3d3b82010-08-15 23:33:57 +0900569 .permission = nilfs_permission,
Ryusuke Konishid2500652009-04-06 19:01:34 -0700570};
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900571
572const struct export_operations nilfs_export_ops = {
573 .encode_fh = nilfs_encode_fh,
574 .fh_to_dentry = nilfs_fh_to_dentry,
575 .fh_to_parent = nilfs_fh_to_parent,
576 .get_parent = nilfs_get_parent,
577};