• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
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 
52 typedef enum {
53 	/** negative values are for error cases */
54 	SA_ENTRY_INIT = 0,      /** init entry */
55 	SA_ENTRY_SUCC = 1,      /** stat succeed */
56 	SA_ENTRY_INVA = 2,      /** invalid entry */
57 	SA_ENTRY_DEST = 3,      /** entry to be destroyed */
58 } se_stat_t;
59 
60 struct ll_sa_entry {
61 	/* link into sai->sai_entries */
62 	struct list_head	      se_link;
63 	/* link into sai->sai_entries_{received,stated} */
64 	struct list_head	      se_list;
65 	/* link into sai hash table locally */
66 	struct list_head	      se_hash;
67 	/* entry reference count */
68 	atomic_t	    se_refcount;
69 	/* entry index in the sai */
70 	__u64		   se_index;
71 	/* low layer ldlm lock handle */
72 	__u64		   se_handle;
73 	/* entry status */
74 	se_stat_t	       se_stat;
75 	/* entry size, contains name */
76 	int		     se_size;
77 	/* pointer to async getattr enqueue info */
78 	struct md_enqueue_info *se_minfo;
79 	/* pointer to the async getattr request */
80 	struct ptlrpc_request  *se_req;
81 	/* pointer to the target inode */
82 	struct inode	   *se_inode;
83 	/* entry name */
84 	struct qstr	     se_qstr;
85 };
86 
87 static unsigned int sai_generation;
88 static DEFINE_SPINLOCK(sai_generation_lock);
89 
ll_sa_entry_unhashed(struct ll_sa_entry * entry)90 static inline int ll_sa_entry_unhashed(struct ll_sa_entry *entry)
91 {
92 	return list_empty(&entry->se_hash);
93 }
94 
95 /*
96  * The entry only can be released by the caller, it is necessary to hold lock.
97  */
ll_sa_entry_stated(struct ll_sa_entry * entry)98 static 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 
ll_sa_entry_hash(int val)104 static inline int ll_sa_entry_hash(int val)
105 {
106 	return val & LL_SA_CACHE_MASK;
107 }
108 
109 /*
110  * Insert entry to hash SA table.
111  */
112 static inline void
ll_sa_entry_enhash(struct ll_statahead_info * sai,struct ll_sa_entry * entry)113 ll_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  * Remove entry from SA table.
124  */
125 static inline void
ll_sa_entry_unhash(struct ll_statahead_info * sai,struct ll_sa_entry * entry)126 ll_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 
agl_should_run(struct ll_statahead_info * sai,struct inode * inode)135 static 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 
141 static inline struct ll_sa_entry *
sa_first_received_entry(struct ll_statahead_info * sai)142 sa_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 
148 static inline struct ll_inode_info *
agl_first_entry(struct ll_statahead_info * sai)149 agl_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 
sa_sent_full(struct ll_statahead_info * sai)155 static inline int sa_sent_full(struct ll_statahead_info *sai)
156 {
157 	return atomic_read(&sai->sai_cache_count) >= sai->sai_max;
158 }
159 
sa_received_empty(struct ll_statahead_info * sai)160 static inline int sa_received_empty(struct ll_statahead_info *sai)
161 {
162 	return list_empty(&sai->sai_entries_received);
163 }
164 
agl_list_empty(struct ll_statahead_info * sai)165 static inline int agl_list_empty(struct ll_statahead_info *sai)
166 {
167 	return list_empty(&sai->sai_entries_agl);
168 }
169 
170 /**
171  * (1) hit ratio less than 80%
172  * or
173  * (2) consecutive miss more than 8
174  * then means low hit.
175  */
sa_low_hit(struct ll_statahead_info * sai)176 static 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  * If the given index is behind of statahead window more than
184  * SA_OMITTED_ENTRY_MAX, then it is old.
185  */
is_omitted_entry(struct ll_statahead_info * sai,__u64 index)186 static 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  * Insert it into sai_entries tail when init.
194  */
195 static struct ll_sa_entry *
ll_sa_entry_alloc(struct ll_statahead_info * sai,__u64 index,const char * name,int len)196 ll_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 	 * Statahead entry reference rules:
216 	 *
217 	 * 1) When statahead entry is initialized, its reference is set as 2.
218 	 *    One reference is used by the directory scanner. When the scanner
219 	 *    searches the statahead cache for the given name, it can perform
220 	 *    lockless hash lookup (only the scanner can remove entry from hash
221 	 *    list), and once found, it needn't to call "atomic_inc()" for the
222 	 *    entry reference. So the performance is improved. After using the
223 	 *    statahead entry, the scanner will call "atomic_dec()" to drop the
224 	 *    reference held when initialization. If it is the last reference,
225 	 *    the statahead entry will be freed.
226 	 *
227 	 * 2) All other threads, including statahead thread and ptlrpcd thread,
228 	 *    when they process the statahead entry, the reference for target
229 	 *    should be held to guarantee the entry will not be released by the
230 	 *    directory scanner. After processing the entry, these threads will
231 	 *    drop the entry reference. If it is the last reference, the entry
232 	 *    will be freed.
233 	 *
234 	 *    The second reference when initializes the statahead entry is used
235 	 *    by the statahead thread, following the rule 2).
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  * Used by the directory scanner to search entry with name.
261  *
262  * Only the caller can remove the entry from hash, so it is unnecessary to hold
263  * hash lock. It is caller's duty to release the init refcount on the entry, so
264  * it is also unnecessary to increase refcount on the entry.
265  */
266 static struct ll_sa_entry *
ll_sa_entry_get_byname(struct ll_statahead_info * sai,const struct qstr * qstr)267 ll_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  * Used by the async getattr request callback to find entry with index.
283  *
284  * Inside lli_sa_lock to prevent others to change the list during the search.
285  * It needs to increase entry refcount before returning to guarantee that the
286  * entry cannot be freed by others.
287  */
288 static struct ll_sa_entry *
ll_sa_entry_get_byindex(struct ll_statahead_info * sai,__u64 index)289 ll_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 
ll_sa_entry_cleanup(struct ll_statahead_info * sai,struct ll_sa_entry * entry)305 static 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 		kfree(minfo);
316 	}
317 
318 	if (req) {
319 		entry->se_req = NULL;
320 		ptlrpc_req_finished(req);
321 	}
322 }
323 
ll_sa_entry_put(struct ll_statahead_info * sai,struct ll_sa_entry * entry)324 static 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 		kfree(entry);
340 		atomic_dec(&sai->sai_cache_count);
341 	}
342 }
343 
344 static inline void
do_sa_entry_fini(struct ll_statahead_info * sai,struct ll_sa_entry * entry)345 do_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  * Delete it from sai_entries_stated list when fini.
366  */
367 static void
ll_sa_entry_fini(struct ll_statahead_info * sai,struct ll_sa_entry * entry)368 ll_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 	/* drop old entry, only 'scanner' process does this, no need to lock */
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  * Inside lli_sa_lock.
385  */
386 static void
do_sa_entry_to_stated(struct ll_statahead_info * sai,struct ll_sa_entry * entry,se_stat_t stat)387 do_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  * Move entry to sai_entries_stated and sort with the index.
409  * \retval 1    -- entry to be destroyed.
410  * \retval 0    -- entry is inserted into stated list.
411  */
412 static int
ll_sa_entry_to_stated(struct ll_statahead_info * sai,struct ll_sa_entry * entry,se_stat_t stat)413 ll_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  * Insert inode into the list of sai_entries_agl.
433  */
ll_agl_add(struct ll_statahead_info * sai,struct inode * inode,int index)434 static 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 
ll_sai_alloc(void)462 static 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 
499 static inline struct ll_statahead_info *
ll_sai_get(struct ll_statahead_info * sai)500 ll_sai_get(struct ll_statahead_info *sai)
501 {
502 	atomic_inc(&sai->sai_refcount);
503 	return sai;
504 }
505 
ll_sai_put(struct ll_statahead_info * sai)506 static 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 			/* It is race case, the interpret callback just hold
516 			 * a reference count */
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 		kfree(sai);
548 	}
549 }
550 
551 /* Do NOT forget to drop inode refcount when into sai_entries_agl. */
ll_agl_trigger(struct inode * inode,struct ll_statahead_info * sai)552 static 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 	/* AGL maybe fall behind statahead with one entry */
561 	if (is_omitted_entry(sai, index + 1)) {
562 		lli->lli_agl_index = 0;
563 		iput(inode);
564 		return;
565 	}
566 
567 	/* Someone is in glimpse (sync or async), do nothing. */
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 	 * Someone triggered glimpse within 1 sec before.
577 	 * 1) The former glimpse succeeded with glimpse lock granted by OST, and
578 	 *    if the lock is still cached on client, AGL needs to do nothing. If
579 	 *    it is cancelled by other client, AGL maybe cannot obtain new lock
580 	 *    for no glimpse callback triggered by AGL.
581 	 * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
582 	 *    Under such case, it is quite possible that the OST will not grant
583 	 *    glimpse lock for AGL also.
584 	 * 3) The former glimpse failed, compared with other two cases, it is
585 	 *    relative rare. AGL can ignore such case, and it will not muchly
586 	 *    affect the performance.
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 
ll_post_statahead(struct ll_statahead_info * sai)611 static 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 		 * lookup.
648 		 */
649 		LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
650 
651 		/* XXX: No fid in reply, this is probably cross-ref case.
652 		 * SA can't handle it yet. */
653 		if (body->valid & OBD_MD_MDS) {
654 			rc = -EAGAIN;
655 			goto out;
656 		}
657 	} else {
658 		/*
659 		 * revalidate.
660 		 */
661 		/* unlinked and re-created with the same name */
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 
689 out:
690 	/* The "ll_sa_entry_to_stated()" will drop related ldlm ibits lock
691 	 * reference count by calling "ll_intent_drop_lock()" in spite of the
692 	 * above operations failed or not. Do not worry about calling
693 	 * "ll_intent_drop_lock()" more than once. */
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 
ll_statahead_interpret(struct ptlrpc_request * req,struct md_enqueue_info * minfo,int rc)701 static 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 	__u64			  handle = 0;
710 	int		       wakeup;
711 
712 	if (it_disposition(it, DISP_LOOKUP_NEG))
713 		rc = -ENOENT;
714 
715 	if (rc == 0) {
716 		/* release ibits lock ASAP to avoid deadlock when statahead
717 		 * thread enqueues lock on parent in readdir and another
718 		 * process enqueues lock on child with parent lock held, eg.
719 		 * unlink. */
720 		handle = it->d.lustre.it_lock_handle;
721 		ll_intent_drop_lock(it);
722 	}
723 
724 	spin_lock(&lli->lli_sa_lock);
725 	/* stale entry */
726 	if (unlikely(lli->lli_sai == NULL ||
727 		     lli->lli_sai->sai_generation != minfo->mi_generation)) {
728 		spin_unlock(&lli->lli_sa_lock);
729 		rc = -ESTALE;
730 		goto out;
731 	} else {
732 		sai = ll_sai_get(lli->lli_sai);
733 		if (unlikely(!thread_is_running(&sai->sai_thread))) {
734 			sai->sai_replied++;
735 			spin_unlock(&lli->lli_sa_lock);
736 			rc = -EBADFD;
737 			goto out;
738 		}
739 
740 		entry = ll_sa_entry_get_byindex(sai, minfo->mi_cbdata);
741 		if (entry == NULL) {
742 			sai->sai_replied++;
743 			spin_unlock(&lli->lli_sa_lock);
744 			rc = -EIDRM;
745 			goto out;
746 		}
747 
748 		if (rc != 0) {
749 			do_sa_entry_to_stated(sai, entry, SA_ENTRY_INVA);
750 			wakeup = (entry->se_index == sai->sai_index_wait);
751 		} else {
752 			entry->se_minfo = minfo;
753 			entry->se_req = ptlrpc_request_addref(req);
754 			/* Release the async ibits lock ASAP to avoid deadlock
755 			 * when statahead thread tries to enqueue lock on parent
756 			 * for readpage and other tries to enqueue lock on child
757 			 * with parent's lock held, for example: unlink. */
758 			entry->se_handle = handle;
759 			wakeup = sa_received_empty(sai);
760 			list_add_tail(&entry->se_list,
761 					  &sai->sai_entries_received);
762 		}
763 		sai->sai_replied++;
764 		spin_unlock(&lli->lli_sa_lock);
765 
766 		ll_sa_entry_put(sai, entry);
767 		if (wakeup)
768 			wake_up(&sai->sai_thread.t_ctl_waitq);
769 	}
770 
771 out:
772 	if (rc != 0) {
773 		ll_intent_release(it);
774 		iput(dir);
775 		kfree(minfo);
776 	}
777 	if (sai != NULL)
778 		ll_sai_put(sai);
779 	return rc;
780 }
781 
sa_args_fini(struct md_enqueue_info * minfo,struct ldlm_enqueue_info * einfo)782 static void sa_args_fini(struct md_enqueue_info *minfo,
783 			 struct ldlm_enqueue_info *einfo)
784 {
785 	LASSERT(minfo && einfo);
786 	iput(minfo->mi_dir);
787 	kfree(minfo);
788 	kfree(einfo);
789 }
790 
791 /**
792  * prepare arguments for async stat RPC.
793  */
sa_args_init(struct inode * dir,struct inode * child,struct ll_sa_entry * entry,struct md_enqueue_info ** pmi,struct ldlm_enqueue_info ** pei)794 static int sa_args_init(struct inode *dir, struct inode *child,
795 			struct ll_sa_entry *entry, struct md_enqueue_info **pmi,
796 			struct ldlm_enqueue_info **pei)
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 		kfree(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 		kfree(einfo);
818 		kfree(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 
838 	return 0;
839 }
840 
do_sa_lookup(struct inode * dir,struct ll_sa_entry * entry)841 static int do_sa_lookup(struct inode *dir, struct ll_sa_entry *entry)
842 {
843 	struct md_enqueue_info   *minfo;
844 	struct ldlm_enqueue_info *einfo;
845 	int		       rc;
846 
847 	rc = sa_args_init(dir, NULL, entry, &minfo, &einfo);
848 	if (rc)
849 		return rc;
850 
851 	rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
852 	if (rc < 0)
853 		sa_args_fini(minfo, einfo);
854 
855 	return rc;
856 }
857 
858 /**
859  * similar to ll_revalidate_it().
860  * \retval      1 -- dentry valid
861  * \retval      0 -- will send stat-ahead request
862  * \retval others -- prepare stat-ahead request failed
863  */
do_sa_revalidate(struct inode * dir,struct ll_sa_entry * entry,struct dentry * dentry)864 static int do_sa_revalidate(struct inode *dir, struct ll_sa_entry *entry,
865 			    struct dentry *dentry)
866 {
867 	struct inode	     *inode = d_inode(dentry);
868 	struct lookup_intent      it = { .it_op = IT_GETATTR,
869 					 .d.lustre.it_lock_handle = 0 };
870 	struct md_enqueue_info   *minfo;
871 	struct ldlm_enqueue_info *einfo;
872 	int rc;
873 
874 	if (unlikely(inode == NULL))
875 		return 1;
876 
877 	if (d_mountpoint(dentry))
878 		return 1;
879 
880 	entry->se_inode = igrab(inode);
881 	rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),
882 				NULL);
883 	if (rc == 1) {
884 		entry->se_handle = it.d.lustre.it_lock_handle;
885 		ll_intent_release(&it);
886 		return 1;
887 	}
888 
889 	rc = sa_args_init(dir, inode, entry, &minfo, &einfo);
890 	if (rc) {
891 		entry->se_inode = NULL;
892 		iput(inode);
893 		return rc;
894 	}
895 
896 	rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
897 	if (rc < 0) {
898 		entry->se_inode = NULL;
899 		iput(inode);
900 		sa_args_fini(minfo, einfo);
901 	}
902 
903 	return rc;
904 }
905 
ll_statahead_one(struct dentry * parent,const char * entry_name,int entry_name_len)906 static void ll_statahead_one(struct dentry *parent, const char *entry_name,
907 			     int entry_name_len)
908 {
909 	struct inode	     *dir    = d_inode(parent);
910 	struct ll_inode_info     *lli    = ll_i2info(dir);
911 	struct ll_statahead_info *sai    = lli->lli_sai;
912 	struct dentry	    *dentry = NULL;
913 	struct ll_sa_entry       *entry;
914 	int		       rc;
915 	int		       rc1;
916 
917 	entry = ll_sa_entry_alloc(sai, sai->sai_index, entry_name,
918 				  entry_name_len);
919 	if (IS_ERR(entry))
920 		return;
921 
922 	dentry = d_lookup(parent, &entry->se_qstr);
923 	if (!dentry) {
924 		rc = do_sa_lookup(dir, entry);
925 	} else {
926 		rc = do_sa_revalidate(dir, entry, dentry);
927 		if (rc == 1 && agl_should_run(sai, d_inode(dentry)))
928 			ll_agl_add(sai, d_inode(dentry), entry->se_index);
929 	}
930 
931 	if (dentry != NULL)
932 		dput(dentry);
933 
934 	if (rc) {
935 		rc1 = ll_sa_entry_to_stated(sai, entry,
936 					rc < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
937 		if (rc1 == 0 && entry->se_index == sai->sai_index_wait)
938 			wake_up(&sai->sai_waitq);
939 	} else {
940 		sai->sai_sent++;
941 	}
942 
943 	sai->sai_index++;
944 	/* drop one refcount on entry by ll_sa_entry_alloc */
945 	ll_sa_entry_put(sai, entry);
946 }
947 
ll_agl_thread(void * arg)948 static int ll_agl_thread(void *arg)
949 {
950 	struct dentry	    *parent = arg;
951 	struct inode	     *dir    = d_inode(parent);
952 	struct ll_inode_info     *plli   = ll_i2info(dir);
953 	struct ll_inode_info     *clli;
954 	struct ll_sb_info	*sbi    = ll_i2sbi(dir);
955 	struct ll_statahead_info *sai    = ll_sai_get(plli->lli_sai);
956 	struct ptlrpc_thread     *thread = &sai->sai_agl_thread;
957 	struct l_wait_info	lwi    = { 0 };
958 
959 	thread->t_pid = current_pid();
960 	CDEBUG(D_READA, "agl thread started: sai %p, parent %pd\n",
961 	       sai, parent);
962 
963 	atomic_inc(&sbi->ll_agl_total);
964 	spin_lock(&plli->lli_agl_lock);
965 	sai->sai_agl_valid = 1;
966 	if (thread_is_init(thread))
967 		/* If someone else has changed the thread state
968 		 * (e.g. already changed to SVC_STOPPING), we can't just
969 		 * blindly overwrite that setting. */
970 		thread_set_flags(thread, SVC_RUNNING);
971 	spin_unlock(&plli->lli_agl_lock);
972 	wake_up(&thread->t_ctl_waitq);
973 
974 	while (1) {
975 		l_wait_event(thread->t_ctl_waitq,
976 			     !agl_list_empty(sai) ||
977 			     !thread_is_running(thread),
978 			     &lwi);
979 
980 		if (!thread_is_running(thread))
981 			break;
982 
983 		spin_lock(&plli->lli_agl_lock);
984 		/* The statahead thread maybe help to process AGL entries,
985 		 * so check whether list empty again. */
986 		if (!agl_list_empty(sai)) {
987 			clli = agl_first_entry(sai);
988 			list_del_init(&clli->lli_agl_list);
989 			spin_unlock(&plli->lli_agl_lock);
990 			ll_agl_trigger(&clli->lli_vfs_inode, sai);
991 		} else {
992 			spin_unlock(&plli->lli_agl_lock);
993 		}
994 	}
995 
996 	spin_lock(&plli->lli_agl_lock);
997 	sai->sai_agl_valid = 0;
998 	while (!agl_list_empty(sai)) {
999 		clli = agl_first_entry(sai);
1000 		list_del_init(&clli->lli_agl_list);
1001 		spin_unlock(&plli->lli_agl_lock);
1002 		clli->lli_agl_index = 0;
1003 		iput(&clli->lli_vfs_inode);
1004 		spin_lock(&plli->lli_agl_lock);
1005 	}
1006 	thread_set_flags(thread, SVC_STOPPED);
1007 	spin_unlock(&plli->lli_agl_lock);
1008 	wake_up(&thread->t_ctl_waitq);
1009 	ll_sai_put(sai);
1010 	CDEBUG(D_READA, "agl thread stopped: sai %p, parent %pd\n",
1011 	       sai, parent);
1012 	return 0;
1013 }
1014 
ll_start_agl(struct dentry * parent,struct ll_statahead_info * sai)1015 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
1016 {
1017 	struct ptlrpc_thread *thread = &sai->sai_agl_thread;
1018 	struct l_wait_info    lwi    = { 0 };
1019 	struct ll_inode_info  *plli;
1020 	struct task_struct *task;
1021 
1022 	CDEBUG(D_READA, "start agl thread: sai %p, parent %pd\n",
1023 	       sai, parent);
1024 
1025 	plli = ll_i2info(d_inode(parent));
1026 	task = kthread_run(ll_agl_thread, parent,
1027 			       "ll_agl_%u", plli->lli_opendir_pid);
1028 	if (IS_ERR(task)) {
1029 		CERROR("can't start ll_agl thread, rc: %ld\n", PTR_ERR(task));
1030 		thread_set_flags(thread, SVC_STOPPED);
1031 		return;
1032 	}
1033 
1034 	l_wait_event(thread->t_ctl_waitq,
1035 		     thread_is_running(thread) || thread_is_stopped(thread),
1036 		     &lwi);
1037 }
1038 
ll_statahead_thread(void * arg)1039 static int ll_statahead_thread(void *arg)
1040 {
1041 	struct dentry	    *parent = arg;
1042 	struct inode	     *dir    = d_inode(parent);
1043 	struct ll_inode_info     *plli   = ll_i2info(dir);
1044 	struct ll_inode_info     *clli;
1045 	struct ll_sb_info	*sbi    = ll_i2sbi(dir);
1046 	struct ll_statahead_info *sai    = ll_sai_get(plli->lli_sai);
1047 	struct ptlrpc_thread     *thread = &sai->sai_thread;
1048 	struct ptlrpc_thread *agl_thread = &sai->sai_agl_thread;
1049 	struct page	      *page;
1050 	__u64		     pos    = 0;
1051 	int		       first  = 0;
1052 	int		       rc     = 0;
1053 	struct ll_dir_chain       chain;
1054 	struct l_wait_info	lwi    = { 0 };
1055 
1056 	thread->t_pid = current_pid();
1057 	CDEBUG(D_READA, "statahead thread starting: sai %p, parent %pd\n",
1058 	       sai, parent);
1059 
1060 	if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
1061 		ll_start_agl(parent, sai);
1062 
1063 	atomic_inc(&sbi->ll_sa_total);
1064 	spin_lock(&plli->lli_sa_lock);
1065 	if (thread_is_init(thread))
1066 		/* If someone else has changed the thread state
1067 		 * (e.g. already changed to SVC_STOPPING), we can't just
1068 		 * blindly overwrite that setting. */
1069 		thread_set_flags(thread, SVC_RUNNING);
1070 	spin_unlock(&plli->lli_sa_lock);
1071 	wake_up(&thread->t_ctl_waitq);
1072 
1073 	ll_dir_chain_init(&chain);
1074 	page = ll_get_dir_page(dir, pos, &chain);
1075 
1076 	while (1) {
1077 		struct lu_dirpage *dp;
1078 		struct lu_dirent  *ent;
1079 
1080 		if (IS_ERR(page)) {
1081 			rc = PTR_ERR(page);
1082 			CDEBUG(D_READA, "error reading dir "DFID" at %llu/%llu: [rc %d] [parent %u]\n",
1083 			       PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1084 			       rc, plli->lli_opendir_pid);
1085 			goto out;
1086 		}
1087 
1088 		dp = page_address(page);
1089 		for (ent = lu_dirent_start(dp); ent != NULL;
1090 		     ent = lu_dirent_next(ent)) {
1091 			__u64 hash;
1092 			int namelen;
1093 			char *name;
1094 
1095 			hash = le64_to_cpu(ent->lde_hash);
1096 			if (unlikely(hash < pos))
1097 				/*
1098 				 * Skip until we find target hash value.
1099 				 */
1100 				continue;
1101 
1102 			namelen = le16_to_cpu(ent->lde_namelen);
1103 			if (unlikely(namelen == 0))
1104 				/*
1105 				 * Skip dummy record.
1106 				 */
1107 				continue;
1108 
1109 			name = ent->lde_name;
1110 			if (name[0] == '.') {
1111 				if (namelen == 1) {
1112 					/*
1113 					 * skip "."
1114 					 */
1115 					continue;
1116 				} else if (name[1] == '.' && namelen == 2) {
1117 					/*
1118 					 * skip ".."
1119 					 */
1120 					continue;
1121 				} else if (!sai->sai_ls_all) {
1122 					/*
1123 					 * skip hidden files.
1124 					 */
1125 					sai->sai_skip_hidden++;
1126 					continue;
1127 				}
1128 			}
1129 
1130 			/*
1131 			 * don't stat-ahead first entry.
1132 			 */
1133 			if (unlikely(++first == 1))
1134 				continue;
1135 
1136 keep_it:
1137 			l_wait_event(thread->t_ctl_waitq,
1138 				     !sa_sent_full(sai) ||
1139 				     !sa_received_empty(sai) ||
1140 				     !agl_list_empty(sai) ||
1141 				     !thread_is_running(thread),
1142 				     &lwi);
1143 
1144 interpret_it:
1145 			while (!sa_received_empty(sai))
1146 				ll_post_statahead(sai);
1147 
1148 			if (unlikely(!thread_is_running(thread))) {
1149 				ll_release_page(page, 0);
1150 				rc = 0;
1151 				goto out;
1152 			}
1153 
1154 			/* If no window for metadata statahead, but there are
1155 			 * some AGL entries to be triggered, then try to help
1156 			 * to process the AGL entries. */
1157 			if (sa_sent_full(sai)) {
1158 				spin_lock(&plli->lli_agl_lock);
1159 				while (!agl_list_empty(sai)) {
1160 					clli = agl_first_entry(sai);
1161 					list_del_init(&clli->lli_agl_list);
1162 					spin_unlock(&plli->lli_agl_lock);
1163 					ll_agl_trigger(&clli->lli_vfs_inode,
1164 						       sai);
1165 
1166 					if (!sa_received_empty(sai))
1167 						goto interpret_it;
1168 
1169 					if (unlikely(
1170 						!thread_is_running(thread))) {
1171 						ll_release_page(page, 0);
1172 						rc = 0;
1173 						goto out;
1174 					}
1175 
1176 					if (!sa_sent_full(sai))
1177 						goto do_it;
1178 
1179 					spin_lock(&plli->lli_agl_lock);
1180 				}
1181 				spin_unlock(&plli->lli_agl_lock);
1182 
1183 				goto keep_it;
1184 			}
1185 
1186 do_it:
1187 			ll_statahead_one(parent, name, namelen);
1188 		}
1189 		pos = le64_to_cpu(dp->ldp_hash_end);
1190 		if (pos == MDS_DIR_END_OFF) {
1191 			/*
1192 			 * End of directory reached.
1193 			 */
1194 			ll_release_page(page, 0);
1195 			while (1) {
1196 				l_wait_event(thread->t_ctl_waitq,
1197 					     !sa_received_empty(sai) ||
1198 					     sai->sai_sent == sai->sai_replied ||
1199 					     !thread_is_running(thread),
1200 					     &lwi);
1201 
1202 				while (!sa_received_empty(sai))
1203 					ll_post_statahead(sai);
1204 
1205 				if (unlikely(!thread_is_running(thread))) {
1206 					rc = 0;
1207 					goto out;
1208 				}
1209 
1210 				if (sai->sai_sent == sai->sai_replied &&
1211 				    sa_received_empty(sai))
1212 					break;
1213 			}
1214 
1215 			spin_lock(&plli->lli_agl_lock);
1216 			while (!agl_list_empty(sai) &&
1217 			       thread_is_running(thread)) {
1218 				clli = agl_first_entry(sai);
1219 				list_del_init(&clli->lli_agl_list);
1220 				spin_unlock(&plli->lli_agl_lock);
1221 				ll_agl_trigger(&clli->lli_vfs_inode, sai);
1222 				spin_lock(&plli->lli_agl_lock);
1223 			}
1224 			spin_unlock(&plli->lli_agl_lock);
1225 
1226 			rc = 0;
1227 			goto out;
1228 		} else if (1) {
1229 			/*
1230 			 * chain is exhausted.
1231 			 * Normal case: continue to the next page.
1232 			 */
1233 			ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1234 					      LDF_COLLIDE);
1235 			page = ll_get_dir_page(dir, pos, &chain);
1236 		} else {
1237 			LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1238 			ll_release_page(page, 1);
1239 			/*
1240 			 * go into overflow page.
1241 			 */
1242 		}
1243 	}
1244 
1245 out:
1246 	if (sai->sai_agl_valid) {
1247 		spin_lock(&plli->lli_agl_lock);
1248 		thread_set_flags(agl_thread, SVC_STOPPING);
1249 		spin_unlock(&plli->lli_agl_lock);
1250 		wake_up(&agl_thread->t_ctl_waitq);
1251 
1252 		CDEBUG(D_READA, "stop agl thread: sai %p pid %u\n",
1253 		       sai, (unsigned int)agl_thread->t_pid);
1254 		l_wait_event(agl_thread->t_ctl_waitq,
1255 			     thread_is_stopped(agl_thread),
1256 			     &lwi);
1257 	} else {
1258 		/* Set agl_thread flags anyway. */
1259 		thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1260 	}
1261 	ll_dir_chain_fini(&chain);
1262 	spin_lock(&plli->lli_sa_lock);
1263 	if (!sa_received_empty(sai)) {
1264 		thread_set_flags(thread, SVC_STOPPING);
1265 		spin_unlock(&plli->lli_sa_lock);
1266 
1267 		/* To release the resources held by received entries. */
1268 		while (!sa_received_empty(sai))
1269 			ll_post_statahead(sai);
1270 
1271 		spin_lock(&plli->lli_sa_lock);
1272 	}
1273 	thread_set_flags(thread, SVC_STOPPED);
1274 	spin_unlock(&plli->lli_sa_lock);
1275 	wake_up(&sai->sai_waitq);
1276 	wake_up(&thread->t_ctl_waitq);
1277 	ll_sai_put(sai);
1278 	dput(parent);
1279 	CDEBUG(D_READA, "statahead thread stopped: sai %p, parent %pd\n",
1280 	       sai, parent);
1281 	return rc;
1282 }
1283 
1284 /**
1285  * called in ll_file_release().
1286  */
ll_stop_statahead(struct inode * dir,void * key)1287 void ll_stop_statahead(struct inode *dir, void *key)
1288 {
1289 	struct ll_inode_info *lli = ll_i2info(dir);
1290 
1291 	if (unlikely(key == NULL))
1292 		return;
1293 
1294 	spin_lock(&lli->lli_sa_lock);
1295 	if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
1296 		spin_unlock(&lli->lli_sa_lock);
1297 		return;
1298 	}
1299 
1300 	lli->lli_opendir_key = NULL;
1301 
1302 	if (lli->lli_sai) {
1303 		struct l_wait_info lwi = { 0 };
1304 		struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
1305 
1306 		if (!thread_is_stopped(thread)) {
1307 			thread_set_flags(thread, SVC_STOPPING);
1308 			spin_unlock(&lli->lli_sa_lock);
1309 			wake_up(&thread->t_ctl_waitq);
1310 
1311 			CDEBUG(D_READA, "stop statahead thread: sai %p pid %u\n",
1312 			       lli->lli_sai, (unsigned int)thread->t_pid);
1313 			l_wait_event(thread->t_ctl_waitq,
1314 				     thread_is_stopped(thread),
1315 				     &lwi);
1316 		} else {
1317 			spin_unlock(&lli->lli_sa_lock);
1318 		}
1319 
1320 		/*
1321 		 * Put the ref which was held when first statahead_enter.
1322 		 * It maybe not the last ref for some statahead requests
1323 		 * maybe inflight.
1324 		 */
1325 		ll_sai_put(lli->lli_sai);
1326 	} else {
1327 		lli->lli_opendir_pid = 0;
1328 		spin_unlock(&lli->lli_sa_lock);
1329 	}
1330 }
1331 
1332 enum {
1333 	/**
1334 	 * not first dirent, or is "."
1335 	 */
1336 	LS_NONE_FIRST_DE = 0,
1337 	/**
1338 	 * the first non-hidden dirent
1339 	 */
1340 	LS_FIRST_DE,
1341 	/**
1342 	 * the first hidden dirent, that is "."
1343 	 */
1344 	LS_FIRST_DOT_DE
1345 };
1346 
is_first_dirent(struct inode * dir,struct dentry * dentry)1347 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1348 {
1349 	struct ll_dir_chain   chain;
1350 	struct qstr	  *target = &dentry->d_name;
1351 	struct page	  *page;
1352 	__u64		 pos    = 0;
1353 	int		   dot_de;
1354 	int		   rc     = LS_NONE_FIRST_DE;
1355 
1356 	ll_dir_chain_init(&chain);
1357 	page = ll_get_dir_page(dir, pos, &chain);
1358 
1359 	while (1) {
1360 		struct lu_dirpage *dp;
1361 		struct lu_dirent  *ent;
1362 
1363 		if (IS_ERR(page)) {
1364 			struct ll_inode_info *lli = ll_i2info(dir);
1365 
1366 			rc = PTR_ERR(page);
1367 			CERROR("error reading dir "DFID" at %llu: [rc %d] [parent %u]\n",
1368 			       PFID(ll_inode2fid(dir)), pos,
1369 			       rc, lli->lli_opendir_pid);
1370 			break;
1371 		}
1372 
1373 		dp = page_address(page);
1374 		for (ent = lu_dirent_start(dp); ent != NULL;
1375 		     ent = lu_dirent_next(ent)) {
1376 			__u64 hash;
1377 			int namelen;
1378 			char *name;
1379 
1380 			hash = le64_to_cpu(ent->lde_hash);
1381 			/* The ll_get_dir_page() can return any page containing
1382 			 * the given hash which may be not the start hash. */
1383 			if (unlikely(hash < pos))
1384 				continue;
1385 
1386 			namelen = le16_to_cpu(ent->lde_namelen);
1387 			if (unlikely(namelen == 0))
1388 				/*
1389 				 * skip dummy record.
1390 				 */
1391 				continue;
1392 
1393 			name = ent->lde_name;
1394 			if (name[0] == '.') {
1395 				if (namelen == 1)
1396 					/*
1397 					 * skip "."
1398 					 */
1399 					continue;
1400 				else if (name[1] == '.' && namelen == 2)
1401 					/*
1402 					 * skip ".."
1403 					 */
1404 					continue;
1405 				else
1406 					dot_de = 1;
1407 			} else {
1408 				dot_de = 0;
1409 			}
1410 
1411 			if (dot_de && target->name[0] != '.') {
1412 				CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1413 				       target->len, target->name,
1414 				       namelen, name);
1415 				continue;
1416 			}
1417 
1418 			if (target->len != namelen ||
1419 			    memcmp(target->name, name, namelen) != 0)
1420 				rc = LS_NONE_FIRST_DE;
1421 			else if (!dot_de)
1422 				rc = LS_FIRST_DE;
1423 			else
1424 				rc = LS_FIRST_DOT_DE;
1425 
1426 			ll_release_page(page, 0);
1427 			goto out;
1428 		}
1429 		pos = le64_to_cpu(dp->ldp_hash_end);
1430 		if (pos == MDS_DIR_END_OFF) {
1431 			/*
1432 			 * End of directory reached.
1433 			 */
1434 			ll_release_page(page, 0);
1435 			break;
1436 		} else if (1) {
1437 			/*
1438 			 * chain is exhausted
1439 			 * Normal case: continue to the next page.
1440 			 */
1441 			ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1442 					      LDF_COLLIDE);
1443 			page = ll_get_dir_page(dir, pos, &chain);
1444 		} else {
1445 			/*
1446 			 * go into overflow page.
1447 			 */
1448 			LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1449 			ll_release_page(page, 1);
1450 		}
1451 	}
1452 
1453 out:
1454 	ll_dir_chain_fini(&chain);
1455 	return rc;
1456 }
1457 
1458 static void
ll_sai_unplug(struct ll_statahead_info * sai,struct ll_sa_entry * entry)1459 ll_sai_unplug(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
1460 {
1461 	struct ptlrpc_thread *thread = &sai->sai_thread;
1462 	struct ll_sb_info    *sbi    = ll_i2sbi(sai->sai_inode);
1463 	int		   hit;
1464 
1465 	if (entry != NULL && entry->se_stat == SA_ENTRY_SUCC)
1466 		hit = 1;
1467 	else
1468 		hit = 0;
1469 
1470 	ll_sa_entry_fini(sai, entry);
1471 	if (hit) {
1472 		sai->sai_hit++;
1473 		sai->sai_consecutive_miss = 0;
1474 		sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1475 	} else {
1476 		struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
1477 
1478 		sai->sai_miss++;
1479 		sai->sai_consecutive_miss++;
1480 		if (sa_low_hit(sai) && thread_is_running(thread)) {
1481 			atomic_inc(&sbi->ll_sa_wrong);
1482 			CDEBUG(D_READA, "Statahead for dir " DFID " hit ratio too low: hit/miss %llu/%llu, sent/replied %llu/%llu, stopping statahead thread\n",
1483 			       PFID(&lli->lli_fid), sai->sai_hit,
1484 			       sai->sai_miss, sai->sai_sent,
1485 			       sai->sai_replied);
1486 			spin_lock(&lli->lli_sa_lock);
1487 			if (!thread_is_stopped(thread))
1488 				thread_set_flags(thread, SVC_STOPPING);
1489 			spin_unlock(&lli->lli_sa_lock);
1490 		}
1491 	}
1492 
1493 	if (!thread_is_stopped(thread))
1494 		wake_up(&thread->t_ctl_waitq);
1495 }
1496 
1497 /**
1498  * Start statahead thread if this is the first dir entry.
1499  * Otherwise if a thread is started already, wait it until it is ahead of me.
1500  * \retval 1       -- find entry with lock in cache, the caller needs to do
1501  *		    nothing.
1502  * \retval 0       -- find entry in cache, but without lock, the caller needs
1503  *		    refresh from MDS.
1504  * \retval others  -- the caller need to process as non-statahead.
1505  */
do_statahead_enter(struct inode * dir,struct dentry ** dentryp,int only_unplug)1506 int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1507 		       int only_unplug)
1508 {
1509 	struct ll_inode_info     *lli   = ll_i2info(dir);
1510 	struct ll_statahead_info *sai   = lli->lli_sai;
1511 	struct dentry	    *parent;
1512 	struct ll_sa_entry       *entry;
1513 	struct ptlrpc_thread     *thread;
1514 	struct l_wait_info	lwi   = { 0 };
1515 	int		       rc    = 0;
1516 	struct ll_inode_info     *plli;
1517 
1518 	LASSERT(lli->lli_opendir_pid == current_pid());
1519 
1520 	if (sai) {
1521 		thread = &sai->sai_thread;
1522 		if (unlikely(thread_is_stopped(thread) &&
1523 			     list_empty(&sai->sai_entries_stated))) {
1524 			/* to release resource */
1525 			ll_stop_statahead(dir, lli->lli_opendir_key);
1526 			return -EAGAIN;
1527 		}
1528 
1529 		if ((*dentryp)->d_name.name[0] == '.') {
1530 			if (sai->sai_ls_all ||
1531 			    sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1532 				/*
1533 				 * Hidden dentry is the first one, or statahead
1534 				 * thread does not skip so many hidden dentries
1535 				 * before "sai_ls_all" enabled as below.
1536 				 */
1537 			} else {
1538 				if (!sai->sai_ls_all)
1539 					/*
1540 					 * It maybe because hidden dentry is not
1541 					 * the first one, "sai_ls_all" was not
1542 					 * set, then "ls -al" missed. Enable
1543 					 * "sai_ls_all" for such case.
1544 					 */
1545 					sai->sai_ls_all = 1;
1546 
1547 				/*
1548 				 * Such "getattr" has been skipped before
1549 				 * "sai_ls_all" enabled as above.
1550 				 */
1551 				sai->sai_miss_hidden++;
1552 				return -EAGAIN;
1553 			}
1554 		}
1555 
1556 		entry = ll_sa_entry_get_byname(sai, &(*dentryp)->d_name);
1557 		if (entry == NULL || only_unplug) {
1558 			ll_sai_unplug(sai, entry);
1559 			return entry ? 1 : -EAGAIN;
1560 		}
1561 
1562 		if (!ll_sa_entry_stated(entry)) {
1563 			sai->sai_index_wait = entry->se_index;
1564 			lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1565 					       LWI_ON_SIGNAL_NOOP, NULL);
1566 			rc = l_wait_event(sai->sai_waitq,
1567 					  ll_sa_entry_stated(entry) ||
1568 					  thread_is_stopped(thread),
1569 					  &lwi);
1570 			if (rc < 0) {
1571 				ll_sai_unplug(sai, entry);
1572 				return -EAGAIN;
1573 			}
1574 		}
1575 
1576 		if (entry->se_stat == SA_ENTRY_SUCC &&
1577 		    entry->se_inode != NULL) {
1578 			struct inode *inode = entry->se_inode;
1579 			struct lookup_intent it = { .it_op = IT_GETATTR,
1580 						    .d.lustre.it_lock_handle =
1581 						     entry->se_handle };
1582 			__u64 bits;
1583 
1584 			rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1585 						ll_inode2fid(inode), &bits);
1586 			if (rc == 1) {
1587 				if (d_inode(*dentryp) == NULL) {
1588 					struct dentry *alias;
1589 
1590 					alias = ll_splice_alias(inode,
1591 								   *dentryp);
1592 					if (IS_ERR(alias)) {
1593 						ll_sai_unplug(sai, entry);
1594 						return PTR_ERR(alias);
1595 					}
1596 					*dentryp = alias;
1597 				} else if (d_inode(*dentryp) != inode) {
1598 					/* revalidate, but inode is recreated */
1599 					CDEBUG(D_READA,
1600 					      "stale dentry %pd inode %lu/%u, statahead inode %lu/%u\n",
1601 					      *dentryp,
1602 					      d_inode(*dentryp)->i_ino,
1603 					      d_inode(*dentryp)->i_generation,
1604 					      inode->i_ino,
1605 					      inode->i_generation);
1606 					ll_sai_unplug(sai, entry);
1607 					return -ESTALE;
1608 				} else {
1609 					iput(inode);
1610 				}
1611 				entry->se_inode = NULL;
1612 
1613 				if ((bits & MDS_INODELOCK_LOOKUP) &&
1614 				    d_lustre_invalid(*dentryp))
1615 					d_lustre_revalidate(*dentryp);
1616 				ll_intent_release(&it);
1617 			}
1618 		}
1619 
1620 		ll_sai_unplug(sai, entry);
1621 		return rc;
1622 	}
1623 
1624 	/* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1625 	rc = is_first_dirent(dir, *dentryp);
1626 	if (rc == LS_NONE_FIRST_DE) {
1627 		/* It is not "ls -{a}l" operation, no need statahead for it. */
1628 		rc = -EAGAIN;
1629 		goto out;
1630 	}
1631 
1632 	sai = ll_sai_alloc();
1633 	if (sai == NULL) {
1634 		rc = -ENOMEM;
1635 		goto out;
1636 	}
1637 
1638 	sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1639 	sai->sai_inode = igrab(dir);
1640 	if (unlikely(sai->sai_inode == NULL)) {
1641 		CWARN("Do not start stat ahead on dying inode "DFID"\n",
1642 		      PFID(&lli->lli_fid));
1643 		rc = -ESTALE;
1644 		goto out;
1645 	}
1646 
1647 	/* get parent reference count here, and put it in ll_statahead_thread */
1648 	parent = dget((*dentryp)->d_parent);
1649 	if (unlikely(sai->sai_inode != d_inode(parent))) {
1650 		struct ll_inode_info *nlli = ll_i2info(d_inode(parent));
1651 
1652 		CWARN("Race condition, someone changed %pd just now: old parent "DFID", new parent "DFID"\n",
1653 		      *dentryp,
1654 		      PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1655 		dput(parent);
1656 		iput(sai->sai_inode);
1657 		rc = -EAGAIN;
1658 		goto out;
1659 	}
1660 
1661 	CDEBUG(D_READA, "start statahead thread: sai %p, parent %pd\n",
1662 	       sai, parent);
1663 
1664 	/* The sai buffer already has one reference taken at allocation time,
1665 	 * but as soon as we expose the sai by attaching it to the lli that
1666 	 * default reference can be dropped by another thread calling
1667 	 * ll_stop_statahead. We need to take a local reference to protect
1668 	 * the sai buffer while we intend to access it. */
1669 	ll_sai_get(sai);
1670 	lli->lli_sai = sai;
1671 
1672 	plli = ll_i2info(d_inode(parent));
1673 	rc = PTR_ERR(kthread_run(ll_statahead_thread, parent,
1674 				 "ll_sa_%u", plli->lli_opendir_pid));
1675 	thread = &sai->sai_thread;
1676 	if (IS_ERR_VALUE(rc)) {
1677 		CERROR("can't start ll_sa thread, rc: %d\n", rc);
1678 		dput(parent);
1679 		lli->lli_opendir_key = NULL;
1680 		thread_set_flags(thread, SVC_STOPPED);
1681 		thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1682 		/* Drop both our own local reference and the default
1683 		 * reference from allocation time. */
1684 		ll_sai_put(sai);
1685 		ll_sai_put(sai);
1686 		LASSERT(lli->lli_sai == NULL);
1687 		return -EAGAIN;
1688 	}
1689 
1690 	l_wait_event(thread->t_ctl_waitq,
1691 		     thread_is_running(thread) || thread_is_stopped(thread),
1692 		     &lwi);
1693 	ll_sai_put(sai);
1694 
1695 	/*
1696 	 * We don't stat-ahead for the first dirent since we are already in
1697 	 * lookup.
1698 	 */
1699 	return -EAGAIN;
1700 
1701 out:
1702 	kfree(sai);
1703 	spin_lock(&lli->lli_sa_lock);
1704 	lli->lli_opendir_key = NULL;
1705 	lli->lli_opendir_pid = 0;
1706 	spin_unlock(&lli->lli_sa_lock);
1707 	return rc;
1708 }
1709