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