Whamcloud - gitweb
- changes with names on exports and another fields in llite and mds. lov_exp is
[fs/lustre-release.git] / lustre / llite / dcache.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2001-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/smp_lock.h>
25 #include <linux/quotaops.h>
26
27 #define DEBUG_SUBSYSTEM S_LLITE
28
29 #include <linux/obd_support.h>
30 #include <linux/lustre_lite.h>
31 #include <linux/lustre_idl.h>
32 #include <linux/lustre_dlm.h>
33 #include <linux/lustre_version.h>
34
35 #include "llite_internal.h"
36
37 /* should NOT be called with the dcache lock, see fs/dcache.c */
38 static void ll_release(struct dentry *de)
39 {
40         struct ll_dentry_data *lld;
41         ENTRY;
42         LASSERT(de != NULL);
43         lld = ll_d2d(de);
44         LASSERT(lld != NULL);
45         LASSERT(lld->lld_cwd_count == 0);
46         LASSERT(lld->lld_mnt_count == 0);
47         OBD_FREE(de->d_fsdata, sizeof(struct ll_dentry_data));
48
49         EXIT;
50 }
51
52 /* should NOT be called with the dcache lock, see fs/dcache.c */
53 static int ll_ddelete(struct dentry *de)
54 {
55         ENTRY;
56         LASSERT(de);
57         CDEBUG(D_DENTRY, "%s dentry %*s (%p, parent %p, inode %p) %s%s\n",
58                (de->d_flags & DCACHE_LUSTRE_INVALID ? "deleting" : "keeping"),
59                de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
60                d_unhashed(de) ? "" : "hashed,",
61                list_empty(&de->d_subdirs) ? "" : "subdirs");
62         RETURN(0);
63 }
64
65 void ll_set_dd(struct dentry *de)
66 {
67         ENTRY;
68         LASSERT(de != NULL);
69
70         CDEBUG(D_DENTRY, "ldd on dentry %*s (%p) parent %p inode %p refc %d\n",
71                de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
72                atomic_read(&de->d_count));
73         lock_kernel();
74         if (de->d_fsdata == NULL) {
75                 OBD_ALLOC(de->d_fsdata, sizeof(struct ll_dentry_data));
76         }
77         unlock_kernel();
78
79         EXIT;
80 }
81
82 void ll_intent_drop_lock(struct lookup_intent *it)
83 {
84         struct lustre_handle *handle;
85
86         if (it->it_op && it->d.lustre.it_lock_mode) {
87                 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
88                 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
89                        " from it %p\n", handle->cookie, it);
90                 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
91
92                 /* bug 494: intent_release may be called multiple times, from
93                  * this thread and we don't want to double-decref this lock */
94                 it->d.lustre.it_lock_mode = 0;
95         }
96 }
97
98 void ll_intent_release(struct lookup_intent *it)
99 {
100         ENTRY;
101
102         ll_intent_drop_lock(it);
103         it->it_magic = 0;
104         it->it_op_release = 0;
105         it->d.lustre.it_disposition = 0;
106         it->d.lustre.it_data = NULL;
107         EXIT;
108 }
109
110 void ll_unhash_aliases(struct inode *inode)
111 {
112         struct list_head *tmp, *head;
113         struct ll_sb_info *sbi;
114         ENTRY;
115
116         if (inode == NULL) {
117                 CERROR("unexpected NULL inode, tell phil\n");
118                 return;
119         }
120
121         CDEBUG(D_INODE, "marking dentries for ino %lu/%u(%p) invalid\n",
122                inode->i_ino, inode->i_generation, inode);
123
124         sbi = ll_i2sbi(inode);
125         head = &inode->i_dentry;
126 restart:
127         spin_lock(&dcache_lock);
128         tmp = head;
129         while ((tmp = tmp->next) != head) {
130                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
131                 if (atomic_read(&dentry->d_count) == 0) {
132                         CDEBUG(D_DENTRY, "deleting dentry %*s (%p) parent %p "
133                                "inode %p\n", dentry->d_name.len,
134                                dentry->d_name.name, dentry, dentry->d_parent,
135                                dentry->d_inode);
136                         dget_locked(dentry);
137                         __d_drop(dentry);
138                         spin_unlock(&dcache_lock);
139                         dput(dentry);
140                         goto restart;
141                 } else if (!(dentry->d_flags & DCACHE_LUSTRE_INVALID)) {
142                         CDEBUG(D_DENTRY, "unhashing dentry %*s (%p) parent %p "
143                                "inode %p refc %d\n", dentry->d_name.len,
144                                dentry->d_name.name, dentry, dentry->d_parent,
145                                dentry->d_inode, atomic_read(&dentry->d_count));
146                         hlist_del_init(&dentry->d_hash);
147                         dentry->d_flags |= DCACHE_LUSTRE_INVALID;
148                         hlist_add_head(&dentry->d_hash,
149                                        &sbi->ll_orphan_dentry_list);
150                 }
151         }
152         spin_unlock(&dcache_lock);
153         EXIT;
154 }
155
156 extern struct dentry *ll_find_alias(struct inode *, struct dentry *);
157
158 int revalidate_it_finish(struct ptlrpc_request *request, int offset, 
159                          struct lookup_intent *it, struct dentry *de)
160 {
161         struct ll_sb_info *sbi;
162         int rc = 0;
163         ENTRY;
164
165         if (!request)
166                 RETURN(0);
167
168         if (it_disposition(it, DISP_LOOKUP_NEG))
169                 RETURN(-ENOENT);
170
171         sbi = ll_i2sbi(de->d_inode);
172         rc = ll_prep_inode(sbi->ll_dt_exp, sbi->ll_md_exp,
173                            &de->d_inode, request, offset, NULL);
174
175         RETURN(rc);
176 }
177
178 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
179 {
180         LASSERT(it != NULL);
181         LASSERT(dentry != NULL);
182
183         if (it->d.lustre.it_lock_mode && dentry->d_inode != NULL) {
184                 struct inode *inode = dentry->d_inode;
185                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
186                        inode, inode->i_ino, inode->i_generation);
187                 mdc_set_lock_data(NULL, &it->d.lustre.it_lock_handle, inode);
188         }
189
190         /* drop lookup or getattr locks immediately */
191         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR ||
192             it->it_op == IT_CHDIR) {
193 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
194                 /*
195                  * on 2.6 there are situations when several lookups and
196                  * revalidations may be requested during single operation.
197                  * Therefore, we don't release intent here -bzzz
198                  */
199                 ll_intent_drop_lock(it);
200 #else
201                 ll_intent_release(it);
202 #endif
203         }
204 }
205
206 void ll_frob_intent(struct lookup_intent **itp, struct lookup_intent *deft)
207 {
208         struct lookup_intent *it = *itp;
209         
210 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
211         if (it) {
212                 LASSERTF(it->it_magic == INTENT_MAGIC, "bad intent magic: %x\n",
213                          it->it_magic);
214         }
215 #endif
216
217         if (!it || it->it_op == IT_GETXATTR)
218                 it = *itp = deft;
219
220         it->it_op_release = ll_intent_release;
221 }
222
223 int ll_revalidate_it(struct dentry *de, int flags, struct nameidata *nd,
224                      struct lookup_intent *it)
225 {
226         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
227         struct ptlrpc_request *req = NULL;
228         struct it_cb_data icbd;
229         struct obd_export *exp;
230         struct lustre_id pid;
231         struct lustre_id cid;
232         int rc;
233         ENTRY;
234
235         CDEBUG(D_VFSTRACE, "VFS Op:name=%s, intent=%s\n", de->d_name.name,
236                LL_IT2STR(it));
237
238         /* Cached negative dentries are unsafe for now - look them up again */
239         if (de->d_inode == NULL)
240                 RETURN(0);
241
242         CDEBUG(D_INODE, "revalidate 0x%p: %*s -> %lu/%lu\n",
243                de, de->d_name.len, de->d_name.name,
244                (unsigned long) de->d_inode->i_ino,
245                (unsigned long) de->d_inode->i_generation);
246
247         exp = ll_i2mdexp(de->d_inode);
248         ll_inode2id(&pid, de->d_parent->d_inode);
249         ll_inode2id(&cid, de->d_inode);
250         LASSERT(id_fid(&cid) != 0);
251
252         icbd.icbd_parent = de->d_parent->d_inode;
253         icbd.icbd_childp = &de;
254
255         /*
256          * never execute intents for mount points. Attributes will be fixed up
257          * in ll_inode_revalidate_it().
258          */
259         if (d_mountpoint(de))
260                 RETURN(1);
261
262         if (nd != NULL)
263                 nd->mnt->mnt_last_used = jiffies;
264
265         ll_frob_intent(&it, &lookup_it);
266         LASSERT(it != NULL);
267
268         if (it->it_op == IT_GETATTR) { /* We need to check for LOOKUP lock as
269                                           well */
270                 rc = md_intent_lock(exp, &pid, de->d_name.name,
271                                     de->d_name.len, NULL, 0, &cid, &lookup_it,
272                                     flags, &req, ll_mdc_blocking_ast);
273                 /* If there was no lookup lock, no point in even checking for
274                    UPDATE lock */
275                 if (!rc) {
276                         it = &lookup_it;
277                         GOTO(out, rc);
278                 }
279                 if (it_disposition(&lookup_it, DISP_LOOKUP_NEG)) {
280                         ll_intent_release(&lookup_it);
281                         it = &lookup_it;
282                         GOTO(out, rc = 0);
283                 }
284
285                 if (req)
286                         ptlrpc_req_finished(req);
287                 req = NULL;
288                 ll_lookup_finish_locks(&lookup_it, de);
289         }
290
291         rc = md_intent_lock(exp, &pid, de->d_name.name, de->d_name.len,
292                             NULL, 0, &cid, it, flags, &req,
293                             ll_mdc_blocking_ast);
294         
295         /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
296          * if all was well, it will return 1 if it found locks, 0 otherwise. */
297         if (req == NULL && rc >= 0)
298                 GOTO(out, rc);
299
300         if (rc < 0) {
301                 if (rc != -ESTALE) {
302                         CDEBUG(D_INFO, "ll_intent_lock(): rc %d : it->it_status "
303                                "%d\n", rc, it->d.lustre.it_status);
304                 }
305                 GOTO(out, rc = 0);
306         }
307
308         rc = revalidate_it_finish(req, 1, it, de);
309         if (rc != 0) {
310                 ll_intent_release(it);
311                 GOTO(out, rc = 0);
312         }
313         rc = 1;
314
315         /* unfortunately ll_intent_lock may cause a callback and revoke our
316            dentry */
317         spin_lock(&dcache_lock);
318         hlist_del_init(&de->d_hash);
319         __d_rehash(de, 0);
320         spin_unlock(&dcache_lock);
321
322         GOTO(out, rc);
323 out:
324         if (req != NULL && rc == 1)
325                 ptlrpc_req_finished(req);
326         if (rc == 0) {
327                 ll_unhash_aliases(de->d_inode);
328                 return rc;
329         }
330
331         CDEBUG(D_DENTRY, "revalidated dentry %*s (%p) parent %p "
332                "inode %p refc %d\n", de->d_name.len,
333                de->d_name.name, de, de->d_parent, de->d_inode,
334                atomic_read(&de->d_count));
335         ll_lookup_finish_locks(it, de);
336         de->d_flags &= ~DCACHE_LUSTRE_INVALID;
337         if (!((de->d_inode->i_mode & S_ISUID) &&S_ISDIR(de->d_inode->i_mode)) ||
338             !(flags & LOOKUP_CONTINUE || (it->it_op & (IT_CHDIR | IT_OPEN))))
339                 return rc;
340
341         if (nd)
342                 (void)ll_dir_process_mount_object(de, nd->mnt);
343         return rc;
344 }
345
346 /*static*/ void ll_pin(struct dentry *de, struct vfsmount *mnt, int flag)
347 {
348         struct inode *inode= de->d_inode;
349         struct ll_sb_info *sbi = ll_i2sbi(inode);
350         struct ll_dentry_data *ldd = ll_d2d(de);
351         struct obd_client_handle *handle;
352         int rc = 0;
353         ENTRY;
354         LASSERT(ldd);
355
356         lock_kernel();
357         /* Strictly speaking this introduces an additional race: the
358          * increments should wait until the rpc has returned.
359          * However, given that at present the function is void, this
360          * issue is moot. */
361         if (flag == 1 && (++ldd->lld_mnt_count) > 1) {
362                 unlock_kernel();
363                 EXIT;
364                 return;
365         }
366
367         if (flag == 0 && (++ldd->lld_cwd_count) > 1) {
368                 unlock_kernel();
369                 EXIT;
370                 return;
371         }
372         unlock_kernel();
373
374         handle = (flag) ? &ldd->lld_mnt_och : &ldd->lld_cwd_och;
375         rc = obd_pin(sbi->ll_md_exp, inode->i_ino, inode->i_generation,
376                      inode->i_mode & S_IFMT, handle, flag);
377
378         if (rc) {
379                 lock_kernel();
380                 memset(handle, 0, sizeof(*handle));
381                 if (flag == 0)
382                         ldd->lld_cwd_count--;
383                 else
384                         ldd->lld_mnt_count--;
385                 unlock_kernel();
386         }
387
388         EXIT;
389         return;
390 }
391
392 /*static*/ void ll_unpin(struct dentry *de, struct vfsmount *mnt, int flag)
393 {
394         struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
395         struct ll_dentry_data *ldd = ll_d2d(de);
396         struct obd_client_handle handle;
397         int count, rc = 0;
398         ENTRY;
399         LASSERT(ldd);
400
401         lock_kernel();
402         /* Strictly speaking this introduces an additional race: the
403          * increments should wait until the rpc has returned.
404          * However, given that at present the function is void, this
405          * issue is moot. */
406         handle = (flag) ? ldd->lld_mnt_och : ldd->lld_cwd_och;
407         if (handle.och_magic != OBD_CLIENT_HANDLE_MAGIC) {
408                 /* the "pin" failed */
409                 unlock_kernel();
410                 EXIT;
411                 return;
412         }
413
414         if (flag)
415                 count = --ldd->lld_mnt_count;
416         else
417                 count = --ldd->lld_cwd_count;
418         unlock_kernel();
419
420         if (count != 0) {
421                 EXIT;
422                 return;
423         }
424
425         rc = obd_unpin(sbi->ll_md_exp, &handle, flag);
426         EXIT;
427 }
428
429 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
430 static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
431 {
432         int rc;
433         ENTRY;
434
435         if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
436                 rc = ll_revalidate_it(dentry, nd->flags, nd, &nd->intent);
437         else
438                 rc = ll_revalidate_it(dentry, 0, nd, NULL);
439
440         RETURN(rc);
441 }
442 #endif
443
444 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
445 static void ll_dentry_iput(struct dentry *dentry, struct inode *inode)
446 {
447         struct ll_sb_info *sbi = ll_i2sbi(inode);
448         struct lustre_id parent, child;
449
450         LASSERT(dentry->d_parent && dentry->d_parent->d_inode);
451         ll_inode2id(&parent, dentry->d_parent->d_inode);
452         ll_inode2id(&child, inode);
453         md_change_cbdata_name(sbi->ll_md_exp, &parent,
454                               (char *)dentry->d_name.name, 
455                               dentry->d_name.len, &child, 
456                               null_if_equal, inode);
457         iput(inode);
458 }
459 #else
460 static void ll_dentry_iput(struct dentry *dentry, struct inode *inode)
461 {
462         struct ll_sb_info *sbi = ll_i2sbi(inode);
463         struct lustre_id parent, child;
464
465         LASSERT(dentry->d_parent && dentry->d_parent->d_inode);
466         ll_inode2id(&parent, dentry->d_parent->d_inode);
467         ll_inode2id(&child, inode);
468         md_change_cbdata_name(sbi->ll_md_exp, &parent,
469                               (char *)dentry->d_name.name, 
470                               dentry->d_name.len, &child, 
471                               null_if_equal, inode);
472         iput(inode);
473 }
474 #endif
475
476 struct dentry_operations ll_d_ops = {
477 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
478         .d_revalidate = ll_revalidate_nd,
479 #else
480         .d_revalidate_it = ll_revalidate_it,
481 #endif
482         .d_release = ll_release,
483         .d_iput = ll_dentry_iput,
484         .d_delete = ll_ddelete,
485 #if 0
486         .d_pin = ll_pin,
487         .d_unpin = ll_unpin,
488 #endif
489 };