Whamcloud - gitweb
- elimininate the system calls from filter obd
[fs/lustre-release.git] / lustre / obdfs / namei.c
1 /*
2  *  linux/fs/obdfs/namei.c
3  *
4  * This code is issued under the GNU General Public License.
5  * See the file COPYING in this distribution
6  *
7  * Copyright (C) 1992, 1993, 1994, 1995
8  * Remy Card (card@masi.ibp.fr)
9  * Laboratoire MASI - Institut Blaise Pascal
10  * Universite Pierre et Marie Curie (Paris VI)
11  *
12  *  from
13  *
14  *  linux/fs/ext2/namei.c
15  *
16  *  Copyright (C) 1991, 1992  Linus Torvalds
17  *
18  *  Big-endian to little-endian byte-swapping/bitmaps by
19  *        David S. Miller (davem@caip.rutgers.edu), 1995
20  *  Directory entry file type support and forward compatibility hooks
21  *      for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
22  * 
23  *  Changes for use in OBDFS
24  *  Copyright (c) 1999, Seagate Technology Inc.
25  *  Copyright (C) 2001, Cluster File Systems, Inc.
26  *                       Rewritten based on recent ext2 page cache use.
27  * 
28  */
29
30 #include <linux/fs.h>
31 #include <linux/locks.h>
32 #include <linux/quotaops.h>
33 #include <linux/obd_support.h>
34 #include <linux/obdfs.h>
35 extern struct address_space_operations obdfs_aops;
36
37 /* from super.c */
38 extern void obdfs_change_inode(struct inode *inode);
39 extern int obdfs_setattr(struct dentry *de, struct iattr *attr);
40
41 /* from dir.c */
42 extern int ext2_add_link (struct dentry *dentry, struct inode *inode);
43 ino_t obdfs_inode_by_name(struct inode * dir, struct dentry *dentry, int *typ);
44 int ext2_make_empty(struct inode *inode, struct inode *parent);
45 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
46                    struct dentry *dentry, struct page ** res_page);
47 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page );
48 int ext2_empty_dir (struct inode * inode);
49 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p);
50 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
51                    struct page *page, struct inode *inode);
52
53 /*
54  * Couple of helper functions - make the code slightly cleaner.
55  */
56 static inline void ext2_inc_count(struct inode *inode)
57 {
58         inode->i_nlink++;
59         obdfs_change_inode(inode);
60 }
61
62 /* postpone the disk update until the inode really goes away */ 
63 static inline void ext2_dec_count(struct inode *inode)
64 {
65         inode->i_nlink--;
66         if (inode->i_nlink > 0) 
67                 obdfs_change_inode(inode);
68 }
69
70 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
71 {
72         int err;
73         err = ext2_add_link(dentry, inode);
74         if (!err) {
75                 d_instantiate(dentry, inode);
76                 return 0;
77         }
78         ext2_dec_count(inode);
79         iput(inode);
80         return err;
81 }
82
83 /* methods */
84 static struct dentry *obdfs_lookup(struct inode * dir, struct dentry *dentry)
85 {
86         struct obdo *oa;
87         struct inode * inode = NULL;
88         int type;
89         ino_t ino;
90         
91         ENTRY;
92         if (dentry->d_name.len > EXT2_NAME_LEN)
93                 return ERR_PTR(-ENAMETOOLONG);
94
95         ino = obdfs_inode_by_name(dir, dentry, &type);
96         if (!ino)
97                 goto negative;
98
99         oa = obdo_fromid(IID(dir), ino, type, 
100                          OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
101         if ( IS_ERR(oa) ) {
102                 printk(__FUNCTION__ ": obdo_fromid failed\n");
103                 EXIT;
104                 return ERR_PTR(-EACCES); 
105         }
106
107         inode = iget4(dir->i_sb, ino, NULL, oa);
108         obdo_free(oa);
109
110         if (!inode) 
111                 return ERR_PTR(-EACCES);
112
113  negative:
114         d_add(dentry, inode);
115         return NULL;
116 }
117
118
119 /*
120  * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
121  *
122  * `len <= EXT2_NAME_LEN' is guaranteed by caller.
123  * `de != NULL' is guaranteed by caller.
124  */
125 static inline int ext2_match (int len, const char * const name,
126                        struct ext2_dir_entry_2 * de)
127 {
128         if (len != de->name_len)
129                 return 0;
130         if (!de->inode)
131                 return 0;
132         return !memcmp(name, de->name, len);
133 }
134
135 static struct inode *obdfs_new_inode(struct inode *dir, int mode)
136 {
137         struct obdo *oa;
138         struct inode *inode;
139         int err;
140
141         ENTRY;
142         oa = obdo_alloc();
143         if (!oa) {
144                 EXIT;
145                 return ERR_PTR(-ENOMEM);
146         }
147
148         /* Send a hint to the create method on the type of file to create */
149         oa->o_mode = mode;
150         oa->o_valid |= OBD_MD_FLMODE;
151         CDEBUG(D_INODE, "\n");
152         err = obd_create(IID(dir), oa);
153         CDEBUG(D_INODE, "\n");
154
155         if ( err ) {
156                 printk("new_inode - fatal: err %d\n", err);
157                 obdo_free(oa);
158                 EXIT;
159                 return ERR_PTR(err);
160         }
161         CDEBUG(D_INODE, "obdo mode %o\n", oa->o_mode);
162
163         inode = iget4(dir->i_sb, (ino_t)oa->o_id, NULL, oa);
164         CDEBUG(D_INODE, "\n");
165
166         if (!inode) {
167                 printk("new_inode -fatal:  %ld\n", (long)oa->o_id);
168                 obd_destroy(IID(dir), oa);
169                 obdo_free(oa);
170                 EXIT;
171                 return ERR_PTR(-EIO);
172         }
173         obdo_free(oa);
174
175         if (!list_empty(&inode->i_dentry)) {
176                 printk("new_inode -fatal: aliases %ld, ct %d lnk %d\n", 
177                        (long)oa->o_id,
178                        atomic_read(&inode->i_count), 
179                        inode->i_nlink);
180                 obd_destroy(IID(dir), oa);
181                 iput(inode);
182                 EXIT;
183                 return ERR_PTR(-EIO);
184         }
185
186         EXIT;
187         return inode;
188 } /* obdfs_new_inode */
189
190
191 /*
192  * By the time this is called, we already have created
193  * the directory cache entry for the new file, but it
194  * is so far negative - it has no inode.
195  *
196  * If the create succeeds, we fill in the inode information
197  * with d_instantiate(). 
198  */
199 static int obdfs_create (struct inode * dir, struct dentry * dentry, int mode)
200 {
201         struct inode * inode = obdfs_new_inode (dir, mode);
202         int err = PTR_ERR(inode);
203         if (!IS_ERR(inode)) {
204                 inode->i_op = &obdfs_file_inode_operations;
205                 inode->i_fop = &obdfs_file_operations;
206                 inode->i_mapping->a_ops = &obdfs_aops;
207                 err = ext2_add_nondir(dentry, inode);
208         }
209         return err;
210 } /* obdfs_create */
211
212
213 static int obdfs_mknod (struct inode * dir, struct dentry *dentry, int mode, int rdev)
214 {
215         struct inode * inode = obdfs_new_inode (dir, mode);
216         int err = PTR_ERR(inode);
217         if (!IS_ERR(inode)) {
218                 init_special_inode(inode, mode, rdev);
219                 obdfs_change_inode(inode);
220                 err = ext2_add_nondir(dentry, inode);
221         }
222         return err;
223 }
224
225 static int obdfs_symlink (struct inode * dir, struct dentry * dentry,
226         const char * symname)
227 {
228         struct super_block * sb = dir->i_sb;
229         int err = -ENAMETOOLONG;
230         unsigned l = strlen(symname)+1;
231         struct inode * inode;
232         struct obdfs_inode_info *oinfo;
233
234         if (l > sb->s_blocksize)
235                 goto out;
236
237         inode = obdfs_new_inode (dir, S_IFLNK | S_IRWXUGO);
238         err = PTR_ERR(inode);
239         if (IS_ERR(inode))
240                 goto out;
241
242         oinfo = obdfs_i2info(inode);
243         if (l >= sizeof(oinfo->oi_inline)) {
244                 /* slow symlink */
245                 inode->i_op = &page_symlink_inode_operations;
246                 inode->i_mapping->a_ops = &obdfs_aops;
247                 err = block_symlink(inode, symname, l);
248                 if (err)
249                         goto out_fail;
250         } else {
251                 /* fast symlink */
252                 inode->i_op = &obdfs_fast_symlink_inode_operations;
253                 memcpy(oinfo->oi_inline, symname, l);
254                 inode->i_size = l-1;
255         }
256         obdfs_change_inode(inode);
257
258         err = ext2_add_nondir(dentry, inode);
259 out:
260         return err;
261
262 out_fail:
263         ext2_dec_count(inode);
264         iput (inode);
265         goto out;
266 }
267
268
269
270 static int obdfs_link (struct dentry * old_dentry, struct inode * dir,
271         struct dentry *dentry)
272 {
273         struct inode *inode = old_dentry->d_inode;
274
275         if (S_ISDIR(inode->i_mode))
276                 return -EPERM;
277
278         if (inode->i_nlink >= EXT2_LINK_MAX)
279                 return -EMLINK;
280
281         inode->i_ctime = CURRENT_TIME;
282         ext2_inc_count(inode);
283         atomic_inc(&inode->i_count);
284
285         return ext2_add_nondir(dentry, inode);
286 }
287
288
289 static int obdfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
290 {
291         struct inode * inode;
292         int err = -EMLINK;
293         ENTRY;
294
295         if (dir->i_nlink >= EXT2_LINK_MAX)
296                 goto out;
297
298         ext2_inc_count(dir);
299
300         inode = obdfs_new_inode (dir, S_IFDIR | mode);
301         err = PTR_ERR(inode);
302         if (IS_ERR(inode))
303                 goto out_dir;
304
305         inode->i_op = &obdfs_dir_inode_operations;
306         inode->i_fop = &obdfs_dir_operations;
307         inode->i_mapping->a_ops = &obdfs_aops;
308
309         ext2_inc_count(inode);
310
311         err = ext2_make_empty(inode, dir);
312         if (err)
313                 goto out_fail;
314
315         err = ext2_add_link(dentry, inode);
316         if (err)
317                 goto out_fail;
318
319         d_instantiate(dentry, inode);
320 out:
321         EXIT;
322         return err;
323
324 out_fail:
325         ext2_dec_count(inode);
326         ext2_dec_count(inode);
327         iput(inode);
328         EXIT;
329 out_dir:
330         ext2_dec_count(dir);
331         EXIT;
332         goto out;
333 }
334
335 static int obdfs_unlink(struct inode * dir, struct dentry *dentry)
336 {
337         struct inode * inode = dentry->d_inode;
338         struct ext2_dir_entry_2 * de;
339         struct page * page;
340         int err = -ENOENT;
341
342         de = ext2_find_entry (dir, dentry, &page);
343         if (!de)
344                 goto out;
345
346         err = ext2_delete_entry (de, page);
347         if (err)
348                 goto out;
349
350         inode->i_ctime = dir->i_ctime;
351         ext2_dec_count(inode);
352         err = 0;
353 out:
354         return err;
355 }
356
357
358 static int obdfs_rmdir (struct inode * dir, struct dentry *dentry)
359 {
360         struct inode * inode = dentry->d_inode;
361         int err = -ENOTEMPTY;
362
363         if (ext2_empty_dir(inode)) {
364                 err = obdfs_unlink(dir, dentry);
365                 if (!err) {
366                         inode->i_size = 0;
367                         ext2_dec_count(inode);
368                         ext2_dec_count(dir);
369                 }
370         }
371         return err;
372 }
373
374 static int obdfs_rename (struct inode * old_dir, struct dentry * old_dentry,
375         struct inode * new_dir, struct dentry * new_dentry )
376 {
377         struct inode * old_inode = old_dentry->d_inode;
378         struct inode * new_inode = new_dentry->d_inode;
379         struct page * dir_page = NULL;
380         struct ext2_dir_entry_2 * dir_de = NULL;
381         struct page * old_page;
382         struct ext2_dir_entry_2 * old_de;
383         int err = -ENOENT;
384
385         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
386         if (!old_de)
387                 goto out;
388
389         if (S_ISDIR(old_inode->i_mode)) {
390                 err = -EIO;
391                 dir_de = ext2_dotdot(old_inode, &dir_page);
392                 if (!dir_de)
393                         goto out_old;
394         }
395
396         if (new_inode) {
397                 struct page *new_page;
398                 struct ext2_dir_entry_2 *new_de;
399
400                 err = -ENOTEMPTY;
401                 if (dir_de && !ext2_empty_dir (new_inode))
402                         goto out_dir;
403
404                 err = -ENOENT;
405                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
406                 if (!new_de)
407                         goto out_dir;
408                 ext2_inc_count(old_inode);
409                 ext2_set_link(new_dir, new_de, new_page, old_inode);
410                 new_inode->i_ctime = CURRENT_TIME;
411                 if (dir_de)
412                         new_inode->i_nlink--;
413                 ext2_dec_count(new_inode);
414         } else {
415                 if (dir_de) {
416                         err = -EMLINK;
417                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
418                                 goto out_dir;
419                 }
420                 ext2_inc_count(old_inode);
421                 err = ext2_add_link(new_dentry, old_inode);
422                 if (err) {
423                         ext2_dec_count(old_inode);
424                         goto out_dir;
425                 }
426                 if (dir_de)
427                         ext2_inc_count(new_dir);
428         }
429
430         ext2_delete_entry (old_de, old_page);
431         ext2_dec_count(old_inode);
432
433         if (dir_de) {
434                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
435                 ext2_dec_count(old_dir);
436         }
437         return 0;
438
439
440 out_dir:
441         if (dir_de) {
442                 kunmap(dir_page);
443                 page_cache_release(dir_page);
444         }
445 out_old:
446         kunmap(old_page);
447         page_cache_release(old_page);
448 out:
449         return err;
450 }
451
452 struct inode_operations obdfs_dir_inode_operations = {
453         create:         obdfs_create,
454         lookup:         obdfs_lookup,
455         link:           obdfs_link,
456         unlink:         obdfs_unlink,
457         symlink:        obdfs_symlink,
458         mkdir:          obdfs_mkdir,
459         rmdir:          obdfs_rmdir,
460         mknod:          obdfs_mknod,
461         rename:         obdfs_rename,
462         setattr:        obdfs_setattr
463 };