Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[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 void ll_set_dd(struct dentry *de)
53 {
54         ENTRY;
55         LASSERT(de != NULL);
56
57         lock_kernel();
58         if (de->d_fsdata == NULL) {
59                 OBD_ALLOC(de->d_fsdata, sizeof(struct ll_dentry_data));
60         }
61         unlock_kernel();
62
63         EXIT;
64 }
65
66 void ll_intent_drop_lock(struct lookup_intent *it)
67 {
68         struct lustre_handle *handle;
69
70         if (it->it_op && it->d.lustre.it_lock_mode) {
71                 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
72                 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
73                        " from it %p\n", handle->cookie, it);
74                 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
75
76                 /* bug 494: intent_release may be called multiple times, from
77                  * this thread and we don't want to double-decref this lock */
78                 it->d.lustre.it_lock_mode = 0;
79         }
80 }
81
82 void ll_intent_release(struct lookup_intent *it)
83 {
84         ENTRY;
85
86         ll_intent_drop_lock(it);
87         it->it_magic = 0;
88         it->it_op_release = 0;
89         it->d.lustre.it_disposition = 0;
90         it->d.lustre.it_data = NULL;
91         EXIT;
92 }
93
94 void ll_unhash_aliases(struct inode *inode)
95 {
96         struct list_head *tmp, *head;
97         struct ll_sb_info *sbi;
98         ENTRY;
99
100         sbi = ll_i2sbi(inode);
101
102         CDEBUG(D_INODE, "marking dentries for ino %lu/%u(%p) invalid\n",
103                inode->i_ino, inode->i_generation, inode);
104
105         if (inode == NULL) {
106                 CERROR("unexpected NULL inode, tell phil\n");
107                 return;
108         }
109         head = &inode->i_dentry;
110 restart:
111         spin_lock(&dcache_lock);
112         tmp = head;
113         while ((tmp = tmp->next) != head) {
114                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
115                 CDEBUG(D_INODE, "invalidate 0x%p: %*s -> %lu/%lu\n",
116                        dentry, dentry->d_name.len, dentry->d_name.name,
117                        (unsigned long) dentry->d_inode->i_ino,
118                        (unsigned long) dentry->d_inode->i_generation);
119                 if (!atomic_read(&dentry->d_count)) {
120                         dget_locked(dentry);
121                         __d_drop(dentry);
122                         spin_unlock(&dcache_lock);
123                         dput(dentry);
124                         goto restart;
125                 } else {
126                         hlist_del_init(&dentry->d_hash);
127                         dentry->d_flags |= DCACHE_LUSTRE_INVALID;
128                         hlist_add_head(&dentry->d_hash,
129                                        &sbi->ll_orphan_dentry_list);
130                 }
131         }
132         spin_unlock(&dcache_lock);
133         EXIT;
134 }
135
136 extern struct dentry *ll_find_alias(struct inode *, struct dentry *);
137
138 static int revalidate_it_finish(struct ptlrpc_request *request, int offset,
139                                 struct lookup_intent *it,
140                                 struct dentry *de)
141 {
142         struct ll_sb_info *sbi;
143         int rc = 0;
144         ENTRY;
145
146         if (!request)
147                 RETURN(0);
148
149         if (it_disposition(it, DISP_LOOKUP_NEG))
150                 RETURN(-ENOENT);
151
152         sbi = ll_i2sbi(de->d_inode);
153         rc = ll_prep_inode(sbi->ll_osc_exp, sbi->ll_mdc_exp,
154                            &de->d_inode, request, offset,NULL);
155
156         RETURN(rc);
157 }
158
159 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
160 {
161         LASSERT(it != NULL);
162         LASSERT(dentry != NULL);
163
164         if (it->d.lustre.it_lock_mode && dentry->d_inode != NULL) {
165                 struct inode *inode = dentry->d_inode;
166                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
167                        inode, inode->i_ino, inode->i_generation);
168                 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
169         }
170
171         /* drop lookup or getattr locks immediately */
172         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
173 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
174                 /* on 2.6 there are situation when several lookups and
175                  * revalidations may be requested during single operation.
176                  * therefore, we don't release intent here -bzzz */
177                 ll_intent_drop_lock(it);
178 #else
179                 ll_intent_release(it);
180 #endif
181         }
182 }
183
184 void ll_frob_intent(struct lookup_intent **itp, struct lookup_intent *deft)
185 {
186         struct lookup_intent *it = *itp;
187 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
188         if (it && it->it_magic != INTENT_MAGIC) {
189                 CERROR("WARNING: uninitialized intent\n");
190                 LBUG();
191         }
192         if (it && (it->it_op == IT_GETATTR || it->it_op == 0))
193                 it->it_op = IT_LOOKUP;
194 #endif
195
196         if (!it || it->it_op == IT_GETXATTR)
197                 it = *itp = deft;
198
199         it->it_op_release = ll_intent_release;
200 }
201
202 int ll_revalidate_it(struct dentry *de, int flags, struct lookup_intent *it)
203 {
204         int rc;
205         struct ll_fid pfid, cfid;
206         struct it_cb_data icbd;
207         struct ll_uctxt ctxt;
208         struct ptlrpc_request *req = NULL;
209         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
210         struct obd_export *exp;
211
212         ENTRY;
213         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%s\n", de->d_name.name,
214                LL_IT2STR(it));
215
216         /* Cached negative dentries are unsafe for now - look them up again */
217         if (de->d_inode == NULL)
218                 RETURN(0);
219
220         CDEBUG(D_INODE, "revalidate 0x%p: %*s -> %lu/%lu\n",
221                         de, de->d_name.len, de->d_name.name,
222                         (unsigned long) de->d_inode->i_ino,
223                         (unsigned long) de->d_inode->i_generation);
224
225         exp = ll_i2mdcexp(de->d_inode);
226         ll_inode2fid(&pfid, de->d_parent->d_inode);
227         ll_inode2fid(&cfid, de->d_inode);
228         icbd.icbd_parent = de->d_parent->d_inode;
229         icbd.icbd_childp = &de;
230
231         /* Never execute intents for mount points.
232          * Attributes will be fixed up in ll_inode_revalidate_it */
233         if (d_mountpoint(de))
234                 RETURN(1);
235
236         ll_frob_intent(&it, &lookup_it);
237         LASSERT(it);
238
239         ll_i2uctxt(&ctxt, de->d_parent->d_inode, de->d_inode);
240
241         if (it->it_op == IT_GETATTR) { /* We need to check for LOOKUP lock
242                                           as well */
243                 rc = md_intent_lock(exp, &ctxt, &pfid, de->d_name.name,
244                                      de->d_name.len, NULL, 0, &cfid, &lookup_it,
245                                      flags, &req, ll_mdc_blocking_ast);
246                 /* If there was no lookup lock, no point in even checking for
247                    UPDATE lock */
248                 if (!rc) {
249                         it = &lookup_it;
250                         GOTO(out, rc);
251                 }
252                 if (it_disposition(&lookup_it, DISP_LOOKUP_NEG)) {
253                         ll_intent_release(&lookup_it);
254                         it = &lookup_it;
255                         GOTO(out, rc = 0);
256                 }
257                         
258                 if (req)
259                         ptlrpc_req_finished(req);
260                 req = NULL;
261                 ll_lookup_finish_locks(&lookup_it, de);
262         }
263
264         rc = md_intent_lock(exp, &ctxt, &pfid, de->d_name.name, de->d_name.len,
265                             NULL, 0, &cfid, it, flags, &req, ll_mdc_blocking_ast);
266         /* If req is NULL, then md_intent_lock only tried to do a lock match;
267          * if all was well, it will return 1 if it found locks, 0 otherwise. */
268         if (req == NULL && rc >= 0)
269                 GOTO(out, rc);
270
271         if (rc < 0) {
272                 if (rc != -ESTALE) {
273                         CDEBUG(D_INFO, "ll_intent_lock: rc %d : it->it_status "
274                                "%d\n", rc, it->d.lustre.it_status);
275                 }
276                 GOTO(out, rc = 0);
277         }
278
279         rc = revalidate_it_finish(req, 1, it, de);
280         if (rc != 0) {
281                 ll_intent_release(it);
282                 GOTO(out, rc = 0);
283         }
284         rc = 1;
285
286         /* unfortunately ll_intent_lock may cause a callback and revoke our
287            dentry */
288         spin_lock(&dcache_lock);
289         hlist_del_init(&de->d_hash);
290         __d_rehash(de, 0);
291         spin_unlock(&dcache_lock);
292
293  out:
294         if (req != NULL && rc == 1)
295                 ptlrpc_req_finished(req);
296         if (rc == 0) {
297                 ll_unhash_aliases(de->d_inode);
298                 de->d_flags |= DCACHE_LUSTRE_INVALID;
299         } else {
300                 ll_lookup_finish_locks(it, de);
301                 de->d_flags &= ~DCACHE_LUSTRE_INVALID;
302         }
303         RETURN(rc);
304 }
305
306 /*static*/ void ll_pin(struct dentry *de, struct vfsmount *mnt, int flag)
307 {
308         struct inode *inode= de->d_inode;
309         struct ll_sb_info *sbi = ll_i2sbi(inode);
310         struct ll_dentry_data *ldd = ll_d2d(de);
311         struct obd_client_handle *handle;
312         int rc = 0;
313         ENTRY;
314         LASSERT(ldd);
315
316         lock_kernel();
317         /* Strictly speaking this introduces an additional race: the
318          * increments should wait until the rpc has returned.
319          * However, given that at present the function is void, this
320          * issue is moot. */
321         if (flag == 1 && (++ldd->lld_mnt_count) > 1) {
322                 unlock_kernel();
323                 EXIT;
324                 return;
325         }
326
327         if (flag == 0 && (++ldd->lld_cwd_count) > 1) {
328                 unlock_kernel();
329                 EXIT;
330                 return;
331         }
332         unlock_kernel();
333
334         handle = (flag) ? &ldd->lld_mnt_och : &ldd->lld_cwd_och;
335         rc = obd_pin(sbi->ll_mdc_exp, inode->i_ino, inode->i_generation,
336                      inode->i_mode & S_IFMT, handle, flag);
337
338         if (rc) {
339                 lock_kernel();
340                 memset(handle, 0, sizeof(*handle));
341                 if (flag == 0)
342                         ldd->lld_cwd_count--;
343                 else
344                         ldd->lld_mnt_count--;
345                 unlock_kernel();
346         }
347
348         EXIT;
349         return;
350 }
351
352 /*static*/ void ll_unpin(struct dentry *de, struct vfsmount *mnt, int flag)
353 {
354         struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
355         struct ll_dentry_data *ldd = ll_d2d(de);
356         struct obd_client_handle handle;
357         int count, rc = 0;
358         ENTRY;
359         LASSERT(ldd);
360
361         lock_kernel();
362         /* Strictly speaking this introduces an additional race: the
363          * increments should wait until the rpc has returned.
364          * However, given that at present the function is void, this
365          * issue is moot. */
366         handle = (flag) ? ldd->lld_mnt_och : ldd->lld_cwd_och;
367         if (handle.och_magic != OBD_CLIENT_HANDLE_MAGIC) {
368                 /* the "pin" failed */
369                 unlock_kernel();
370                 EXIT;
371                 return;
372         }
373
374         if (flag)
375                 count = --ldd->lld_mnt_count;
376         else
377                 count = --ldd->lld_cwd_count;
378         unlock_kernel();
379
380         if (count != 0) {
381                 EXIT;
382                 return;
383         }
384
385         rc = obd_unpin(sbi->ll_mdc_exp, &handle, flag);
386         EXIT;
387         return;
388 }
389
390 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
391 static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
392 {
393         int rc;
394         ENTRY;
395
396         if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
397                 rc = ll_revalidate_it(dentry, nd->flags, &nd->intent);
398         else
399                 rc = ll_revalidate_it(dentry, 0, NULL);
400
401         RETURN(rc);
402 }
403 #endif
404
405 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
406 static void ll_dentry_iput(struct dentry *dentry, struct inode *inode)
407 {
408         struct ll_sb_info *sbi = ll_i2sbi(inode);
409         struct ll_fid parent, child;
410
411         LASSERT(dentry->d_parent && dentry->d_parent->d_inode);
412         ll_inode2fid(&parent, dentry->d_parent->d_inode);
413         ll_inode2fid(&child, inode);
414         md_change_cbdata_name(sbi->ll_mdc_exp, &parent,
415                               (char *)dentry->d_name.name, dentry->d_name.len,
416                               &child, null_if_equal, inode);
417         iput(inode);
418 }
419 #else
420 #error "implement ->d_iput() for 2.6"
421 #endif
422
423 struct dentry_operations ll_d_ops = {
424 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
425         .d_revalidate = ll_revalidate_nd,
426 #else
427         .d_revalidate_it = ll_revalidate_it,
428 #endif
429         .d_release = ll_release,
430         .d_iput = ll_dentry_iput,
431 #if 0
432         .d_pin = ll_pin,
433         .d_unpin = ll_unpin,
434 #endif
435 };