Whamcloud - gitweb
Land b_smallfix onto HEAD (20040512_1806)
[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 ? "keeping" : "deleting"),
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 static int revalidate_it_finish(struct ptlrpc_request *request, int offset,
159                                 struct lookup_intent *it,
160                                 struct dentry *de)
161 {
162         struct ll_sb_info *sbi;
163         int rc = 0;
164         ENTRY;
165
166         if (!request)
167                 RETURN(0);
168
169         if (it_disposition(it, DISP_LOOKUP_NEG))
170                 RETURN(-ENOENT);
171
172         sbi = ll_i2sbi(de->d_inode);
173         rc = ll_prep_inode(sbi->ll_osc_exp, &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(&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 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
193                 /* on 2.6 there are situation when several lookups and
194                  * revalidations may be requested during single operation.
195                  * therefore, we don't release intent here -bzzz */
196                 ll_intent_drop_lock(it);
197 #else
198                 ll_intent_release(it);
199 #endif
200         }
201 }
202
203 void ll_frob_intent(struct lookup_intent **itp, struct lookup_intent *deft)
204 {
205         struct lookup_intent *it = *itp;
206 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
207         if (it && it->it_magic != INTENT_MAGIC) {
208                 CERROR("WARNING: uninitialized intent\n");
209                 LBUG();
210         }
211         if (it && (it->it_op == IT_GETATTR || it->it_op == 0))
212                 it->it_op = IT_LOOKUP;
213 #endif
214
215         if (!it || it->it_op == IT_GETXATTR)
216                 it = *itp = deft;
217
218         it->it_op_release = ll_intent_release;
219 }
220
221 int ll_revalidate_it(struct dentry *de, int flags, struct lookup_intent *it)
222 {
223         int rc;
224         struct ll_fid pfid, cfid;
225         struct it_cb_data icbd;
226         struct ll_uctxt ctxt;
227         struct ptlrpc_request *req = NULL;
228         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
229         struct obd_export *exp;
230
231         ENTRY;
232         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%s\n", de->d_name.name,
233                LL_IT2STR(it));
234
235         /* Cached negative dentries are unsafe for now - look them up again */
236         if (de->d_inode == NULL)
237                 RETURN(0);
238
239         exp = ll_i2mdcexp(de->d_inode);
240         ll_inode2fid(&pfid, de->d_parent->d_inode);
241         ll_inode2fid(&cfid, de->d_inode);
242         icbd.icbd_parent = de->d_parent->d_inode;
243         icbd.icbd_childp = &de;
244
245         /* Never execute intents for mount points.
246          * Attributes will be fixed up in ll_inode_revalidate_it */
247         if (d_mountpoint(de))
248                 RETURN(1);
249
250         ll_frob_intent(&it, &lookup_it);
251         LASSERT(it);
252
253         ll_i2uctxt(&ctxt, de->d_parent->d_inode, de->d_inode);
254
255         if (it->it_op == IT_GETATTR) { /* We need to check for LOOKUP lock
256                                           as well */
257                 rc = mdc_intent_lock(exp, &ctxt, &pfid, de->d_name.name,
258                                      de->d_name.len, NULL, 0, &cfid, &lookup_it,
259                                      flags, &req, ll_mdc_blocking_ast);
260                 /* If there was no lookup lock, no point in even checking for
261                    UPDATE lock */
262                 if (!rc) {
263                         it = &lookup_it;
264                         GOTO(out, rc);
265                 }
266                 if (it_disposition(&lookup_it, DISP_LOOKUP_NEG)) {
267                         ll_intent_release(&lookup_it);
268                         it = &lookup_it;
269                         GOTO(out, rc = 0);
270                 }
271
272                 if (req)
273                         ptlrpc_req_finished(req);
274                 req = NULL;
275                 ll_lookup_finish_locks(&lookup_it, de);
276         }
277
278         rc = mdc_intent_lock(exp, &ctxt, &pfid, de->d_name.name, de->d_name.len,
279                              NULL, 0,
280                              &cfid, it, flags, &req, ll_mdc_blocking_ast);
281         /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
282          * if all was well, it will return 1 if it found locks, 0 otherwise. */
283         if (req == NULL && rc >= 0)
284                 GOTO(out, rc);
285
286         if (rc < 0) {
287                 if (rc != -ESTALE) {
288                         CDEBUG(D_INFO, "ll_intent_lock: rc %d : it->it_status "
289                                "%d\n", rc, it->d.lustre.it_status);
290                 }
291                 GOTO(out, rc = 0);
292         }
293
294         rc = revalidate_it_finish(req, 1, it, de);
295         if (rc != 0) {
296                 ll_intent_release(it);
297                 GOTO(out, rc = 0);
298         }
299         rc = 1;
300
301         /* unfortunately ll_intent_lock may cause a callback and revoke our
302            dentry */
303         spin_lock(&dcache_lock);
304         hlist_del_init(&de->d_hash);
305         __d_rehash(de, 0);
306         spin_unlock(&dcache_lock);
307
308  out:
309         if (req != NULL && rc == 1)
310                 ptlrpc_req_finished(req);
311         if (rc == 0) {
312                 ll_unhash_aliases(de->d_inode);
313                 /* done in ll_unhash_aliases()
314                 dentry->d_flags |= DCACHE_LUSTRE_INVALID; */
315         } else {
316                 CDEBUG(D_DENTRY, "revalidated dentry %*s (%p) parent %p "
317                                "inode %p refc %d\n", de->d_name.len,
318                                de->d_name.name, de, de->d_parent, de->d_inode,
319                                atomic_read(&de->d_count));
320                 ll_lookup_finish_locks(it, de);
321                 de->d_flags &= ~DCACHE_LUSTRE_INVALID;
322         }
323         RETURN(rc);
324 }
325
326 /*static*/ void ll_pin(struct dentry *de, struct vfsmount *mnt, int flag)
327 {
328         struct inode *inode= de->d_inode;
329         struct ll_sb_info *sbi = ll_i2sbi(inode);
330         struct ll_dentry_data *ldd = ll_d2d(de);
331         struct obd_client_handle *handle;
332         int rc = 0;
333         ENTRY;
334         LASSERT(ldd);
335
336         lock_kernel();
337         /* Strictly speaking this introduces an additional race: the
338          * increments should wait until the rpc has returned.
339          * However, given that at present the function is void, this
340          * issue is moot. */
341         if (flag == 1 && (++ldd->lld_mnt_count) > 1) {
342                 unlock_kernel();
343                 EXIT;
344                 return;
345         }
346
347         if (flag == 0 && (++ldd->lld_cwd_count) > 1) {
348                 unlock_kernel();
349                 EXIT;
350                 return;
351         }
352         unlock_kernel();
353
354         handle = (flag) ? &ldd->lld_mnt_och : &ldd->lld_cwd_och;
355         rc = obd_pin(sbi->ll_mdc_exp, inode->i_ino, inode->i_generation,
356                      inode->i_mode & S_IFMT, handle, flag);
357
358         if (rc) {
359                 lock_kernel();
360                 memset(handle, 0, sizeof(*handle));
361                 if (flag == 0)
362                         ldd->lld_cwd_count--;
363                 else
364                         ldd->lld_mnt_count--;
365                 unlock_kernel();
366         }
367
368         EXIT;
369         return;
370 }
371
372 /*static*/ void ll_unpin(struct dentry *de, struct vfsmount *mnt, int flag)
373 {
374         struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
375         struct ll_dentry_data *ldd = ll_d2d(de);
376         struct obd_client_handle handle;
377         int count, rc = 0;
378         ENTRY;
379         LASSERT(ldd);
380
381         lock_kernel();
382         /* Strictly speaking this introduces an additional race: the
383          * increments should wait until the rpc has returned.
384          * However, given that at present the function is void, this
385          * issue is moot. */
386         handle = (flag) ? ldd->lld_mnt_och : ldd->lld_cwd_och;
387         if (handle.och_magic != OBD_CLIENT_HANDLE_MAGIC) {
388                 /* the "pin" failed */
389                 unlock_kernel();
390                 EXIT;
391                 return;
392         }
393
394         if (flag)
395                 count = --ldd->lld_mnt_count;
396         else
397                 count = --ldd->lld_cwd_count;
398         unlock_kernel();
399
400         if (count != 0) {
401                 EXIT;
402                 return;
403         }
404
405         rc = obd_unpin(sbi->ll_mdc_exp, &handle, flag);
406         EXIT;
407         return;
408 }
409
410 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
411 static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
412 {
413         int rc;
414         ENTRY;
415
416         if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
417                 rc = ll_revalidate_it(dentry, nd->flags, &nd->intent);
418         else
419                 rc = ll_revalidate_it(dentry, 0, NULL);
420
421         RETURN(rc);
422 }
423 #endif
424
425 struct dentry_operations ll_d_ops = {
426 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
427         .d_revalidate = ll_revalidate_nd,
428 #else
429         .d_revalidate_it = ll_revalidate_it,
430 #endif
431         .d_release = ll_release,
432         .d_delete = ll_ddelete,
433 #if 0
434         .d_pin = ll_pin,
435         .d_unpin = ll_unpin,
436 #endif
437 };