Whamcloud - gitweb
- Added match_or_enqueue helper function
[fs/lustre-release.git] / lustre / llite / namei.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
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
34 #define DEBUG_SUBSYSTEM S_LLITE
35
36 #include <linux/obd_support.h>
37 #include <linux/lustre_lite.h>
38 #include <linux/lustre_dlm.h>
39 extern struct address_space_operations ll_aops;
40
41 /* from super.c */
42 extern void ll_change_inode(struct inode *inode);
43 extern int ll_setattr(struct dentry *de, struct iattr *attr);
44
45 /* from dir.c */
46 extern int ll_add_link (struct dentry *dentry, struct inode *inode);
47 obd_id ll_inode_by_name(struct inode * dir, struct dentry *dentry, int *typ);
48 int ext2_make_empty(struct inode *inode, struct inode *parent);
49 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
50                    struct dentry *dentry, struct page ** res_page);
51 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page );
52 int ext2_empty_dir (struct inode * inode);
53 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p);
54 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
55                    struct page *page, struct inode *inode);
56
57 /*
58  * Couple of helper functions - make the code slightly cleaner.
59  */
60 static inline void ext2_inc_count(struct inode *inode)
61 {
62         inode->i_nlink++;
63 }
64
65 /* postpone the disk update until the inode really goes away */
66 static inline void ext2_dec_count(struct inode *inode)
67 {
68         inode->i_nlink--;
69 }
70
71 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
72 {
73         int err;
74         err = ll_add_link(dentry, inode);
75         if (!err) {
76                 d_instantiate(dentry, inode);
77                 return 0;
78         }
79         ext2_dec_count(inode);
80         iput(inode);
81         return err;
82 }
83
84 /* methods */
85 static int ll_find_inode(struct inode *inode, unsigned long ino, void *opaque)
86 {
87         struct mds_body *body = (struct mds_body *)opaque;
88
89         if (inode->i_generation != body->generation)
90                 return 0;
91
92         return 1;
93 }
94
95 extern struct dentry_operations ll_d_ops;
96
97 int ll_lock(struct inode *dir, struct dentry *dentry,
98             struct lookup_intent *it, struct lustre_handle *lockh)
99 {
100         struct ll_sb_info *sbi = ll_i2sbi(dir);
101         int err, lock_mode;
102
103         if ((it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_SETATTR |
104                           IT_MKNOD)))
105                 lock_mode = LCK_PW;
106         else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_UNLINK |
107                               IT_RMDIR | IT_RENAME | IT_RENAME2))
108                 lock_mode = LCK_PR;
109         else if (it->it_op & IT_LOOKUP)
110                 lock_mode = LCK_CR;
111         else {
112                 LBUG();
113                 RETURN(-1);
114         }
115
116         err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_MDSINTENT, it, lock_mode, dir,
117                           dentry, lockh, 0, NULL, 0, dir, sizeof(*dir));
118
119         RETURN(err);
120 }
121
122 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
123 {
124         ENTRY;
125
126         ldlm_lock_decref(lockh, mode);
127
128         RETURN(0);
129 }
130
131 static struct dentry *ll_lookup2(struct inode * dir, struct dentry *dentry,
132                                  struct lookup_intent *it)
133 {
134         struct ptlrpc_request *request = NULL;
135         struct inode * inode = NULL;
136         struct ll_sb_info *sbi = ll_i2sbi(dir);
137         struct ll_inode_md md;
138         struct lustre_handle lockh;
139         int err, type, offset;
140         struct lookup_intent lookup_it = { IT_LOOKUP };
141         obd_id ino;
142
143         ENTRY;
144
145         if (it == NULL) {
146                 it = &lookup_it;
147                 dentry->d_it = it;
148         }
149
150         CDEBUG(D_INFO, "name: %*s, intent op: %d\n", dentry->d_name.len,
151                dentry->d_name.name, it->it_op);
152
153         if (dentry->d_name.len > EXT2_NAME_LEN)
154                 RETURN(ERR_PTR(-ENAMETOOLONG));
155
156         err = ll_lock(dir, dentry, it, &lockh);
157         if (err < 0) {
158                 /* FIXME: Mike handle EINTR here */
159                 LBUG();
160                 RETURN(ERR_PTR(err));
161         }
162         memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
163
164         if ((it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) &&
165             it->it_disposition && !it->it_status)
166                 GOTO(negative, NULL);
167
168         if ((it->it_op & (IT_RENAME | IT_GETATTR | IT_UNLINK | IT_RMDIR |
169                           IT_SETATTR | IT_LOOKUP)) && it->it_disposition &&
170             it->it_status)
171                 GOTO(negative, NULL);
172
173         request = (struct ptlrpc_request *)it->it_data;
174         if (!it->it_disposition) {
175                 struct ll_inode_info *lli = ll_i2info(dir);
176                 memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
177
178                 ino = ll_inode_by_name(dir, dentry, &type);
179
180                 err = mdc_getattr(&sbi->ll_mdc_conn, ino, type,
181                                   OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
182                 if (err) {
183                         CERROR("failure %d inode %Ld\n", err, (long long)ino);
184                         ptlrpc_free_req(request);
185                         RETURN(ERR_PTR(-abs(err)));
186                 }
187                 offset = 0;
188         } else if (it->it_op == IT_UNLINK) {
189                 struct obdo *obdo;
190                 obdo = lustre_msg_buf(request->rq_repmsg, 1);
191                 inode = new_inode(dir->i_sb);
192
193                 ll_i2info(inode)->lli_obdo = obdo_alloc();
194                 /* XXX fix mem allocation error */
195                 memcpy(ll_i2info(inode)->lli_obdo, obdo, sizeof(*obdo));
196
197                 if (!inode)
198                         GOTO(out_req, -ENOMEM);
199                 inode->i_mode = S_IFREG;
200                 inode->i_nlink = 1;
201                 GOTO(out_req, 0);
202         } else if (it->it_op == IT_RMDIR) {
203                 inode = new_inode(dir->i_sb);
204                 if (!inode)
205                         GOTO(out_req, -ENOMEM);
206                 ll_i2info(inode)->lli_obdo = NULL;
207                 inode->i_mode = S_IFDIR;
208                 inode->i_nlink = 1;
209                 GOTO(out_req, 0);
210         } else if (it->it_op == IT_RENAME2) {
211                 LBUG();
212         } else {
213                 struct mds_body *body;
214
215                 offset = 1;
216                 body = lustre_msg_buf(request->rq_repmsg, 1);
217                 type = body->mode;
218                 ino = body->fid1.id;
219         }
220
221         if (S_ISREG(type)) {
222                 if (request->rq_repmsg->bufcount < offset + 2 ||
223                     request->rq_repmsg->buflens[offset + 1] !=
224                     sizeof(struct obdo))
225                         LBUG();
226
227                 md.obdo = lustre_msg_buf(request->rq_repmsg, offset + 1);
228         } else
229                 md.obdo = NULL;
230
231         if (!(it->it_op & IT_RENAME2))
232                 md.body = lustre_msg_buf(request->rq_repmsg, offset);
233
234         inode = iget4(dir->i_sb, ino, ll_find_inode, &md);
235
236         if (it->it_op & IT_RENAME)
237                 it->it_data = dentry;
238
239  out_req:
240         ptlrpc_free_req(request);
241         if (!inode)
242                 RETURN(ERR_PTR(-ENOMEM));
243
244         EXIT;
245  negative:
246         dentry->d_op = &ll_d_ops;
247         d_add(dentry, inode);
248         if (it->it_op == IT_LOOKUP)
249                 ll_intent_release(dentry);
250
251         return NULL;
252 }
253
254 static struct inode *ll_create_node(struct inode *dir, const char *name,
255                                     int namelen, const char *tgt, int tgtlen,
256                                     int mode, __u64 extra,
257                                     struct lookup_intent *it, struct obdo *obdo)
258 {
259         struct inode *inode;
260         struct ptlrpc_request *request = NULL;
261         struct mds_body *body;
262         int rc;
263         time_t time = CURRENT_TIME;
264         struct ll_sb_info *sbi = ll_i2sbi(dir);
265         int gid = current->fsgid;
266         struct ll_inode_md md;
267
268         ENTRY;
269
270         if (dir->i_mode & S_ISGID) {
271                 gid = dir->i_gid;
272                 if (S_ISDIR(mode))
273                         mode |= S_ISGID;
274         }
275
276         if (!it->it_disposition) {
277                 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
278                                  tgtlen, mode, current->fsuid,
279                                  gid, time, extra, obdo, &request);
280                 if (rc) {
281                         inode = ERR_PTR(rc);
282                         GOTO(out, rc);
283                 }
284                 body = lustre_msg_buf(request->rq_repmsg, 0);
285                 md.obdo = obdo;
286         } else {
287                 request = it->it_data;
288                 body = lustre_msg_buf(request->rq_repmsg, 1);
289                 md.obdo = NULL;
290         }
291
292         body->valid = OBD_MD_FLNOTOBD;
293
294         body->nlink = 1;
295         body->atime = body->ctime = body->mtime = time;
296         body->uid = current->fsuid;
297         body->gid = gid;
298         body->mode = mode;
299
300         md.body = body;
301
302         inode = iget4(dir->i_sb, body->ino, ll_find_inode, &md);
303         if (IS_ERR(inode)) {
304                 rc = PTR_ERR(inode);
305                 CERROR("new_inode -fatal: rc %d\n", rc);
306                 LBUG();
307                 GOTO(out, rc);
308         }
309
310         if (!list_empty(&inode->i_dentry)) {
311                 CERROR("new_inode -fatal: inode %d, ct %d lnk %d\n",
312                        body->ino, atomic_read(&inode->i_count),
313                        inode->i_nlink);
314                 iput(inode);
315                 LBUG();
316                 inode = ERR_PTR(-EIO);
317                 GOTO(out, -EIO);
318         }
319
320         EXIT;
321  out:
322         ptlrpc_free_req(request);
323         return inode;
324 }
325
326 int ll_mdc_unlink(struct inode *dir, struct inode *child,
327                   const char *name, int len)
328 {
329         struct ptlrpc_request *request = NULL;
330         int err;
331         struct ll_sb_info *sbi = ll_i2sbi(dir);
332
333         ENTRY;
334
335         err = mdc_unlink(&sbi->ll_mdc_conn, dir, child,
336                          name, len, &request);
337         ptlrpc_free_req(request);
338
339         RETURN(err);
340 }
341
342 int ll_mdc_link(struct dentry *src, struct inode *dir,
343                 const char *name, int len)
344 {
345         struct ptlrpc_request *request = NULL;
346         int err;
347         struct ll_sb_info *sbi = ll_i2sbi(dir);
348
349         ENTRY;
350
351         err = mdc_link(&sbi->ll_mdc_conn, src, dir, name,
352                        len, &request);
353         ptlrpc_free_req(request);
354
355         RETURN(err);
356 }
357
358 int ll_mdc_rename(struct inode *src, struct inode *tgt,
359                   struct dentry *old, struct dentry *new)
360 {
361         struct ptlrpc_request *request = NULL;
362         struct ll_sb_info *sbi = ll_i2sbi(src);
363         int err;
364
365         ENTRY;
366
367         err = mdc_rename(&sbi->ll_mdc_conn, src, tgt,
368                          old->d_name.name, old->d_name.len,
369                          new->d_name.name, new->d_name.len, &request);
370         ptlrpc_free_req(request);
371
372         RETURN(err);
373 }
374
375 /*
376  * By the time this is called, we already have created
377  * the directory cache entry for the new file, but it
378  * is so far negative - it has no inode.
379  *
380  * If the create succeeds, we fill in the inode information
381  * with d_instantiate().
382  */
383
384 static int ll_create(struct inode * dir, struct dentry * dentry, int mode)
385 {
386         int err, rc = 0;
387         struct obdo oa;
388         struct inode *inode;
389
390         if (dentry->d_it->it_disposition == 0) {
391                 memset(&oa, 0, sizeof(oa));
392                 oa.o_valid = OBD_MD_FLMODE;
393                 oa.o_mode = S_IFREG | 0600;
394                 rc = obd_create(ll_i2obdconn(dir), &oa);
395                 if (rc)
396                         RETURN(rc);
397         }
398
399         mode = mode | S_IFREG;
400         CDEBUG(D_DENTRY, "name %s mode %o o_id %lld\n",
401                dentry->d_name.name, mode, (unsigned long long)oa.o_id);
402         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
403                                NULL, 0, mode, 0, dentry->d_it, &oa);
404
405         if (IS_ERR(inode)) {
406                 rc = PTR_ERR(inode);
407                 CERROR("error creating MDS object for id %Ld: rc = %d\n",
408                        (unsigned long long)oa.o_id, rc);
409                 GOTO(out_destroy, rc);
410         }
411
412         if (dentry->d_it->it_disposition) {
413                 struct ll_inode_info *ii = ll_i2info(inode);
414                 ii->lli_flags |= OBD_FL_CREATEONOPEN;
415                 memcpy(&ii->lli_intent_lock_handle,
416                        dentry->d_it->it_lock_handle,
417                        sizeof(struct lustre_handle));
418         }
419
420         /* no directory data updates when intents rule */
421         if (dentry->d_it->it_disposition == 0)
422                 rc = ext2_add_nondir(dentry, inode);
423         else
424                 d_instantiate(dentry, inode);
425         RETURN(rc);
426
427 out_destroy:
428         err = obd_destroy(ll_i2obdconn(dir), &oa);
429         if (err)
430                 CERROR("error destroying object %Ld in error path: err = %d\n",
431                        (unsigned long long)oa.o_id, err);
432         return rc;
433 }
434
435 static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
436                     int rdev)
437 {
438         struct inode * inode = ll_create_node(dir, dentry->d_name.name,
439                                               dentry->d_name.len, NULL, 0,
440                                               mode, rdev, NULL, NULL);
441         int err = PTR_ERR(inode);
442         if (!IS_ERR(inode))
443                 err = ext2_add_nondir(dentry, inode);
444         return err;
445 }
446
447 static int ll_symlink(struct inode *dir, struct dentry *dentry,
448                       const char *symname)
449 {
450         int err = -ENAMETOOLONG;
451         unsigned l = strlen(symname);
452         struct inode * inode;
453         struct ll_inode_info *oinfo;
454
455         inode = ll_create_node(dir, dentry->d_name.name,
456                                dentry->d_name.len, symname, l,
457                                S_IFLNK | S_IRWXUGO, 0, dentry->d_it, NULL);
458         err = PTR_ERR(inode);
459         if (IS_ERR(inode))
460                 return err;
461
462         oinfo = ll_i2info(inode);
463
464         OBD_ALLOC(oinfo->lli_symlink_name, l + 1);
465         memcpy(oinfo->lli_symlink_name, symname, l + 1);
466         inode->i_size = l;
467
468         err = ext2_add_nondir(dentry, inode);
469
470         if (err) {
471                 ext2_dec_count(inode);
472                 iput (inode);
473         }
474         return err;
475 }
476
477 static int ll_link(struct dentry * old_dentry, struct inode * dir,
478                    struct dentry *dentry)
479 {
480         int err;
481         struct inode *inode = old_dentry->d_inode;
482
483         if (S_ISDIR(inode->i_mode))
484                 return -EPERM;
485
486         if (inode->i_nlink >= EXT2_LINK_MAX)
487                 return -EMLINK;
488
489         err = ll_mdc_link(old_dentry, dir,
490                           dentry->d_name.name, dentry->d_name.len);
491         if (err) {
492                 EXIT;
493                 return err;
494         }
495
496         inode->i_ctime = CURRENT_TIME;
497         ext2_inc_count(inode);
498         atomic_inc(&inode->i_count);
499
500         return ext2_add_nondir(dentry, inode);
501 }
502
503 static int ll_mkdir(struct inode * dir, struct dentry * dentry, int mode)
504 {
505         struct inode * inode;
506         int err = -EMLINK;
507         ENTRY;
508
509         if (dir->i_nlink >= EXT2_LINK_MAX)
510                 goto out;
511
512         ext2_inc_count(dir);
513
514         inode = ll_create_node (dir, dentry->d_name.name,
515                                 dentry->d_name.len, NULL, 0,
516                                 S_IFDIR | mode, 0, dentry->d_it, NULL);
517         err = PTR_ERR(inode);
518         if (IS_ERR(inode))
519                 goto out_dir;
520
521         inode->i_nlink = 1;
522         ext2_inc_count(inode);
523
524         err = ext2_make_empty(inode, dir);
525         if (err)
526                 goto out_fail;
527
528         /* no directory data updates when intents rule */
529         if (dentry->d_it->it_disposition == 0) {
530                 err = ll_add_link(dentry, inode);
531                 if (err)
532                         goto out_fail;
533         }
534
535         d_instantiate(dentry, inode);
536 out:
537         EXIT;
538         return err;
539
540 out_fail:
541         ext2_dec_count(inode);
542         ext2_dec_count(inode);
543         iput(inode);
544         EXIT;
545 out_dir:
546         ext2_dec_count(dir);
547         EXIT;
548         goto out;
549 }
550
551 static int ll_unlink(struct inode * dir, struct dentry *dentry)
552 {
553         struct inode * inode = dentry->d_inode;
554         struct ext2_dir_entry_2 * de;
555         struct page * page;
556         int err = -ENOENT;
557
558         if (dentry->d_it && dentry->d_it->it_disposition) {
559                 inode->i_nlink = 0;
560                 GOTO(out, err = dentry->d_it->it_status);
561         }
562
563         de = ext2_find_entry (dir, dentry, &page);
564         if (!de)
565                 goto out;
566
567         err = ll_mdc_unlink(dir, dentry->d_inode,
568                             dentry->d_name.name, dentry->d_name.len);
569         if (err)
570                 goto out;
571
572         err = ext2_delete_entry (de, page);
573         if (err)
574                 goto out;
575
576         inode->i_ctime = dir->i_ctime;
577         ext2_dec_count(inode);
578 out:
579         return err;
580 }
581
582 static int ll_rmdir(struct inode * dir, struct dentry *dentry)
583 {
584         struct inode * inode = dentry->d_inode;
585         int err = 0;
586         int intent_did = dentry->d_it && dentry->d_it->it_disposition;
587
588         if (!intent_did) {
589                 if (!ext2_empty_dir(inode))
590                 LBUG();
591
592                 err = ll_unlink(dir, dentry);
593                 if (err)
594                         RETURN(err);
595         } else
596                 err = dentry->d_it->it_status;
597         inode->i_size = 0;
598         ext2_dec_count(inode);
599         ext2_dec_count(dir);
600         RETURN(err);
601 }
602
603 static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
604                      struct inode * new_dir, struct dentry * new_dentry)
605 {
606         struct inode * old_inode = old_dentry->d_inode;
607         struct inode * new_inode = new_dentry->d_inode;
608         struct page * dir_page = NULL;
609         struct ext2_dir_entry_2 * dir_de = NULL;
610         struct page * old_page;
611         struct ext2_dir_entry_2 * old_de;
612         int err = -ENOENT;
613
614         if (new_dentry->d_it && new_dentry->d_it->it_disposition)
615                 GOTO(out, err = new_dentry->d_it->it_status);
616
617         err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
618         if (err)
619                 goto out;
620
621         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
622         if (!old_de)
623                 goto out;
624
625         if (S_ISDIR(old_inode->i_mode)) {
626                 err = -EIO;
627                 dir_de = ext2_dotdot(old_inode, &dir_page);
628                 if (!dir_de)
629                         goto out_old;
630         }
631
632         if (new_inode) {
633                 struct page *new_page;
634                 struct ext2_dir_entry_2 *new_de;
635
636                 err = -ENOTEMPTY;
637                 if (dir_de && !ext2_empty_dir (new_inode))
638                         goto out_dir;
639
640                 err = -ENOENT;
641                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
642                 if (!new_de)
643                         goto out_dir;
644                 ext2_inc_count(old_inode);
645                 ext2_set_link(new_dir, new_de, new_page, old_inode);
646                 new_inode->i_ctime = CURRENT_TIME;
647                 if (dir_de)
648                         new_inode->i_nlink--;
649                 ext2_dec_count(new_inode);
650         } else {
651                 if (dir_de) {
652                         err = -EMLINK;
653                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
654                                 goto out_dir;
655                 }
656                 ext2_inc_count(old_inode);
657                 err = ll_add_link(new_dentry, old_inode);
658                 if (err) {
659                         ext2_dec_count(old_inode);
660                         goto out_dir;
661                 }
662                 if (dir_de)
663                         ext2_inc_count(new_dir);
664         }
665
666         ext2_delete_entry (old_de, old_page);
667         ext2_dec_count(old_inode);
668
669         if (dir_de) {
670                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
671                 ext2_dec_count(old_dir);
672         }
673         return 0;
674
675 out_dir:
676         if (dir_de) {
677                 kunmap(dir_page);
678                 page_cache_release(dir_page);
679         }
680 out_old:
681         kunmap(old_page);
682         page_cache_release(old_page);
683 out:
684         return err;
685 }
686
687 struct inode_operations ll_dir_inode_operations = {
688         create:         ll_create,
689         lookup2:        ll_lookup2,
690         link:           ll_link,
691         unlink:         ll_unlink,
692         symlink:        ll_symlink,
693         mkdir:          ll_mkdir,
694         rmdir:          ll_rmdir,
695         mknod:          ll_mknod,
696         rename:         ll_rename,
697         setattr:        ll_setattr
698 };