Whamcloud - gitweb
e912050a58b07f3f3eb189fbde6f6b7a39ad5d6a
[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         obdo_free(oa);
166
167         if (!inode) {
168                 printk("new_inode -fatal:  %ld\n", (long)oa->o_id);
169                 IOPS(dir, destroy)(IID(dir), oa);
170                 EXIT;
171                 return ERR_PTR(-EIO);
172         }
173
174         if (!list_empty(&inode->i_dentry)) {
175                 printk("new_inode -fatal: aliases %ld, ct %d lnk %d\n", 
176                        (long)oa->o_id,
177                        atomic_read(&inode->i_count), 
178                        inode->i_nlink);
179                 IOPS(dir, destroy)(IID(dir), oa);
180                 iput(inode);
181                 EXIT;
182                 return ERR_PTR(-EIO);
183         }
184
185         EXIT;
186         return inode;
187 } /* obdfs_new_inode */
188
189
190 /*
191  * By the time this is called, we already have created
192  * the directory cache entry for the new file, but it
193  * is so far negative - it has no inode.
194  *
195  * If the create succeeds, we fill in the inode information
196  * with d_instantiate(). 
197  */
198 static int obdfs_create (struct inode * dir, struct dentry * dentry, int mode)
199 {
200         struct inode * inode = obdfs_new_inode (dir, mode);
201         int err = PTR_ERR(inode);
202         if (!IS_ERR(inode)) {
203                 inode->i_op = &obdfs_file_inode_operations;
204                 inode->i_fop = &obdfs_file_operations;
205                 inode->i_mapping->a_ops = &obdfs_aops;
206                 err = ext2_add_nondir(dentry, inode);
207         }
208         return err;
209 } /* obdfs_create */
210
211
212 static int obdfs_mknod (struct inode * dir, struct dentry *dentry, int mode, int rdev)
213 {
214         struct inode * inode = obdfs_new_inode (dir, mode);
215         int err = PTR_ERR(inode);
216         if (!IS_ERR(inode)) {
217                 init_special_inode(inode, mode, rdev);
218                 obdfs_change_inode(inode);
219                 err = ext2_add_nondir(dentry, inode);
220         }
221         return err;
222 }
223
224 static int obdfs_symlink (struct inode * dir, struct dentry * dentry,
225         const char * symname)
226 {
227         struct super_block * sb = dir->i_sb;
228         int err = -ENAMETOOLONG;
229         unsigned l = strlen(symname)+1;
230         struct inode * inode;
231         struct obdfs_inode_info *oinfo;
232
233         if (l > sb->s_blocksize)
234                 goto out;
235
236         inode = obdfs_new_inode (dir, S_IFLNK | S_IRWXUGO);
237         err = PTR_ERR(inode);
238         if (IS_ERR(inode))
239                 goto out;
240
241         oinfo = obdfs_i2info(inode);
242         if (l >= sizeof(oinfo->oi_inline)) {
243                 /* slow symlink */
244                 inode->i_op = &page_symlink_inode_operations;
245                 inode->i_mapping->a_ops = &obdfs_aops;
246                 err = block_symlink(inode, symname, l);
247                 if (err)
248                         goto out_fail;
249         } else {
250                 /* fast symlink */
251                 inode->i_op = &obdfs_fast_symlink_inode_operations;
252                 memcpy(oinfo->oi_inline, symname, l);
253                 inode->i_size = l-1;
254         }
255         obdfs_change_inode(inode);
256
257         err = ext2_add_nondir(dentry, inode);
258 out:
259         return err;
260
261 out_fail:
262         ext2_dec_count(inode);
263         iput (inode);
264         goto out;
265 }
266
267
268
269 static int obdfs_link (struct dentry * old_dentry, struct inode * dir,
270         struct dentry *dentry)
271 {
272         struct inode *inode = old_dentry->d_inode;
273
274         if (S_ISDIR(inode->i_mode))
275                 return -EPERM;
276
277         if (inode->i_nlink >= EXT2_LINK_MAX)
278                 return -EMLINK;
279
280         inode->i_ctime = CURRENT_TIME;
281         ext2_inc_count(inode);
282         atomic_inc(&inode->i_count);
283
284         return ext2_add_nondir(dentry, inode);
285 }
286
287
288 static int obdfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
289 {
290         struct inode * inode;
291         int err = -EMLINK;
292         ENTRY;
293
294         if (dir->i_nlink >= EXT2_LINK_MAX)
295                 goto out;
296
297         ext2_inc_count(dir);
298
299         inode = obdfs_new_inode (dir, S_IFDIR | mode);
300         err = PTR_ERR(inode);
301         if (IS_ERR(inode))
302                 goto out_dir;
303
304         inode->i_op = &obdfs_dir_inode_operations;
305         inode->i_fop = &obdfs_dir_operations;
306         inode->i_mapping->a_ops = &obdfs_aops;
307
308         ext2_inc_count(inode);
309
310         err = ext2_make_empty(inode, dir);
311         if (err)
312                 goto out_fail;
313
314         err = ext2_add_link(dentry, inode);
315         if (err)
316                 goto out_fail;
317
318         d_instantiate(dentry, inode);
319 out:
320         EXIT;
321         return err;
322
323 out_fail:
324         ext2_dec_count(inode);
325         ext2_dec_count(inode);
326         iput(inode);
327         EXIT;
328 out_dir:
329         ext2_dec_count(dir);
330         EXIT;
331         goto out;
332 }
333
334 static int obdfs_unlink(struct inode * dir, struct dentry *dentry)
335 {
336         struct inode * inode = dentry->d_inode;
337         struct ext2_dir_entry_2 * de;
338         struct page * page;
339         int err = -ENOENT;
340
341         de = ext2_find_entry (dir, dentry, &page);
342         if (!de)
343                 goto out;
344
345         err = ext2_delete_entry (de, page);
346         if (err)
347                 goto out;
348
349         inode->i_ctime = dir->i_ctime;
350         ext2_dec_count(inode);
351         err = 0;
352 out:
353         return err;
354 }
355
356
357 static int obdfs_rmdir (struct inode * dir, struct dentry *dentry)
358 {
359         struct inode * inode = dentry->d_inode;
360         int err = -ENOTEMPTY;
361
362         if (ext2_empty_dir(inode)) {
363                 err = obdfs_unlink(dir, dentry);
364                 if (!err) {
365                         inode->i_size = 0;
366                         ext2_dec_count(inode);
367                         ext2_dec_count(dir);
368                 }
369         }
370         return err;
371 }
372
373 static int obdfs_rename (struct inode * old_dir, struct dentry * old_dentry,
374         struct inode * new_dir, struct dentry * new_dentry )
375 {
376         struct inode * old_inode = old_dentry->d_inode;
377         struct inode * new_inode = new_dentry->d_inode;
378         struct page * dir_page = NULL;
379         struct ext2_dir_entry_2 * dir_de = NULL;
380         struct page * old_page;
381         struct ext2_dir_entry_2 * old_de;
382         int err = -ENOENT;
383
384         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
385         if (!old_de)
386                 goto out;
387
388         if (S_ISDIR(old_inode->i_mode)) {
389                 err = -EIO;
390                 dir_de = ext2_dotdot(old_inode, &dir_page);
391                 if (!dir_de)
392                         goto out_old;
393         }
394
395         if (new_inode) {
396                 struct page *new_page;
397                 struct ext2_dir_entry_2 *new_de;
398
399                 err = -ENOTEMPTY;
400                 if (dir_de && !ext2_empty_dir (new_inode))
401                         goto out_dir;
402
403                 err = -ENOENT;
404                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
405                 if (!new_de)
406                         goto out_dir;
407                 ext2_inc_count(old_inode);
408                 ext2_set_link(new_dir, new_de, new_page, old_inode);
409                 new_inode->i_ctime = CURRENT_TIME;
410                 if (dir_de)
411                         new_inode->i_nlink--;
412                 ext2_dec_count(new_inode);
413         } else {
414                 if (dir_de) {
415                         err = -EMLINK;
416                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
417                                 goto out_dir;
418                 }
419                 ext2_inc_count(old_inode);
420                 err = ext2_add_link(new_dentry, old_inode);
421                 if (err) {
422                         ext2_dec_count(old_inode);
423                         goto out_dir;
424                 }
425                 if (dir_de)
426                         ext2_inc_count(new_dir);
427         }
428
429         ext2_delete_entry (old_de, old_page);
430         ext2_dec_count(old_inode);
431
432         if (dir_de) {
433                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
434                 ext2_dec_count(old_dir);
435         }
436         return 0;
437
438
439 out_dir:
440         if (dir_de) {
441                 kunmap(dir_page);
442                 page_cache_release(dir_page);
443         }
444 out_old:
445         kunmap(old_page);
446         page_cache_release(old_page);
447 out:
448         return err;
449 }
450
451 struct inode_operations obdfs_dir_inode_operations = {
452         create:         obdfs_create,
453         lookup:         obdfs_lookup,
454         link:           obdfs_link,
455         unlink:         obdfs_unlink,
456         symlink:        obdfs_symlink,
457         mkdir:          obdfs_mkdir,
458         rmdir:          obdfs_rmdir,
459         mknod:          obdfs_mknod,
460         rename:         obdfs_rename,
461         setattr:        obdfs_setattr
462 };