1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include <linux/fs.h>
38#include <linux/sched.h>
39#include <linux/mm.h>
40#include <linux/highmem.h>
41#include <linux/pagemap.h>
42
43#define DEBUG_SUBSYSTEM S_LLITE
44
45#include "../include/obd_support.h"
46#include "../include/lustre_lite.h"
47#include "../include/lustre_dlm.h"
48#include "llite_internal.h"
49
50#define SA_OMITTED_ENTRY_MAX 8ULL
51
52typedef enum {
53
54 SA_ENTRY_INIT = 0,
55 SA_ENTRY_SUCC = 1,
56 SA_ENTRY_INVA = 2,
57 SA_ENTRY_DEST = 3,
58} se_stat_t;
59
60struct ll_sa_entry {
61
62 struct list_head se_link;
63
64 struct list_head se_list;
65
66 struct list_head se_hash;
67
68 atomic_t se_refcount;
69
70 __u64 se_index;
71
72 __u64 se_handle;
73
74 se_stat_t se_stat;
75
76 int se_size;
77
78 struct md_enqueue_info *se_minfo;
79
80 struct ptlrpc_request *se_req;
81
82 struct inode *se_inode;
83
84 struct qstr se_qstr;
85};
86
87static unsigned int sai_generation = 0;
88static DEFINE_SPINLOCK(sai_generation_lock);
89
90static inline int ll_sa_entry_unhashed(struct ll_sa_entry *entry)
91{
92 return list_empty(&entry->se_hash);
93}
94
95
96
97
98static inline int ll_sa_entry_stated(struct ll_sa_entry *entry)
99{
100 smp_rmb();
101 return (entry->se_stat != SA_ENTRY_INIT);
102}
103
104static inline int ll_sa_entry_hash(int val)
105{
106 return val & LL_SA_CACHE_MASK;
107}
108
109
110
111
112static inline void
113ll_sa_entry_enhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
114{
115 int i = ll_sa_entry_hash(entry->se_qstr.hash);
116
117 spin_lock(&sai->sai_cache_lock[i]);
118 list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
119 spin_unlock(&sai->sai_cache_lock[i]);
120}
121
122
123
124
125static inline void
126ll_sa_entry_unhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
127{
128 int i = ll_sa_entry_hash(entry->se_qstr.hash);
129
130 spin_lock(&sai->sai_cache_lock[i]);
131 list_del_init(&entry->se_hash);
132 spin_unlock(&sai->sai_cache_lock[i]);
133}
134
135static inline int agl_should_run(struct ll_statahead_info *sai,
136 struct inode *inode)
137{
138 return (inode != NULL && S_ISREG(inode->i_mode) && sai->sai_agl_valid);
139}
140
141static inline struct ll_sa_entry *
142sa_first_received_entry(struct ll_statahead_info *sai)
143{
144 return list_entry(sai->sai_entries_received.next,
145 struct ll_sa_entry, se_list);
146}
147
148static inline struct ll_inode_info *
149agl_first_entry(struct ll_statahead_info *sai)
150{
151 return list_entry(sai->sai_entries_agl.next,
152 struct ll_inode_info, lli_agl_list);
153}
154
155static inline int sa_sent_full(struct ll_statahead_info *sai)
156{
157 return atomic_read(&sai->sai_cache_count) >= sai->sai_max;
158}
159
160static inline int sa_received_empty(struct ll_statahead_info *sai)
161{
162 return list_empty(&sai->sai_entries_received);
163}
164
165static inline int agl_list_empty(struct ll_statahead_info *sai)
166{
167 return list_empty(&sai->sai_entries_agl);
168}
169
170
171
172
173
174
175
176static inline int sa_low_hit(struct ll_statahead_info *sai)
177{
178 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
179 (sai->sai_consecutive_miss > 8));
180}
181
182
183
184
185
186static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
187{
188 return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
189 sai->sai_index);
190}
191
192
193
194
195static struct ll_sa_entry *
196ll_sa_entry_alloc(struct ll_statahead_info *sai, __u64 index,
197 const char *name, int len)
198{
199 struct ll_inode_info *lli;
200 struct ll_sa_entry *entry;
201 int entry_size;
202 char *dname;
203
204 entry_size = sizeof(struct ll_sa_entry) + (len & ~3) + 4;
205 entry = kzalloc(entry_size, GFP_NOFS);
206 if (unlikely(!entry))
207 return ERR_PTR(-ENOMEM);
208
209 CDEBUG(D_READA, "alloc sa entry %.*s(%p) index %llu\n",
210 len, name, entry, index);
211
212 entry->se_index = index;
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 atomic_set(&entry->se_refcount, 2);
238 entry->se_stat = SA_ENTRY_INIT;
239 entry->se_size = entry_size;
240 dname = (char *)entry + sizeof(struct ll_sa_entry);
241 memcpy(dname, name, len);
242 dname[len] = 0;
243 entry->se_qstr.hash = full_name_hash(name, len);
244 entry->se_qstr.len = len;
245 entry->se_qstr.name = dname;
246
247 lli = ll_i2info(sai->sai_inode);
248 spin_lock(&lli->lli_sa_lock);
249 list_add_tail(&entry->se_link, &sai->sai_entries);
250 INIT_LIST_HEAD(&entry->se_list);
251 ll_sa_entry_enhash(sai, entry);
252 spin_unlock(&lli->lli_sa_lock);
253
254 atomic_inc(&sai->sai_cache_count);
255
256 return entry;
257}
258
259
260
261
262
263
264
265
266static struct ll_sa_entry *
267ll_sa_entry_get_byname(struct ll_statahead_info *sai, const struct qstr *qstr)
268{
269 struct ll_sa_entry *entry;
270 int i = ll_sa_entry_hash(qstr->hash);
271
272 list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
273 if (entry->se_qstr.hash == qstr->hash &&
274 entry->se_qstr.len == qstr->len &&
275 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
276 return entry;
277 }
278 return NULL;
279}
280
281
282
283
284
285
286
287
288static struct ll_sa_entry *
289ll_sa_entry_get_byindex(struct ll_statahead_info *sai, __u64 index)
290{
291 struct ll_sa_entry *entry;
292
293 list_for_each_entry(entry, &sai->sai_entries, se_link) {
294 if (entry->se_index == index) {
295 LASSERT(atomic_read(&entry->se_refcount) > 0);
296 atomic_inc(&entry->se_refcount);
297 return entry;
298 }
299 if (entry->se_index > index)
300 break;
301 }
302 return NULL;
303}
304
305static void ll_sa_entry_cleanup(struct ll_statahead_info *sai,
306 struct ll_sa_entry *entry)
307{
308 struct md_enqueue_info *minfo = entry->se_minfo;
309 struct ptlrpc_request *req = entry->se_req;
310
311 if (minfo) {
312 entry->se_minfo = NULL;
313 ll_intent_release(&minfo->mi_it);
314 iput(minfo->mi_dir);
315 OBD_FREE_PTR(minfo);
316 }
317
318 if (req) {
319 entry->se_req = NULL;
320 ptlrpc_req_finished(req);
321 }
322}
323
324static void ll_sa_entry_put(struct ll_statahead_info *sai,
325 struct ll_sa_entry *entry)
326{
327 if (atomic_dec_and_test(&entry->se_refcount)) {
328 CDEBUG(D_READA, "free sa entry %.*s(%p) index %llu\n",
329 entry->se_qstr.len, entry->se_qstr.name, entry,
330 entry->se_index);
331
332 LASSERT(list_empty(&entry->se_link));
333 LASSERT(list_empty(&entry->se_list));
334 LASSERT(ll_sa_entry_unhashed(entry));
335
336 ll_sa_entry_cleanup(sai, entry);
337 iput(entry->se_inode);
338
339 OBD_FREE(entry, entry->se_size);
340 atomic_dec(&sai->sai_cache_count);
341 }
342}
343
344static inline void
345do_sa_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
346{
347 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
348
349 LASSERT(!ll_sa_entry_unhashed(entry));
350 LASSERT(!list_empty(&entry->se_link));
351
352 ll_sa_entry_unhash(sai, entry);
353
354 spin_lock(&lli->lli_sa_lock);
355 entry->se_stat = SA_ENTRY_DEST;
356 list_del_init(&entry->se_link);
357 if (likely(!list_empty(&entry->se_list)))
358 list_del_init(&entry->se_list);
359 spin_unlock(&lli->lli_sa_lock);
360
361 ll_sa_entry_put(sai, entry);
362}
363
364
365
366
367static void
368ll_sa_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
369{
370 struct ll_sa_entry *pos, *next;
371
372 if (entry)
373 do_sa_entry_fini(sai, entry);
374
375
376 list_for_each_entry_safe(pos, next, &sai->sai_entries, se_link) {
377 if (!is_omitted_entry(sai, pos->se_index))
378 break;
379 do_sa_entry_fini(sai, pos);
380 }
381}
382
383
384
385
386static void
387do_sa_entry_to_stated(struct ll_statahead_info *sai,
388 struct ll_sa_entry *entry, se_stat_t stat)
389{
390 struct ll_sa_entry *se;
391 struct list_head *pos = &sai->sai_entries_stated;
392
393 if (!list_empty(&entry->se_list))
394 list_del_init(&entry->se_list);
395
396 list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
397 if (se->se_index < entry->se_index) {
398 pos = &se->se_list;
399 break;
400 }
401 }
402
403 list_add(&entry->se_list, pos);
404 entry->se_stat = stat;
405}
406
407
408
409
410
411
412static int
413ll_sa_entry_to_stated(struct ll_statahead_info *sai,
414 struct ll_sa_entry *entry, se_stat_t stat)
415{
416 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
417 int ret = 1;
418
419 ll_sa_entry_cleanup(sai, entry);
420
421 spin_lock(&lli->lli_sa_lock);
422 if (likely(entry->se_stat != SA_ENTRY_DEST)) {
423 do_sa_entry_to_stated(sai, entry, stat);
424 ret = 0;
425 }
426 spin_unlock(&lli->lli_sa_lock);
427
428 return ret;
429}
430
431
432
433
434static void ll_agl_add(struct ll_statahead_info *sai,
435 struct inode *inode, int index)
436{
437 struct ll_inode_info *child = ll_i2info(inode);
438 struct ll_inode_info *parent = ll_i2info(sai->sai_inode);
439 int added = 0;
440
441 spin_lock(&child->lli_agl_lock);
442 if (child->lli_agl_index == 0) {
443 child->lli_agl_index = index;
444 spin_unlock(&child->lli_agl_lock);
445
446 LASSERT(list_empty(&child->lli_agl_list));
447
448 igrab(inode);
449 spin_lock(&parent->lli_agl_lock);
450 if (agl_list_empty(sai))
451 added = 1;
452 list_add_tail(&child->lli_agl_list, &sai->sai_entries_agl);
453 spin_unlock(&parent->lli_agl_lock);
454 } else {
455 spin_unlock(&child->lli_agl_lock);
456 }
457
458 if (added > 0)
459 wake_up(&sai->sai_agl_thread.t_ctl_waitq);
460}
461
462static struct ll_statahead_info *ll_sai_alloc(void)
463{
464 struct ll_statahead_info *sai;
465 int i;
466
467 sai = kzalloc(sizeof(*sai), GFP_NOFS);
468 if (!sai)
469 return NULL;
470
471 atomic_set(&sai->sai_refcount, 1);
472
473 spin_lock(&sai_generation_lock);
474 sai->sai_generation = ++sai_generation;
475 if (unlikely(sai_generation == 0))
476 sai->sai_generation = ++sai_generation;
477 spin_unlock(&sai_generation_lock);
478
479 sai->sai_max = LL_SA_RPC_MIN;
480 sai->sai_index = 1;
481 init_waitqueue_head(&sai->sai_waitq);
482 init_waitqueue_head(&sai->sai_thread.t_ctl_waitq);
483 init_waitqueue_head(&sai->sai_agl_thread.t_ctl_waitq);
484
485 INIT_LIST_HEAD(&sai->sai_entries);
486 INIT_LIST_HEAD(&sai->sai_entries_received);
487 INIT_LIST_HEAD(&sai->sai_entries_stated);
488 INIT_LIST_HEAD(&sai->sai_entries_agl);
489
490 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
491 INIT_LIST_HEAD(&sai->sai_cache[i]);
492 spin_lock_init(&sai->sai_cache_lock[i]);
493 }
494 atomic_set(&sai->sai_cache_count, 0);
495
496 return sai;
497}
498
499static inline struct ll_statahead_info *
500ll_sai_get(struct ll_statahead_info *sai)
501{
502 atomic_inc(&sai->sai_refcount);
503 return sai;
504}
505
506static void ll_sai_put(struct ll_statahead_info *sai)
507{
508 struct inode *inode = sai->sai_inode;
509 struct ll_inode_info *lli = ll_i2info(inode);
510
511 if (atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
512 struct ll_sa_entry *entry, *next;
513
514 if (unlikely(atomic_read(&sai->sai_refcount) > 0)) {
515
516
517 spin_unlock(&lli->lli_sa_lock);
518 return;
519 }
520
521 LASSERT(lli->lli_opendir_key == NULL);
522 LASSERT(thread_is_stopped(&sai->sai_thread));
523 LASSERT(thread_is_stopped(&sai->sai_agl_thread));
524
525 lli->lli_sai = NULL;
526 lli->lli_opendir_pid = 0;
527 spin_unlock(&lli->lli_sa_lock);
528
529 if (sai->sai_sent > sai->sai_replied)
530 CDEBUG(D_READA, "statahead for dir "DFID
531 " does not finish: [sent:%llu] [replied:%llu]\n",
532 PFID(&lli->lli_fid),
533 sai->sai_sent, sai->sai_replied);
534
535 list_for_each_entry_safe(entry, next,
536 &sai->sai_entries, se_link)
537 do_sa_entry_fini(sai, entry);
538
539 LASSERT(list_empty(&sai->sai_entries));
540 LASSERT(sa_received_empty(sai));
541 LASSERT(list_empty(&sai->sai_entries_stated));
542
543 LASSERT(atomic_read(&sai->sai_cache_count) == 0);
544 LASSERT(agl_list_empty(sai));
545
546 iput(inode);
547 OBD_FREE_PTR(sai);
548 }
549}
550
551
552static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
553{
554 struct ll_inode_info *lli = ll_i2info(inode);
555 __u64 index = lli->lli_agl_index;
556 int rc;
557
558 LASSERT(list_empty(&lli->lli_agl_list));
559
560
561 if (is_omitted_entry(sai, index + 1)) {
562 lli->lli_agl_index = 0;
563 iput(inode);
564 return;
565 }
566
567
568 rc = down_write_trylock(&lli->lli_glimpse_sem);
569 if (rc == 0) {
570 lli->lli_agl_index = 0;
571 iput(inode);
572 return;
573 }
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588 if (lli->lli_glimpse_time != 0 &&
589 time_before(cfs_time_shift(-1), lli->lli_glimpse_time)) {
590 up_write(&lli->lli_glimpse_sem);
591 lli->lli_agl_index = 0;
592 iput(inode);
593 return;
594 }
595
596 CDEBUG(D_READA, "Handling (init) async glimpse: inode = "
597 DFID", idx = %llu\n", PFID(&lli->lli_fid), index);
598
599 cl_agl(inode);
600 lli->lli_agl_index = 0;
601 lli->lli_glimpse_time = cfs_time_current();
602 up_write(&lli->lli_glimpse_sem);
603
604 CDEBUG(D_READA, "Handled (init) async glimpse: inode= "
605 DFID", idx = %llu, rc = %d\n",
606 PFID(&lli->lli_fid), index, rc);
607
608 iput(inode);
609}
610
611static void ll_post_statahead(struct ll_statahead_info *sai)
612{
613 struct inode *dir = sai->sai_inode;
614 struct inode *child;
615 struct ll_inode_info *lli = ll_i2info(dir);
616 struct ll_sa_entry *entry;
617 struct md_enqueue_info *minfo;
618 struct lookup_intent *it;
619 struct ptlrpc_request *req;
620 struct mdt_body *body;
621 int rc = 0;
622
623 spin_lock(&lli->lli_sa_lock);
624 if (unlikely(sa_received_empty(sai))) {
625 spin_unlock(&lli->lli_sa_lock);
626 return;
627 }
628 entry = sa_first_received_entry(sai);
629 atomic_inc(&entry->se_refcount);
630 list_del_init(&entry->se_list);
631 spin_unlock(&lli->lli_sa_lock);
632
633 LASSERT(entry->se_handle != 0);
634
635 minfo = entry->se_minfo;
636 it = &minfo->mi_it;
637 req = entry->se_req;
638 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
639 if (body == NULL) {
640 rc = -EFAULT;
641 goto out;
642 }
643
644 child = entry->se_inode;
645 if (child == NULL) {
646
647
648
649 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
650
651
652
653 if (body->valid & OBD_MD_MDS) {
654 rc = -EAGAIN;
655 goto out;
656 }
657 } else {
658
659
660
661
662 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2, &body->fid1))){
663 entry->se_inode = NULL;
664 iput(child);
665 child = NULL;
666 }
667 }
668
669 it->d.lustre.it_lock_handle = entry->se_handle;
670 rc = md_revalidate_lock(ll_i2mdexp(dir), it, ll_inode2fid(dir), NULL);
671 if (rc != 1) {
672 rc = -EAGAIN;
673 goto out;
674 }
675
676 rc = ll_prep_inode(&child, req, dir->i_sb, it);
677 if (rc)
678 goto out;
679
680 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
681 child, child->i_ino, child->i_generation);
682 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
683
684 entry->se_inode = child;
685
686 if (agl_should_run(sai, child))
687 ll_agl_add(sai, child, entry->se_index);
688
689out:
690
691
692
693
694 rc = ll_sa_entry_to_stated(sai, entry,
695 rc < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
696 if (rc == 0 && entry->se_index == sai->sai_index_wait)
697 wake_up(&sai->sai_waitq);
698 ll_sa_entry_put(sai, entry);
699}
700
701static int ll_statahead_interpret(struct ptlrpc_request *req,
702 struct md_enqueue_info *minfo, int rc)
703{
704 struct lookup_intent *it = &minfo->mi_it;
705 struct inode *dir = minfo->mi_dir;
706 struct ll_inode_info *lli = ll_i2info(dir);
707 struct ll_statahead_info *sai = NULL;
708 struct ll_sa_entry *entry;
709 int wakeup;
710
711 if (it_disposition(it, DISP_LOOKUP_NEG))
712 rc = -ENOENT;
713
714 spin_lock(&lli->lli_sa_lock);
715
716 if (unlikely(lli->lli_sai == NULL ||
717 lli->lli_sai->sai_generation != minfo->mi_generation)) {
718 spin_unlock(&lli->lli_sa_lock);
719 rc = -ESTALE;
720 goto out;
721 } else {
722 sai = ll_sai_get(lli->lli_sai);
723 if (unlikely(!thread_is_running(&sai->sai_thread))) {
724 sai->sai_replied++;
725 spin_unlock(&lli->lli_sa_lock);
726 rc = -EBADFD;
727 goto out;
728 }
729
730 entry = ll_sa_entry_get_byindex(sai, minfo->mi_cbdata);
731 if (entry == NULL) {
732 sai->sai_replied++;
733 spin_unlock(&lli->lli_sa_lock);
734 rc = -EIDRM;
735 goto out;
736 }
737
738 if (rc != 0) {
739 do_sa_entry_to_stated(sai, entry, SA_ENTRY_INVA);
740 wakeup = (entry->se_index == sai->sai_index_wait);
741 } else {
742 entry->se_minfo = minfo;
743 entry->se_req = ptlrpc_request_addref(req);
744
745
746
747
748 entry->se_handle = it->d.lustre.it_lock_handle;
749 ll_intent_drop_lock(it);
750 wakeup = sa_received_empty(sai);
751 list_add_tail(&entry->se_list,
752 &sai->sai_entries_received);
753 }
754 sai->sai_replied++;
755 spin_unlock(&lli->lli_sa_lock);
756
757 ll_sa_entry_put(sai, entry);
758 if (wakeup)
759 wake_up(&sai->sai_thread.t_ctl_waitq);
760 }
761
762out:
763 if (rc != 0) {
764 ll_intent_release(it);
765 iput(dir);
766 OBD_FREE_PTR(minfo);
767 }
768 if (sai != NULL)
769 ll_sai_put(sai);
770 return rc;
771}
772
773static void sa_args_fini(struct md_enqueue_info *minfo,
774 struct ldlm_enqueue_info *einfo)
775{
776 LASSERT(minfo && einfo);
777 iput(minfo->mi_dir);
778 capa_put(minfo->mi_data.op_capa1);
779 capa_put(minfo->mi_data.op_capa2);
780 OBD_FREE_PTR(minfo);
781 OBD_FREE_PTR(einfo);
782}
783
784
785
786
787
788
789
790
791
792
793static int sa_args_init(struct inode *dir, struct inode *child,
794 struct ll_sa_entry *entry, struct md_enqueue_info **pmi,
795 struct ldlm_enqueue_info **pei,
796 struct obd_capa **pcapa)
797{
798 struct qstr *qstr = &entry->se_qstr;
799 struct ll_inode_info *lli = ll_i2info(dir);
800 struct md_enqueue_info *minfo;
801 struct ldlm_enqueue_info *einfo;
802 struct md_op_data *op_data;
803
804 einfo = kzalloc(sizeof(*einfo), GFP_NOFS);
805 if (!einfo)
806 return -ENOMEM;
807
808 minfo = kzalloc(sizeof(*minfo), GFP_NOFS);
809 if (!minfo) {
810 OBD_FREE_PTR(einfo);
811 return -ENOMEM;
812 }
813
814 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, qstr->name,
815 qstr->len, 0, LUSTRE_OPC_ANY, NULL);
816 if (IS_ERR(op_data)) {
817 OBD_FREE_PTR(einfo);
818 OBD_FREE_PTR(minfo);
819 return PTR_ERR(op_data);
820 }
821
822 minfo->mi_it.it_op = IT_GETATTR;
823 minfo->mi_dir = igrab(dir);
824 minfo->mi_cb = ll_statahead_interpret;
825 minfo->mi_generation = lli->lli_sai->sai_generation;
826 minfo->mi_cbdata = entry->se_index;
827
828 einfo->ei_type = LDLM_IBITS;
829 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
830 einfo->ei_cb_bl = ll_md_blocking_ast;
831 einfo->ei_cb_cp = ldlm_completion_ast;
832 einfo->ei_cb_gl = NULL;
833 einfo->ei_cbdata = NULL;
834
835 *pmi = minfo;
836 *pei = einfo;
837 pcapa[0] = op_data->op_capa1;
838 pcapa[1] = op_data->op_capa2;
839
840 return 0;
841}
842
843static int do_sa_lookup(struct inode *dir, struct ll_sa_entry *entry)
844{
845 struct md_enqueue_info *minfo;
846 struct ldlm_enqueue_info *einfo;
847 struct obd_capa *capas[2];
848 int rc;
849
850 rc = sa_args_init(dir, NULL, entry, &minfo, &einfo, capas);
851 if (rc)
852 return rc;
853
854 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
855 if (!rc) {
856 capa_put(capas[0]);
857 capa_put(capas[1]);
858 } else {
859 sa_args_fini(minfo, einfo);
860 }
861
862 return rc;
863}
864
865
866
867
868
869
870
871static int do_sa_revalidate(struct inode *dir, struct ll_sa_entry *entry,
872 struct dentry *dentry)
873{
874 struct inode *inode = dentry->d_inode;
875 struct lookup_intent it = { .it_op = IT_GETATTR,
876 .d.lustre.it_lock_handle = 0 };
877 struct md_enqueue_info *minfo;
878 struct ldlm_enqueue_info *einfo;
879 struct obd_capa *capas[2];
880 int rc;
881
882 if (unlikely(inode == NULL))
883 return 1;
884
885 if (d_mountpoint(dentry))
886 return 1;
887
888 entry->se_inode = igrab(inode);
889 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),
890 NULL);
891 if (rc == 1) {
892 entry->se_handle = it.d.lustre.it_lock_handle;
893 ll_intent_release(&it);
894 return 1;
895 }
896
897 rc = sa_args_init(dir, inode, entry, &minfo, &einfo, capas);
898 if (rc) {
899 entry->se_inode = NULL;
900 iput(inode);
901 return rc;
902 }
903
904 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
905 if (!rc) {
906 capa_put(capas[0]);
907 capa_put(capas[1]);
908 } else {
909 entry->se_inode = NULL;
910 iput(inode);
911 sa_args_fini(minfo, einfo);
912 }
913
914 return rc;
915}
916
917static void ll_statahead_one(struct dentry *parent, const char *entry_name,
918 int entry_name_len)
919{
920 struct inode *dir = parent->d_inode;
921 struct ll_inode_info *lli = ll_i2info(dir);
922 struct ll_statahead_info *sai = lli->lli_sai;
923 struct dentry *dentry = NULL;
924 struct ll_sa_entry *entry;
925 int rc;
926 int rc1;
927
928 entry = ll_sa_entry_alloc(sai, sai->sai_index, entry_name,
929 entry_name_len);
930 if (IS_ERR(entry))
931 return;
932
933 dentry = d_lookup(parent, &entry->se_qstr);
934 if (!dentry) {
935 rc = do_sa_lookup(dir, entry);
936 } else {
937 rc = do_sa_revalidate(dir, entry, dentry);
938 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
939 ll_agl_add(sai, dentry->d_inode, entry->se_index);
940 }
941
942 if (dentry != NULL)
943 dput(dentry);
944
945 if (rc) {
946 rc1 = ll_sa_entry_to_stated(sai, entry,
947 rc < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
948 if (rc1 == 0 && entry->se_index == sai->sai_index_wait)
949 wake_up(&sai->sai_waitq);
950 } else {
951 sai->sai_sent++;
952 }
953
954 sai->sai_index++;
955
956 ll_sa_entry_put(sai, entry);
957}
958
959static int ll_agl_thread(void *arg)
960{
961 struct dentry *parent = (struct dentry *)arg;
962 struct inode *dir = parent->d_inode;
963 struct ll_inode_info *plli = ll_i2info(dir);
964 struct ll_inode_info *clli;
965 struct ll_sb_info *sbi = ll_i2sbi(dir);
966 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
967 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
968 struct l_wait_info lwi = { 0 };
969
970 thread->t_pid = current_pid();
971 CDEBUG(D_READA, "agl thread started: sai %p, parent %pd\n",
972 sai, parent);
973
974 atomic_inc(&sbi->ll_agl_total);
975 spin_lock(&plli->lli_agl_lock);
976 sai->sai_agl_valid = 1;
977 if (thread_is_init(thread))
978
979
980
981 thread_set_flags(thread, SVC_RUNNING);
982 spin_unlock(&plli->lli_agl_lock);
983 wake_up(&thread->t_ctl_waitq);
984
985 while (1) {
986 l_wait_event(thread->t_ctl_waitq,
987 !agl_list_empty(sai) ||
988 !thread_is_running(thread),
989 &lwi);
990
991 if (!thread_is_running(thread))
992 break;
993
994 spin_lock(&plli->lli_agl_lock);
995
996
997 if (!agl_list_empty(sai)) {
998 clli = agl_first_entry(sai);
999 list_del_init(&clli->lli_agl_list);
1000 spin_unlock(&plli->lli_agl_lock);
1001 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1002 } else {
1003 spin_unlock(&plli->lli_agl_lock);
1004 }
1005 }
1006
1007 spin_lock(&plli->lli_agl_lock);
1008 sai->sai_agl_valid = 0;
1009 while (!agl_list_empty(sai)) {
1010 clli = agl_first_entry(sai);
1011 list_del_init(&clli->lli_agl_list);
1012 spin_unlock(&plli->lli_agl_lock);
1013 clli->lli_agl_index = 0;
1014 iput(&clli->lli_vfs_inode);
1015 spin_lock(&plli->lli_agl_lock);
1016 }
1017 thread_set_flags(thread, SVC_STOPPED);
1018 spin_unlock(&plli->lli_agl_lock);
1019 wake_up(&thread->t_ctl_waitq);
1020 ll_sai_put(sai);
1021 CDEBUG(D_READA, "agl thread stopped: sai %p, parent %pd\n",
1022 sai, parent);
1023 return 0;
1024}
1025
1026static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
1027{
1028 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
1029 struct l_wait_info lwi = { 0 };
1030 struct ll_inode_info *plli;
1031 struct task_struct *task;
1032
1033 CDEBUG(D_READA, "start agl thread: sai %p, parent %pd\n",
1034 sai, parent);
1035
1036 plli = ll_i2info(parent->d_inode);
1037 task = kthread_run(ll_agl_thread, parent,
1038 "ll_agl_%u", plli->lli_opendir_pid);
1039 if (IS_ERR(task)) {
1040 CERROR("can't start ll_agl thread, rc: %ld\n", PTR_ERR(task));
1041 thread_set_flags(thread, SVC_STOPPED);
1042 return;
1043 }
1044
1045 l_wait_event(thread->t_ctl_waitq,
1046 thread_is_running(thread) || thread_is_stopped(thread),
1047 &lwi);
1048}
1049
1050static int ll_statahead_thread(void *arg)
1051{
1052 struct dentry *parent = (struct dentry *)arg;
1053 struct inode *dir = parent->d_inode;
1054 struct ll_inode_info *plli = ll_i2info(dir);
1055 struct ll_inode_info *clli;
1056 struct ll_sb_info *sbi = ll_i2sbi(dir);
1057 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
1058 struct ptlrpc_thread *thread = &sai->sai_thread;
1059 struct ptlrpc_thread *agl_thread = &sai->sai_agl_thread;
1060 struct page *page;
1061 __u64 pos = 0;
1062 int first = 0;
1063 int rc = 0;
1064 struct ll_dir_chain chain;
1065 struct l_wait_info lwi = { 0 };
1066
1067 thread->t_pid = current_pid();
1068 CDEBUG(D_READA, "statahead thread starting: sai %p, parent %pd\n",
1069 sai, parent);
1070
1071 if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
1072 ll_start_agl(parent, sai);
1073
1074 atomic_inc(&sbi->ll_sa_total);
1075 spin_lock(&plli->lli_sa_lock);
1076 if (thread_is_init(thread))
1077
1078
1079
1080 thread_set_flags(thread, SVC_RUNNING);
1081 spin_unlock(&plli->lli_sa_lock);
1082 wake_up(&thread->t_ctl_waitq);
1083
1084 ll_dir_chain_init(&chain);
1085 page = ll_get_dir_page(dir, pos, &chain);
1086
1087 while (1) {
1088 struct lu_dirpage *dp;
1089 struct lu_dirent *ent;
1090
1091 if (IS_ERR(page)) {
1092 rc = PTR_ERR(page);
1093 CDEBUG(D_READA, "error reading dir "DFID" at %llu/%llu: [rc %d] [parent %u]\n",
1094 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1095 rc, plli->lli_opendir_pid);
1096 goto out;
1097 }
1098
1099 dp = page_address(page);
1100 for (ent = lu_dirent_start(dp); ent != NULL;
1101 ent = lu_dirent_next(ent)) {
1102 __u64 hash;
1103 int namelen;
1104 char *name;
1105
1106 hash = le64_to_cpu(ent->lde_hash);
1107 if (unlikely(hash < pos))
1108
1109
1110
1111 continue;
1112
1113 namelen = le16_to_cpu(ent->lde_namelen);
1114 if (unlikely(namelen == 0))
1115
1116
1117
1118 continue;
1119
1120 name = ent->lde_name;
1121 if (name[0] == '.') {
1122 if (namelen == 1) {
1123
1124
1125
1126 continue;
1127 } else if (name[1] == '.' && namelen == 2) {
1128
1129
1130
1131 continue;
1132 } else if (!sai->sai_ls_all) {
1133
1134
1135
1136 sai->sai_skip_hidden++;
1137 continue;
1138 }
1139 }
1140
1141
1142
1143
1144 if (unlikely(++first == 1))
1145 continue;
1146
1147keep_it:
1148 l_wait_event(thread->t_ctl_waitq,
1149 !sa_sent_full(sai) ||
1150 !sa_received_empty(sai) ||
1151 !agl_list_empty(sai) ||
1152 !thread_is_running(thread),
1153 &lwi);
1154
1155interpret_it:
1156 while (!sa_received_empty(sai))
1157 ll_post_statahead(sai);
1158
1159 if (unlikely(!thread_is_running(thread))) {
1160 ll_release_page(page, 0);
1161 rc = 0;
1162 goto out;
1163 }
1164
1165
1166
1167
1168 if (sa_sent_full(sai)) {
1169 spin_lock(&plli->lli_agl_lock);
1170 while (!agl_list_empty(sai)) {
1171 clli = agl_first_entry(sai);
1172 list_del_init(&clli->lli_agl_list);
1173 spin_unlock(&plli->lli_agl_lock);
1174 ll_agl_trigger(&clli->lli_vfs_inode,
1175 sai);
1176
1177 if (!sa_received_empty(sai))
1178 goto interpret_it;
1179
1180 if (unlikely(
1181 !thread_is_running(thread))) {
1182 ll_release_page(page, 0);
1183 rc = 0;
1184 goto out;
1185 }
1186
1187 if (!sa_sent_full(sai))
1188 goto do_it;
1189
1190 spin_lock(&plli->lli_agl_lock);
1191 }
1192 spin_unlock(&plli->lli_agl_lock);
1193
1194 goto keep_it;
1195 }
1196
1197do_it:
1198 ll_statahead_one(parent, name, namelen);
1199 }
1200 pos = le64_to_cpu(dp->ldp_hash_end);
1201 if (pos == MDS_DIR_END_OFF) {
1202
1203
1204
1205 ll_release_page(page, 0);
1206 while (1) {
1207 l_wait_event(thread->t_ctl_waitq,
1208 !sa_received_empty(sai) ||
1209 sai->sai_sent == sai->sai_replied||
1210 !thread_is_running(thread),
1211 &lwi);
1212
1213 while (!sa_received_empty(sai))
1214 ll_post_statahead(sai);
1215
1216 if (unlikely(!thread_is_running(thread))) {
1217 rc = 0;
1218 goto out;
1219 }
1220
1221 if (sai->sai_sent == sai->sai_replied &&
1222 sa_received_empty(sai))
1223 break;
1224 }
1225
1226 spin_lock(&plli->lli_agl_lock);
1227 while (!agl_list_empty(sai) &&
1228 thread_is_running(thread)) {
1229 clli = agl_first_entry(sai);
1230 list_del_init(&clli->lli_agl_list);
1231 spin_unlock(&plli->lli_agl_lock);
1232 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1233 spin_lock(&plli->lli_agl_lock);
1234 }
1235 spin_unlock(&plli->lli_agl_lock);
1236
1237 rc = 0;
1238 goto out;
1239 } else if (1) {
1240
1241
1242
1243
1244 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1245 LDF_COLLIDE);
1246 page = ll_get_dir_page(dir, pos, &chain);
1247 } else {
1248 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1249 ll_release_page(page, 1);
1250
1251
1252
1253 }
1254 }
1255
1256out:
1257 if (sai->sai_agl_valid) {
1258 spin_lock(&plli->lli_agl_lock);
1259 thread_set_flags(agl_thread, SVC_STOPPING);
1260 spin_unlock(&plli->lli_agl_lock);
1261 wake_up(&agl_thread->t_ctl_waitq);
1262
1263 CDEBUG(D_READA, "stop agl thread: sai %p pid %u\n",
1264 sai, (unsigned int)agl_thread->t_pid);
1265 l_wait_event(agl_thread->t_ctl_waitq,
1266 thread_is_stopped(agl_thread),
1267 &lwi);
1268 } else {
1269
1270 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1271 }
1272 ll_dir_chain_fini(&chain);
1273 spin_lock(&plli->lli_sa_lock);
1274 if (!sa_received_empty(sai)) {
1275 thread_set_flags(thread, SVC_STOPPING);
1276 spin_unlock(&plli->lli_sa_lock);
1277
1278
1279 while (!sa_received_empty(sai))
1280 ll_post_statahead(sai);
1281
1282 spin_lock(&plli->lli_sa_lock);
1283 }
1284 thread_set_flags(thread, SVC_STOPPED);
1285 spin_unlock(&plli->lli_sa_lock);
1286 wake_up(&sai->sai_waitq);
1287 wake_up(&thread->t_ctl_waitq);
1288 ll_sai_put(sai);
1289 dput(parent);
1290 CDEBUG(D_READA, "statahead thread stopped: sai %p, parent %pd\n",
1291 sai, parent);
1292 return rc;
1293}
1294
1295
1296
1297
1298void ll_stop_statahead(struct inode *dir, void *key)
1299{
1300 struct ll_inode_info *lli = ll_i2info(dir);
1301
1302 if (unlikely(key == NULL))
1303 return;
1304
1305 spin_lock(&lli->lli_sa_lock);
1306 if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
1307 spin_unlock(&lli->lli_sa_lock);
1308 return;
1309 }
1310
1311 lli->lli_opendir_key = NULL;
1312
1313 if (lli->lli_sai) {
1314 struct l_wait_info lwi = { 0 };
1315 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
1316
1317 if (!thread_is_stopped(thread)) {
1318 thread_set_flags(thread, SVC_STOPPING);
1319 spin_unlock(&lli->lli_sa_lock);
1320 wake_up(&thread->t_ctl_waitq);
1321
1322 CDEBUG(D_READA, "stop statahead thread: sai %p pid %u\n",
1323 lli->lli_sai, (unsigned int)thread->t_pid);
1324 l_wait_event(thread->t_ctl_waitq,
1325 thread_is_stopped(thread),
1326 &lwi);
1327 } else {
1328 spin_unlock(&lli->lli_sa_lock);
1329 }
1330
1331
1332
1333
1334
1335
1336 ll_sai_put(lli->lli_sai);
1337 } else {
1338 lli->lli_opendir_pid = 0;
1339 spin_unlock(&lli->lli_sa_lock);
1340 }
1341}
1342
1343enum {
1344
1345
1346
1347 LS_NONE_FIRST_DE = 0,
1348
1349
1350
1351 LS_FIRST_DE,
1352
1353
1354
1355 LS_FIRST_DOT_DE
1356};
1357
1358static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1359{
1360 struct ll_dir_chain chain;
1361 struct qstr *target = &dentry->d_name;
1362 struct page *page;
1363 __u64 pos = 0;
1364 int dot_de;
1365 int rc = LS_NONE_FIRST_DE;
1366
1367 ll_dir_chain_init(&chain);
1368 page = ll_get_dir_page(dir, pos, &chain);
1369
1370 while (1) {
1371 struct lu_dirpage *dp;
1372 struct lu_dirent *ent;
1373
1374 if (IS_ERR(page)) {
1375 struct ll_inode_info *lli = ll_i2info(dir);
1376
1377 rc = PTR_ERR(page);
1378 CERROR("error reading dir "DFID" at %llu: [rc %d] [parent %u]\n",
1379 PFID(ll_inode2fid(dir)), pos,
1380 rc, lli->lli_opendir_pid);
1381 break;
1382 }
1383
1384 dp = page_address(page);
1385 for (ent = lu_dirent_start(dp); ent != NULL;
1386 ent = lu_dirent_next(ent)) {
1387 __u64 hash;
1388 int namelen;
1389 char *name;
1390
1391 hash = le64_to_cpu(ent->lde_hash);
1392
1393
1394 if (unlikely(hash < pos))
1395 continue;
1396
1397 namelen = le16_to_cpu(ent->lde_namelen);
1398 if (unlikely(namelen == 0))
1399
1400
1401
1402 continue;
1403
1404 name = ent->lde_name;
1405 if (name[0] == '.') {
1406 if (namelen == 1)
1407
1408
1409
1410 continue;
1411 else if (name[1] == '.' && namelen == 2)
1412
1413
1414
1415 continue;
1416 else
1417 dot_de = 1;
1418 } else {
1419 dot_de = 0;
1420 }
1421
1422 if (dot_de && target->name[0] != '.') {
1423 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1424 target->len, target->name,
1425 namelen, name);
1426 continue;
1427 }
1428
1429 if (target->len != namelen ||
1430 memcmp(target->name, name, namelen) != 0)
1431 rc = LS_NONE_FIRST_DE;
1432 else if (!dot_de)
1433 rc = LS_FIRST_DE;
1434 else
1435 rc = LS_FIRST_DOT_DE;
1436
1437 ll_release_page(page, 0);
1438 goto out;
1439 }
1440 pos = le64_to_cpu(dp->ldp_hash_end);
1441 if (pos == MDS_DIR_END_OFF) {
1442
1443
1444
1445 ll_release_page(page, 0);
1446 break;
1447 } else if (1) {
1448
1449
1450
1451
1452 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1453 LDF_COLLIDE);
1454 page = ll_get_dir_page(dir, pos, &chain);
1455 } else {
1456
1457
1458
1459 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1460 ll_release_page(page, 1);
1461 }
1462 }
1463
1464out:
1465 ll_dir_chain_fini(&chain);
1466 return rc;
1467}
1468
1469static void
1470ll_sai_unplug(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
1471{
1472 struct ptlrpc_thread *thread = &sai->sai_thread;
1473 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
1474 int hit;
1475
1476 if (entry != NULL && entry->se_stat == SA_ENTRY_SUCC)
1477 hit = 1;
1478 else
1479 hit = 0;
1480
1481 ll_sa_entry_fini(sai, entry);
1482 if (hit) {
1483 sai->sai_hit++;
1484 sai->sai_consecutive_miss = 0;
1485 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1486 } else {
1487 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
1488
1489 sai->sai_miss++;
1490 sai->sai_consecutive_miss++;
1491 if (sa_low_hit(sai) && thread_is_running(thread)) {
1492 atomic_inc(&sbi->ll_sa_wrong);
1493 CDEBUG(D_READA, "Statahead for dir " DFID " hit ratio too low: hit/miss %llu/%llu, sent/replied %llu/%llu, stopping statahead thread\n",
1494 PFID(&lli->lli_fid), sai->sai_hit,
1495 sai->sai_miss, sai->sai_sent,
1496 sai->sai_replied);
1497 spin_lock(&lli->lli_sa_lock);
1498 if (!thread_is_stopped(thread))
1499 thread_set_flags(thread, SVC_STOPPING);
1500 spin_unlock(&lli->lli_sa_lock);
1501 }
1502 }
1503
1504 if (!thread_is_stopped(thread))
1505 wake_up(&thread->t_ctl_waitq);
1506}
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1518 int only_unplug)
1519{
1520 struct ll_inode_info *lli = ll_i2info(dir);
1521 struct ll_statahead_info *sai = lli->lli_sai;
1522 struct dentry *parent;
1523 struct ll_sa_entry *entry;
1524 struct ptlrpc_thread *thread;
1525 struct l_wait_info lwi = { 0 };
1526 int rc = 0;
1527 struct ll_inode_info *plli;
1528
1529 LASSERT(lli->lli_opendir_pid == current_pid());
1530
1531 if (sai) {
1532 thread = &sai->sai_thread;
1533 if (unlikely(thread_is_stopped(thread) &&
1534 list_empty(&sai->sai_entries_stated))) {
1535
1536 ll_stop_statahead(dir, lli->lli_opendir_key);
1537 return -EAGAIN;
1538 }
1539
1540 if ((*dentryp)->d_name.name[0] == '.') {
1541 if (sai->sai_ls_all ||
1542 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1543
1544
1545
1546
1547
1548 } else {
1549 if (!sai->sai_ls_all)
1550
1551
1552
1553
1554
1555
1556 sai->sai_ls_all = 1;
1557
1558
1559
1560
1561
1562 sai->sai_miss_hidden++;
1563 return -EAGAIN;
1564 }
1565 }
1566
1567 entry = ll_sa_entry_get_byname(sai, &(*dentryp)->d_name);
1568 if (entry == NULL || only_unplug) {
1569 ll_sai_unplug(sai, entry);
1570 return entry ? 1 : -EAGAIN;
1571 }
1572
1573 if (!ll_sa_entry_stated(entry)) {
1574 sai->sai_index_wait = entry->se_index;
1575 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1576 LWI_ON_SIGNAL_NOOP, NULL);
1577 rc = l_wait_event(sai->sai_waitq,
1578 ll_sa_entry_stated(entry) ||
1579 thread_is_stopped(thread),
1580 &lwi);
1581 if (rc < 0) {
1582 ll_sai_unplug(sai, entry);
1583 return -EAGAIN;
1584 }
1585 }
1586
1587 if (entry->se_stat == SA_ENTRY_SUCC &&
1588 entry->se_inode != NULL) {
1589 struct inode *inode = entry->se_inode;
1590 struct lookup_intent it = { .it_op = IT_GETATTR,
1591 .d.lustre.it_lock_handle =
1592 entry->se_handle };
1593 __u64 bits;
1594
1595 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1596 ll_inode2fid(inode), &bits);
1597 if (rc == 1) {
1598 if ((*dentryp)->d_inode == NULL) {
1599 struct dentry *alias;
1600
1601 alias = ll_splice_alias(inode,
1602 *dentryp);
1603 if (IS_ERR(alias)) {
1604 ll_sai_unplug(sai, entry);
1605 return PTR_ERR(alias);
1606 }
1607 *dentryp = alias;
1608 } else if ((*dentryp)->d_inode != inode) {
1609
1610 CDEBUG(D_READA,
1611 "stale dentry %pd inode %lu/%u, statahead inode %lu/%u\n",
1612 *dentryp,
1613 (*dentryp)->d_inode->i_ino,
1614 (*dentryp)->d_inode->i_generation,
1615 inode->i_ino,
1616 inode->i_generation);
1617 ll_sai_unplug(sai, entry);
1618 return -ESTALE;
1619 } else {
1620 iput(inode);
1621 }
1622 entry->se_inode = NULL;
1623
1624 if ((bits & MDS_INODELOCK_LOOKUP) &&
1625 d_lustre_invalid(*dentryp))
1626 d_lustre_revalidate(*dentryp);
1627 ll_intent_release(&it);
1628 }
1629 }
1630
1631 ll_sai_unplug(sai, entry);
1632 return rc;
1633 }
1634
1635
1636 rc = is_first_dirent(dir, *dentryp);
1637 if (rc == LS_NONE_FIRST_DE) {
1638
1639 rc = -EAGAIN;
1640 goto out;
1641 }
1642
1643 sai = ll_sai_alloc();
1644 if (sai == NULL) {
1645 rc = -ENOMEM;
1646 goto out;
1647 }
1648
1649 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1650 sai->sai_inode = igrab(dir);
1651 if (unlikely(sai->sai_inode == NULL)) {
1652 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1653 PFID(&lli->lli_fid));
1654 rc = -ESTALE;
1655 goto out;
1656 }
1657
1658
1659 parent = dget((*dentryp)->d_parent);
1660 if (unlikely(sai->sai_inode != parent->d_inode)) {
1661 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1662
1663 CWARN("Race condition, someone changed %pd just now: old parent "DFID", new parent "DFID"\n",
1664 *dentryp,
1665 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1666 dput(parent);
1667 iput(sai->sai_inode);
1668 rc = -EAGAIN;
1669 goto out;
1670 }
1671
1672 CDEBUG(D_READA, "start statahead thread: sai %p, parent %pd\n",
1673 sai, parent);
1674
1675
1676
1677
1678
1679
1680 ll_sai_get(sai);
1681 lli->lli_sai = sai;
1682
1683 plli = ll_i2info(parent->d_inode);
1684 rc = PTR_ERR(kthread_run(ll_statahead_thread, parent,
1685 "ll_sa_%u", plli->lli_opendir_pid));
1686 thread = &sai->sai_thread;
1687 if (IS_ERR_VALUE(rc)) {
1688 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1689 dput(parent);
1690 lli->lli_opendir_key = NULL;
1691 thread_set_flags(thread, SVC_STOPPED);
1692 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1693
1694
1695 ll_sai_put(sai);
1696 ll_sai_put(sai);
1697 LASSERT(lli->lli_sai == NULL);
1698 return -EAGAIN;
1699 }
1700
1701 l_wait_event(thread->t_ctl_waitq,
1702 thread_is_running(thread) || thread_is_stopped(thread),
1703 &lwi);
1704 ll_sai_put(sai);
1705
1706
1707
1708
1709
1710 return -EAGAIN;
1711
1712out:
1713 if (sai != NULL)
1714 OBD_FREE_PTR(sai);
1715 spin_lock(&lli->lli_sa_lock);
1716 lli->lli_opendir_key = NULL;
1717 lli->lli_opendir_pid = 0;
1718 spin_unlock(&lli->lli_sa_lock);
1719 return rc;
1720}
1721