Whamcloud - gitweb
update lmv_placement_policy(). For directories it uses policy 'by name', for
[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  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/fs.h>
23 #include <linux/sched.h>
24 #include <linux/mm.h>
25 #include <linux/smp_lock.h>
26 #include <linux/quotaops.h>
27 #include <linux/highmem.h>
28 #include <linux/pagemap.h>
29
30 #define DEBUG_SUBSYSTEM S_LLITE
31
32 #include <obd_support.h>
33 #include <lustre_lite.h>
34 #include <lustre_dlm.h>
35 #include <lustre_ver.h>
36 #include <lustre_mdc.h>
37 #include "llite_internal.h"
38
39 /* methods */
40
41 extern struct dentry_operations ll_d_ops;
42
43 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
44 {
45         ENTRY;
46
47         ldlm_lock_decref(lockh, mode);
48
49         RETURN(0);
50 }
51
52 /* Get an inode by inode number (already instantiated by the intent lookup).
53  * Returns inode or NULL
54  */
55 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
56 int ll_set_inode(struct inode *inode, void *opaque)
57 {
58         ll_read_inode2(inode, opaque);
59         return 0;
60 }
61
62 struct inode *ll_iget(struct super_block *sb, ino_t hash,
63                       struct lustre_md *md)
64 {
65         struct ll_inode_info *lli;
66         struct inode *inode;
67         LASSERT(hash != 0);
68
69         inode = iget_locked(sb, hash);
70         if (inode) {
71                 if (inode->i_state & I_NEW) {
72                         lli = ll_i2info(inode);
73                         ll_read_inode2(inode, md);
74                         unlock_new_inode(inode);
75                 } else {
76                         ll_update_inode(inode, md);
77                 }
78                 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n",
79                        inode->i_ino, inode->i_generation, inode);
80         }
81
82         return inode;
83 }
84 #else
85 struct inode *ll_iget(struct super_block *sb, ino_t hash,
86                       struct lustre_md *md)
87 {
88         struct inode *inode;
89         LASSERT(hash != 0);
90
91         inode = iget4(sb, hash, NULL, md);
92         if (inode) {
93                 if (!(inode->i_state & (I_FREEING | I_CLEAR)))
94                         ll_update_inode(inode, md);
95
96                 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n",
97                        inode->i_ino, inode->i_generation, inode);
98         }
99         return inode;
100 }
101 #endif
102
103 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
104                        void *data, int flag)
105 {
106         int rc;
107         struct lustre_handle lockh;
108         ENTRY;
109
110         switch (flag) {
111         case LDLM_CB_BLOCKING:
112                 ldlm_lock2handle(lock, &lockh);
113                 rc = ldlm_cli_cancel(&lockh);
114                 if (rc < 0) {
115                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
116                         RETURN(rc);
117                 }
118                 break;
119         case LDLM_CB_CANCELING: {
120                 struct inode *inode = ll_inode_from_lock(lock);
121                 __u64 bits = lock->l_policy_data.l_inodebits.bits;
122
123                 /* Invalidate all dentries associated with this inode */
124                 if (inode == NULL)
125                         break;
126
127                 if (lock->l_resource->lr_name.name[0] != fid_seq(ll_inode2fid(inode)) ||
128                     lock->l_resource->lr_name.name[1] != fid_oid(ll_inode2fid(inode)) ||
129                     lock->l_resource->lr_name.name[2] != fid_ver(ll_inode2fid(inode))) {
130                         LDLM_ERROR(lock, "data mismatch with object "DFID" (%p)",
131                                    PFID(ll_inode2fid(inode)), inode);
132                 }
133
134                 if (bits & MDS_INODELOCK_UPDATE)
135                         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK,
136                                   &(ll_i2info(inode)->lli_flags));
137
138
139                 if (S_ISDIR(inode->i_mode) &&
140                      (bits & MDS_INODELOCK_UPDATE))  {
141                         CDEBUG(D_INODE, "invalidating inode %lu\n",
142                                inode->i_ino);
143                         truncate_inode_pages(inode->i_mapping, 0);
144                 }
145
146                 if (inode->i_sb->s_root &&
147                     inode != inode->i_sb->s_root->d_inode &&
148                     (bits & MDS_INODELOCK_LOOKUP))
149                         ll_unhash_aliases(inode);
150                 iput(inode);
151                 break;
152         }
153         default:
154                 LBUG();
155         }
156
157         RETURN(0);
158 }
159
160 /* Pack the required supplementary groups into the supplied groups array.
161  * If we don't need to use the groups from the target inode(s) then we
162  * instead pack one or more groups from the user's supplementary group
163  * array in case it might be useful.  Not needed if doing an MDS-side upcall. */
164 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
165 {
166         int i;
167
168         LASSERT(i1 != NULL);
169         LASSERT(suppgids != NULL);
170
171         if (in_group_p(i1->i_gid))
172                 suppgids[0] = i1->i_gid;
173         else
174                 suppgids[0] = -1;
175
176         if (i2) {
177                 if (in_group_p(i2->i_gid))
178                         suppgids[1] = i2->i_gid;
179                 else
180                         suppgids[1] = -1;
181         } else {
182                 suppgids[1] = -1;
183         }
184
185         for (i = 0; i < current_ngroups; i++) {
186                 if (suppgids[0] == -1) {
187                         if (current_groups[i] != suppgids[1])
188                                 suppgids[0] = current_groups[i];
189                         continue;
190                 }
191                 if (suppgids[1] == -1) {
192                         if (current_groups[i] != suppgids[0])
193                                 suppgids[1] = current_groups[i];
194                         continue;
195                 }
196                 break;
197         }
198 }
199
200 /*
201  * this function prepares md_op_data hint for passing ot down to MD stack.
202  *
203  * Note: it zeroes @op_data out before doing anything else, so all additional
204  * initializations of @op_data should be done after it.
205  */
206 void ll_prepare_md_op_data(struct md_op_data *op_data, struct inode *i1,
207                             struct inode *i2, const char *name, int namelen,
208                             int mode)
209 {
210         LASSERT(i1 != NULL);
211         LASSERT(op_data != NULL);
212
213         memset(op_data, 0, sizeof(*op_data));
214         ll_i2gids(op_data->suppgids, i1, i2);
215         op_data->fid1 = ll_i2info(i1)->lli_fid;
216
217         /* @i2 may be NULL. In this case caller itself has to initialize ->fid2
218          * if needed. */
219         if (i2)
220                 op_data->fid2 = ll_i2info(i2)->lli_fid;
221
222         op_data->name = name;
223         op_data->namelen = namelen;
224         op_data->create_mode = mode;
225         op_data->mod_time = CURRENT_SECONDS;
226 }
227
228 static void ll_d_add(struct dentry *de, struct inode *inode)
229 {
230         CDEBUG(D_DENTRY, "adding inode %p to dentry %p\n", inode, de);
231         /* d_instantiate */
232         if (!list_empty(&de->d_alias)) {
233                 spin_unlock(&dcache_lock);
234                 CERROR("dentry %.*s %p alias next %p, prev %p\n",
235                        de->d_name.len, de->d_name.name, de,
236                        de->d_alias.next, de->d_alias.prev);
237                 LBUG();
238         }
239         if (inode)
240                 list_add(&de->d_alias, &inode->i_dentry);
241         de->d_inode = inode;
242
243         /* d_rehash */
244         if (!d_unhashed(de)) {
245                 spin_unlock(&dcache_lock);
246                 CERROR("dentry %.*s %p hash next %p\n",
247                        de->d_name.len, de->d_name.name, de, de->d_hash.next);
248                 LBUG();
249         }
250         __d_rehash(de, 0);
251 }
252
253 /* 2.6.15 and prior versions have buggy d_instantiate_unique that leaks an inode
254  * if suitable alias is found. But we are not going to fix it by just freeing
255  * such inode, because if some vendor's kernel contains this bugfix already,
256  * we will break everything then. We will use our own reimplementation
257  * instead. */
258 #if !defined(HAVE_D_ADD_UNIQUE) || (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16))
259 /* Search "inode"'s alias list for a dentry that has the same name and parent as
260  * de.  If found, return it.  If not found, return de. */
261 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
262 {
263         struct list_head *tmp;
264
265         spin_lock(&dcache_lock);
266         list_for_each(tmp, &inode->i_dentry) {
267                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
268
269                 /* We are called here with 'de' already on the aliases list. */
270                 if (dentry == de) {
271                         CERROR("whoops\n");
272                         continue;
273                 }
274
275                 if (dentry->d_parent != de->d_parent)
276                         continue;
277
278                 if (dentry->d_name.len != de->d_name.len)
279                         continue;
280
281                 if (memcmp(dentry->d_name.name, de->d_name.name,
282                            de->d_name.len) != 0)
283                         continue;
284
285                 dget_locked(dentry);
286                 lock_dentry(dentry);
287                 __d_drop(dentry);
288                 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
289                 unlock_dentry(dentry);
290                 __d_rehash(dentry, 0); /* avoid taking dcache_lock inside */
291                 spin_unlock(&dcache_lock);
292                 iput(inode);
293                 CDEBUG(D_DENTRY, "alias dentry %.*s (%p) parent %p inode %p "
294                        "refc %d\n", de->d_name.len, de->d_name.name, de,
295                        de->d_parent, de->d_inode, atomic_read(&de->d_count));
296                 return dentry;
297         }
298
299         ll_d_add(de, inode);
300
301         spin_unlock(&dcache_lock);
302
303         return de;
304 }
305 #else
306 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
307 {
308         struct dentry *dentry;
309
310         dentry = d_add_unique(de, inode);
311         if (dentry) {
312                 lock_dentry(dentry);
313                 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
314                 unlock_dentry(dentry);
315         }
316
317         return dentry?dentry:de;
318 }
319 #endif
320
321 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
322                             struct lookup_intent *it, void *data)
323 {
324         struct it_cb_data *icbd = data;
325         struct dentry **de = icbd->icbd_childp;
326         struct inode *parent = icbd->icbd_parent;
327         struct ll_sb_info *sbi = ll_i2sbi(parent);
328         struct inode *inode = NULL;
329         int rc;
330
331         /* NB 1 request reference will be taken away by ll_intent_lock()
332          * when I return */
333         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
334                 ENTRY;
335
336                 rc = ll_prep_inode(&inode, request, offset,
337                                    (*de)->d_sb);
338                 if (rc)
339                         RETURN(rc);
340
341                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
342                        inode, inode->i_ino, inode->i_generation);
343                 md_set_lock_data(sbi->ll_md_exp,
344                                  &it->d.lustre.it_lock_handle, inode);
345
346                 /* We used to query real size from OSTs here, but actually
347                    this is not needed. For stat() calls size would be updated
348                    from subsequent do_revalidate()->ll_inode_revalidate_it() in
349                    2.4 and
350                    vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
351                    Everybody else who needs correct file size would call
352                    ll_glimpse_size or some equivalent themselves anyway.
353                    Also see bug 7198. */
354
355                 *de = ll_find_alias(inode, *de);
356         } else {
357                 ENTRY;
358                 /* Check that parent has UPDATE lock. If there is none, we
359                  * cannot afford to hash this dentry (done by ll_d_add) as it
360                  * might get picked up later when UPDATE lock will appear */
361                 if (ll_have_md_lock(parent, MDS_INODELOCK_UPDATE)) {
362                         spin_lock(&dcache_lock);
363                         ll_d_add(*de, inode);
364                         spin_unlock(&dcache_lock);
365                 } else
366                         (*de)->d_inode = NULL;
367         }
368
369         ll_set_dd(*de);
370         (*de)->d_op = &ll_d_ops;
371
372         RETURN(0);
373 }
374
375 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
376                                    struct lookup_intent *it, int lookup_flags)
377 {
378         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
379         struct dentry *save = dentry, *retval;
380         struct ptlrpc_request *req = NULL;
381         struct md_op_data *op_data;
382         struct it_cb_data icbd;
383         int rc;
384         ENTRY;
385
386         if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
387                 RETURN(ERR_PTR(-ENAMETOOLONG));
388
389         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
390                dentry->d_name.len, dentry->d_name.name, parent->i_ino,
391                parent->i_generation, parent, LL_IT2STR(it));
392
393         if (d_mountpoint(dentry))
394                 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
395
396         ll_frob_intent(&it, &lookup_it);
397
398         icbd.icbd_childp = &dentry;
399         icbd.icbd_parent = parent;
400
401         OBD_ALLOC_PTR(op_data);
402         if (op_data == NULL)
403                 RETURN(ERR_PTR(-ENOMEM));
404
405         /* prepare operatoin hint first */
406         ll_prepare_md_op_data(op_data, parent, NULL, dentry->d_name.name,
407                               dentry->d_name.len, lookup_flags);
408
409         /* allocate new fid for child */
410         if (it->it_op & IT_CREAT ||
411             (it->it_op & IT_OPEN && it->it_create_mode & O_CREAT)) {
412                 struct lu_placement_hint hint = { .ph_pname = NULL,
413                                                   .ph_pfid = ll_inode2fid(parent),
414                                                   .ph_cname = &dentry->d_name,
415                                                   .ph_opc = LUSTRE_OPC_CREATE };
416
417                 rc = ll_fid_md_alloc(ll_i2sbi(parent), &op_data->fid2, &hint);
418                 if (rc) {
419                         CERROR("can't allocate new fid, rc %d\n", rc);
420                         LBUG();
421                 }
422         }
423
424         it->it_create_mode &= ~current->fs->umask;
425
426         rc = md_intent_lock(ll_i2mdexp(parent), op_data, NULL, 0, it,
427                             lookup_flags, &req, ll_md_blocking_ast, 0);
428         OBD_FREE_PTR(op_data);
429         
430         if (rc < 0)
431                 GOTO(out, retval = ERR_PTR(rc));
432
433         rc = lookup_it_finish(req, 1, it, &icbd);
434         if (rc != 0) {
435                 ll_intent_release(it);
436                 GOTO(out, retval = ERR_PTR(rc));
437         }
438
439         if ((it->it_op & IT_OPEN) && dentry->d_inode &&
440             !S_ISREG(dentry->d_inode->i_mode) &&
441             !S_ISDIR(dentry->d_inode->i_mode)) {
442                 ll_release_openhandle(dentry, it);
443         }
444         ll_lookup_finish_locks(it, dentry);
445
446         if (dentry == save)
447                 GOTO(out, retval = NULL);
448         else
449                 GOTO(out, retval = dentry);
450  out:
451         if (req)
452                 ptlrpc_req_finished(req);
453         return retval;
454 }
455
456 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
457 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
458                                    struct nameidata *nd)
459 {
460         struct dentry *de;
461         ENTRY;
462
463         if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
464                 de = ll_lookup_it(parent, dentry, &nd->intent, nd->flags);
465         else
466                 de = ll_lookup_it(parent, dentry, NULL, 0);
467
468         RETURN(de);
469 }
470 #endif
471
472 /* We depend on "mode" being set with the proper file type/umask by now */
473 static struct inode *ll_create_node(struct inode *dir, const char *name,
474                                     int namelen, const void *data, int datalen,
475                                     int mode, __u64 extra,
476                                     struct lookup_intent *it)
477 {
478         struct inode *inode = NULL;
479         struct ptlrpc_request *request = NULL;
480         struct ll_sb_info *sbi = ll_i2sbi(dir);
481         int rc;
482         ENTRY;
483
484         LASSERT(it && it->d.lustre.it_disposition);
485
486         request = it->d.lustre.it_data;
487         rc = ll_prep_inode(&inode, request, 1, dir->i_sb);
488         if (rc)
489                 GOTO(out, inode = ERR_PTR(rc));
490
491         LASSERT(list_empty(&inode->i_dentry));
492
493         /* We asked for a lock on the directory, but were granted a
494          * lock on the inode.  Since we finally have an inode pointer,
495          * stuff it in the lock. */
496         CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
497                inode, inode->i_ino, inode->i_generation);
498         md_set_lock_data(sbi->ll_md_exp,
499                          &it->d.lustre.it_lock_handle, inode);
500         EXIT;
501  out:
502         ptlrpc_req_finished(request);
503         return inode;
504 }
505
506 /*
507  * By the time this is called, we already have created the directory cache
508  * entry for the new file, but it is so far negative - it has no inode.
509  *
510  * We defer creating the OBD object(s) until open, to keep the intent and
511  * non-intent code paths similar, and also because we do not have the MDS
512  * inode number before calling ll_create_node() (which is needed for LOV),
513  * so we would need to do yet another RPC to the MDS to store the LOV EA
514  * data on the MDS.  If needed, we would pass the PACKED lmm as data and
515  * lmm_size in datalen (the MDS still has code which will handle that).
516  *
517  * If the create succeeds, we fill in the inode information
518  * with d_instantiate().
519  */
520 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
521                         struct lookup_intent *it)
522 {
523         struct inode *inode;
524         int rc = 0;
525         ENTRY;
526
527         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
528                dentry->d_name.len, dentry->d_name.name, dir->i_ino,
529                dir->i_generation, dir, LL_IT2STR(it));
530
531         rc = it_open_error(DISP_OPEN_CREATE, it);
532         if (rc)
533                 RETURN(rc);
534
535         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
536                                NULL, 0, mode, 0, it);
537         if (IS_ERR(inode)) {
538                 RETURN(PTR_ERR(inode));
539         }
540
541         d_instantiate(dentry, inode);
542         RETURN(0);
543 }
544
545 static void ll_update_times(struct ptlrpc_request *request, int offset,
546                             struct inode *inode)
547 {
548         struct mdt_body *body = lustre_msg_buf(request->rq_repmsg, offset,
549                                                sizeof(*body));
550         LASSERT(body);
551
552         if (body->valid & OBD_MD_FLMTIME &&
553             body->mtime > LTIME_S(inode->i_mtime)) {
554                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to "LPU64"\n",
555                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
556                 LTIME_S(inode->i_mtime) = body->mtime;
557         }
558         if (body->valid & OBD_MD_FLCTIME &&
559             body->ctime > LTIME_S(inode->i_ctime))
560                 LTIME_S(inode->i_ctime) = body->ctime;
561 }
562
563 static int ll_mknod_generic(struct inode *dir, struct qstr *name, int mode,
564                             unsigned rdev, struct dentry *dchild)
565 {
566         struct ptlrpc_request *request = NULL;
567         struct inode *inode = NULL;
568         struct ll_sb_info *sbi = ll_i2sbi(dir);
569         struct md_op_data *op_data;
570         struct lu_placement_hint hint = {
571                 .ph_pname = NULL,
572                 .ph_pfid = ll_inode2fid(dir),
573                 .ph_cname = name,
574                 .ph_opc = LUSTRE_OPC_MKNOD
575         };
576         int err;
577         ENTRY;
578
579         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p) mode %o dev %x\n",
580                name->len, name->name, dir->i_ino, dir->i_generation, dir,
581                mode, rdev);
582
583         mode &= ~current->fs->umask;
584
585         switch (mode & S_IFMT) {
586         case 0:
587         case S_IFREG:
588                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
589         case S_IFCHR:
590         case S_IFBLK:
591         case S_IFIFO:
592         case S_IFSOCK:
593                 OBD_ALLOC_PTR(op_data);
594                 if (op_data == NULL)
595                         RETURN(-ENOMEM);
596                 ll_prepare_md_op_data(op_data, dir, NULL, name->name,
597                                       name->len, 0);
598                 err = ll_fid_md_alloc(sbi, &op_data->fid2, &hint);
599                 if (err)
600                         break;
601                 err = md_create(sbi->ll_md_exp, op_data, NULL, 0, mode,
602                                 current->fsuid, current->fsgid,
603                                 current->cap_effective, rdev, &request);
604                 OBD_FREE_PTR(op_data);
605                 if (err)
606                         break;
607                 ll_update_times(request, 0, dir);
608
609                 if (dchild) {
610                         err = ll_prep_inode(&inode, request, 0,
611                                             dchild->d_sb);
612                         if (err)
613                                 break;
614
615                         d_instantiate(dchild, inode);
616                 }
617                 break;
618         case S_IFDIR:
619                 err = -EPERM;
620                 break;
621         default:
622                 err = -EINVAL;
623         }
624         ptlrpc_req_finished(request);
625         RETURN(err);
626 }
627
628 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
629 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
630 {
631         if (!nd || !nd->intent.d.lustre.it_disposition) {
632                 /* No saved request? Just mknod the file */
633                 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
634         }
635
636         return ll_create_it(dir, dentry, mode, &nd->intent);
637 }
638 #endif
639
640 static int ll_symlink_generic(struct inode *dir, struct dentry *dchild,
641                               const char *tgt)
642 {
643         struct qstr *name = &dchild->d_name;
644         struct lu_placement_hint hint = { .ph_pname = NULL,
645                                           .ph_pfid = ll_inode2fid(dir),
646                                           .ph_cname = name,
647                                           .ph_opc = LUSTRE_OPC_SYMLINK };
648
649         struct ptlrpc_request *request = NULL;
650         struct ll_sb_info *sbi = ll_i2sbi(dir);
651         struct inode *inode = NULL;
652         struct md_op_data *op_data;
653         int err;
654         ENTRY;
655
656         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),target=%s\n",
657                name->len, name->name, dir->i_ino, dir->i_generation,
658                dir, tgt);
659
660         OBD_ALLOC_PTR(op_data);
661         if (op_data == NULL)
662                 RETURN(-ENOMEM);
663         
664         ll_prepare_md_op_data(op_data, dir, NULL,
665                               name->name, name->len, 0);
666
667         /* allocate new fid */
668         err = ll_fid_md_alloc(ll_i2sbi(dir), &op_data->fid2, &hint);
669         if (err) {
670                 CERROR("can't allocate new fid, rc %d\n", err);
671                 LBUG();
672         }
673
674         err = md_create(sbi->ll_md_exp, op_data,
675                         tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
676                         current->fsuid, current->fsgid, current->cap_effective,
677                         0, &request);
678         OBD_FREE_PTR(op_data);
679         if (err == 0) {
680                 ll_update_times(request, 0, dir);
681
682                 if (dchild) {
683                         err = ll_prep_inode(&inode, request, 0,
684                                             dchild->d_sb);
685                         if (err == 0)
686                                 d_instantiate(dchild, inode);
687                 }
688         }
689
690         ptlrpc_req_finished(request);
691         RETURN(err);
692 }
693
694 static int ll_link_generic(struct inode *src,  struct inode *dir,
695                            struct qstr *name)
696 {
697         struct ll_sb_info *sbi = ll_i2sbi(dir);
698         struct ptlrpc_request *request = NULL;
699         struct md_op_data *op_data;
700         int err;
701
702         ENTRY;
703         CDEBUG(D_VFSTRACE,
704                "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%.*s\n",
705                src->i_ino, src->i_generation, src, dir->i_ino,
706                dir->i_generation, dir, name->len, name->name);
707
708         OBD_ALLOC_PTR(op_data);
709         if (op_data == NULL)
710                 RETURN(-ENOMEM);
711         ll_prepare_md_op_data(op_data, src, dir, name->name,
712                               name->len, 0);
713         err = md_link(sbi->ll_md_exp, op_data, &request);
714         OBD_FREE_PTR(op_data);
715         if (err == 0)
716                 ll_update_times(request, 0, dir);
717
718         ptlrpc_req_finished(request);
719         RETURN(err);
720 }
721
722 static int ll_mkdir_generic(struct inode *dir, struct qstr *name,
723                             int mode, struct dentry *dchild)
724
725 {
726         struct lu_placement_hint hint = { .ph_pname = NULL,
727                                           .ph_pfid = ll_inode2fid(dir),
728                                           .ph_cname = name,
729                                           .ph_opc = LUSTRE_OPC_MKDIR };
730         struct ptlrpc_request *request = NULL;
731         struct ll_sb_info *sbi = ll_i2sbi(dir);
732         struct inode *inode = NULL;
733         struct md_op_data *op_data;
734         int err;
735         ENTRY;
736         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
737                name->len, name->name, dir->i_ino, dir->i_generation, dir);
738
739         mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
740
741         OBD_ALLOC_PTR(op_data);
742         if (op_data == NULL)
743                 RETURN(-ENOMEM);
744         ll_prepare_md_op_data(op_data, dir, NULL,
745                               name->name, name->len, 0);
746
747         /* allocate new fid */
748         err = ll_fid_md_alloc(ll_i2sbi(dir), &op_data->fid2, &hint);
749         if (err) {
750                 CERROR("can't allocate new fid, rc %d\n", err);
751                 LBUG();
752         }
753
754         err = md_create(sbi->ll_md_exp, op_data, NULL, 0, mode,
755                         current->fsuid, current->fsgid,
756                         current->cap_effective, 0, &request);
757         OBD_FREE_PTR(op_data);
758         ll_update_times(request, 0, dir);
759         if (!err && dchild) {
760                 err = ll_prep_inode(&inode, request, 0,
761                                     dchild->d_sb);
762                 if (err)
763                         GOTO(out, err);
764                 d_instantiate(dchild, inode);
765         }
766         EXIT;
767 out:
768         ptlrpc_req_finished(request);
769         return err;
770 }
771
772 static int ll_rmdir_generic(struct inode *dir, struct dentry *dparent,
773                             struct qstr *name)
774 {
775         struct ptlrpc_request *request = NULL;
776         struct md_op_data *op_data;
777         struct dentry *dentry;
778         int rc;
779         ENTRY;
780         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
781                name->len, name->name, dir->i_ino, dir->i_generation, dir);
782
783         /* Check if we have something mounted at the dir we are going to delete
784          * In such a case there would always be dentry present. */
785         if (dparent) {
786                 dentry = d_lookup(dparent, name);
787                 if (dentry) {
788                         int mounted = d_mountpoint(dentry);
789                         dput(dentry);
790                         if (mounted)
791                                 RETURN(-EBUSY);
792                 }
793         }
794
795         OBD_ALLOC_PTR(op_data);
796         if (op_data == NULL)
797                 RETURN(-ENOMEM);
798
799         ll_prepare_md_op_data(op_data, dir, NULL, name->name,
800                               name->len, S_IFDIR);
801         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
802         OBD_FREE_PTR(op_data);
803         if (rc == 0)
804                 ll_update_times(request, 0, dir);
805         ptlrpc_req_finished(request);
806         RETURN(rc);
807 }
808
809 int ll_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
810 {
811         struct mdt_body *body;
812         struct lov_mds_md *eadata;
813         struct lov_stripe_md *lsm = NULL;
814         struct obd_trans_info oti = { 0 };
815         struct obdo *oa;
816         int rc;
817         ENTRY;
818
819         /* req is swabbed so this is safe */
820         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
821
822         if (!(body->valid & OBD_MD_FLEASIZE))
823                 RETURN(0);
824
825         if (body->eadatasize == 0) {
826                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
827                 GOTO(out, rc = -EPROTO);
828         }
829
830         /* The MDS sent back the EA because we unlinked the last reference
831          * to this file. Use this EA to unlink the objects on the OST.
832          * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
833          * check it is complete and sensible. */
834         eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
835         LASSERT(eadata != NULL);
836         if (eadata == NULL) {
837                 CERROR("Can't unpack MDS EA data\n");
838                 GOTO(out, rc = -EPROTO);
839         }
840
841         rc = obd_unpackmd(ll_i2dtexp(dir), &lsm, eadata, body->eadatasize);
842         if (rc < 0) {
843                 CERROR("obd_unpackmd: %d\n", rc);
844                 GOTO(out, rc);
845         }
846         LASSERT(rc >= sizeof(*lsm));
847
848         rc = obd_checkmd(ll_i2dtexp(dir), ll_i2mdexp(dir), lsm);
849         if (rc)
850                 GOTO(out_free_memmd, rc);
851
852         oa = obdo_alloc();
853         if (oa == NULL)
854                 GOTO(out_free_memmd, rc = -ENOMEM);
855
856         oa->o_id = lsm->lsm_object_id;
857         oa->o_mode = body->mode & S_IFMT;
858         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
859
860         if (body->valid & OBD_MD_FLCOOKIE) {
861                 oa->o_valid |= OBD_MD_FLCOOKIE;
862                 oti.oti_logcookies =
863                         lustre_msg_buf(request->rq_repmsg, 2,
864                                        sizeof(struct llog_cookie) *
865                                        lsm->lsm_stripe_count);
866                 if (oti.oti_logcookies == NULL) {
867                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
868                         body->valid &= ~OBD_MD_FLCOOKIE;
869                 }
870         }
871
872         rc = obd_destroy(ll_i2dtexp(dir), oa, lsm, &oti, ll_i2mdexp(dir));
873         obdo_free(oa);
874         if (rc)
875                 CERROR("obd destroy objid "LPX64" error %d\n",
876                        lsm->lsm_object_id, rc);
877  out_free_memmd:
878         obd_free_memmd(ll_i2dtexp(dir), &lsm);
879  out:
880         return rc;
881 }
882
883 static int ll_unlink_generic(struct inode * dir, struct qstr *name)
884 {
885         struct ptlrpc_request *request = NULL;
886         struct md_op_data *op_data;
887         int rc;
888         ENTRY;
889         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
890                name->len, name->name, dir->i_ino, dir->i_generation, dir);
891
892         OBD_ALLOC_PTR(op_data);
893         if (op_data == NULL)
894                 RETURN(-ENOMEM);
895        
896         ll_prepare_md_op_data(op_data, dir, NULL, name->name, name->len, 0);
897         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
898         OBD_FREE_PTR(op_data);
899         
900         if (rc)
901                 GOTO(out, rc);
902
903         ll_update_times(request, 0, dir);
904
905         rc = ll_objects_destroy(request, dir);
906  out:
907         ptlrpc_req_finished(request);
908         RETURN(rc);
909 }
910
911 static int ll_rename_generic(struct inode *src, struct qstr *src_name,
912                              struct inode *tgt, struct qstr *tgt_name)
913 {
914         struct ptlrpc_request *request = NULL;
915         struct ll_sb_info *sbi = ll_i2sbi(src);
916         struct md_op_data *op_data;
917         int err;
918         ENTRY;
919         CDEBUG(D_VFSTRACE,"VFS Op:oldname=%.*s,src_dir=%lu/%u(%p),newname=%.*s,"
920                "tgt_dir=%lu/%u(%p)\n", src_name->len, src_name->name,
921                src->i_ino, src->i_generation, src, tgt_name->len,
922                tgt_name->name, tgt->i_ino, tgt->i_generation, tgt);
923
924         OBD_ALLOC_PTR(op_data);
925         if (op_data == NULL)
926                 RETURN(-ENOMEM);
927
928         ll_prepare_md_op_data(op_data, src, tgt, NULL, 0, 0);
929         err = md_rename(sbi->ll_md_exp, op_data,
930                         src_name->name, src_name->len,
931                         tgt_name->name, tgt_name->len, &request);
932         OBD_FREE_PTR(op_data);
933         if (!err) {
934                 ll_update_times(request, 0, src);
935                 ll_update_times(request, 0, tgt);
936                 err = ll_objects_destroy(request, src);
937         }
938
939         ptlrpc_req_finished(request);
940
941         RETURN(err);
942 }
943
944 static int ll_mknod_raw(struct nameidata *nd, int mode, dev_t rdev)
945 {
946         return ll_mknod_generic(nd->dentry->d_inode, &nd->last, mode,rdev,NULL);
947 }
948 static int ll_rename_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
949 {
950         return ll_rename_generic(srcnd->dentry->d_inode, &srcnd->last,
951                                  tgtnd->dentry->d_inode, &tgtnd->last);
952 }
953 static int ll_link_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
954 {
955         return ll_link_generic(srcnd->dentry->d_inode, tgtnd->dentry->d_inode,
956                                &tgtnd->last);
957 }
958 static int ll_symlink_raw(struct nameidata *nd, const char *tgt)
959 {
960         return -EOPNOTSUPP;
961 }
962 static int ll_rmdir_raw(struct nameidata *nd)
963 {
964         return ll_rmdir_generic(nd->dentry->d_inode, nd->dentry, &nd->last);
965 }
966 static int ll_mkdir_raw(struct nameidata *nd, int mode)
967 {
968         return ll_mkdir_generic(nd->dentry->d_inode, &nd->last, mode, NULL);
969 }
970 static int ll_unlink_raw(struct nameidata *nd)
971 {
972         return ll_unlink_generic(nd->dentry->d_inode, &nd->last);
973 }
974
975 static int ll_mknod(struct inode *dir, struct dentry *dchild, int mode,
976                     ll_dev_t rdev)
977 {
978         return ll_mknod_generic(dir, &dchild->d_name, mode,
979                                 old_encode_dev(rdev), dchild);
980 }
981
982 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
983 static int ll_unlink(struct inode * dir, struct dentry *dentry)
984 {
985         return ll_unlink_generic(dir, &dentry->d_name);
986 }
987 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
988 {
989         return ll_mkdir_generic(dir, &dentry->d_name, mode, dentry);
990 }
991 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
992 {
993         return ll_rmdir_generic(dir, NULL, &dentry->d_name);
994 }
995 static int ll_symlink(struct inode *dir, struct dentry *dentry,
996                       const char *oldname)
997 {
998         return ll_symlink_generic(dir, dentry, oldname);
999 }
1000 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1001                    struct dentry *new_dentry)
1002 {
1003         return ll_link_generic(old_dentry->d_inode, dir, &new_dentry->d_name);
1004 }
1005 static int ll_rename(struct inode *old_dir, struct dentry *old_dentry,
1006                      struct inode *new_dir, struct dentry *new_dentry)
1007 {
1008         return ll_rename_generic(old_dir, &old_dentry->d_name, new_dir,
1009                                &new_dentry->d_name);
1010 }
1011 #endif
1012
1013 struct inode_operations ll_dir_inode_operations = {
1014         .link_raw           = ll_link_raw,
1015         .unlink_raw         = ll_unlink_raw,
1016         .symlink_raw        = ll_symlink_raw,
1017         .mkdir_raw          = ll_mkdir_raw,
1018         .rmdir_raw          = ll_rmdir_raw,
1019         .mknod_raw          = ll_mknod_raw,
1020         .mknod              = ll_mknod,
1021         .rename_raw         = ll_rename_raw,
1022         .setattr            = ll_setattr,
1023         .setattr_raw        = ll_setattr_raw,
1024 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1025         .create_it          = ll_create_it,
1026         .lookup_it          = ll_lookup_it,
1027         .revalidate_it      = ll_inode_revalidate_it,
1028 #else
1029         .lookup             = ll_lookup_nd,
1030         .create             = ll_create_nd,
1031         .getattr_it         = ll_getattr_it,
1032         /* We need all these non-raw things for NFSD, to not patch it. */
1033         .unlink             = ll_unlink,
1034         .mkdir              = ll_mkdir,
1035         .rmdir              = ll_rmdir,
1036         .symlink            = ll_symlink,
1037         .link               = ll_link,
1038         .rename             = ll_rename,
1039         .setattr            = ll_setattr,
1040         .getattr            = ll_getattr,
1041 #endif
1042         .permission         = ll_inode_permission,
1043         .setxattr           = ll_setxattr,
1044         .getxattr           = ll_getxattr,
1045         .listxattr          = ll_listxattr,
1046         .removexattr        = ll_removexattr,
1047 };
1048
1049 struct inode_operations ll_special_inode_operations = {
1050         .setattr_raw    = ll_setattr_raw,
1051         .setattr        = ll_setattr,
1052 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1053         .getattr_it     = ll_getattr_it,
1054 #else
1055         .revalidate_it  = ll_inode_revalidate_it,
1056 #endif
1057         .permission     = ll_inode_permission,
1058         .setxattr       = ll_setxattr,
1059         .getxattr       = ll_getxattr,
1060         .listxattr      = ll_listxattr,
1061         .removexattr    = ll_removexattr,
1062 };