Whamcloud - gitweb
LU-13030 pcc: Init saved dataset flags properly
[fs/lustre-release.git] / lustre / llite / pcc.c
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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2017, DDN Storage Corporation.
24  */
25 /*
26  * Persistent Client Cache
27  *
28  * PCC is a new framework which provides a group of local cache on Lustre
29  * client side. It works in two modes: RW-PCC enables a read-write cache on the
30  * local SSDs of a single client; RO-PCC provides a read-only cache on the
31  * local SSDs of multiple clients. Less overhead is visible to the applications
32  * and network latencies and lock conflicts can be significantly reduced.
33  *
34  * For RW-PCC, no global namespace will be provided. Each client uses its own
35  * local storage as a cache for itself. Local file system is used to manage
36  * the data on local caches. Cached I/O is directed to local file system while
37  * normal I/O is directed to OSTs. RW-PCC uses HSM for data synchronization.
38  * It uses HSM copytool to restore file from local caches to Lustre OSTs. Each
39  * PCC has a copytool instance running with unique archive number. Any remote
40  * access from another Lustre client would trigger the data synchronization. If
41  * a client with RW-PCC goes offline, the cached data becomes inaccessible for
42  * other client temporarily. And after the RW-PCC client reboots and the
43  * copytool restarts, the data will be accessible again.
44  *
45  * Following is what will happen in different conditions for RW-PCC:
46  *
47  * > When file is being created on RW-PCC
48  *
49  * A normal HSM released file is created on MDT;
50  * An empty mirror file is created on local cache;
51  * The HSM status of the Lustre file will be set to archived and released;
52  * The archive number will be set to the proper value.
53  *
54  * > When file is being prefetched to RW-PCC
55  *
56  * An file is copied to the local cache;
57  * The HSM status of the Lustre file will be set to archived and released;
58  * The archive number will be set to the proper value.
59  *
60  * > When file is being accessed from PCC
61  *
62  * Data will be read directly from local cache;
63  * Metadata will be read from MDT, except file size;
64  * File size will be got from local cache.
65  *
66  * > When PCC cached file is being accessed on another client
67  *
68  * RW-PCC cached files are automatically restored when a process on another
69  * client tries to read or modify them. The corresponding I/O will block
70  * waiting for the released file to be restored. This is transparent to the
71  * process.
72  *
73  * For RW-PCC, when a file is being created, a rule-based policy is used to
74  * determine whether it will be cached. Rule-based caching of newly created
75  * files can determine which file can use a cache on PCC directly without any
76  * admission control.
77  *
78  * RW-PCC design can accelerate I/O intensive applications with one-to-one
79  * mappings between files and accessing clients. However, in several use cases,
80  * files will never be updated, but need to be read simultaneously from many
81  * clients. RO-PCC implements a read-only caching on Lustre clients using
82  * SSDs. RO-PCC is based on the same framework as RW-PCC, expect
83  * that no HSM mechanism is used.
84  *
85  * The main advantages to use this SSD cache on the Lustre clients via PCC
86  * is that:
87  * - The I/O stack becomes much simpler for the cached data, as there is no
88  *   interference with I/Os from other clients, which enables easier
89  *   performance optimizations;
90  * - The requirements on the HW inside the client nodes are small, any kind of
91  *   SSDs or even HDDs can be used as cache devices;
92  * - Caching reduces the pressure on the object storage targets (OSTs), as
93  *   small or random I/Os can be regularized to big sequential I/Os and
94  *   temporary files do not even need to be flushed to OSTs.
95  *
96  * PCC can accelerate applications with certain I/O patterns:
97  * - small-sized random writes (< 1MB) from a single client
98  * - repeated read of data that is larger than RAM
99  * - clients with high network latency
100  *
101  * Author: Li Xi <lixi@ddn.com>
102  * Author: Qian Yingjin <qian@ddn.com>
103  */
104
105 #define DEBUG_SUBSYSTEM S_LLITE
106
107 #include "pcc.h"
108 #include <linux/namei.h>
109 #include <linux/file.h>
110 #include <lustre_compat.h>
111 #include "llite_internal.h"
112
113 struct kmem_cache *pcc_inode_slab;
114
115 int pcc_super_init(struct pcc_super *super)
116 {
117         struct cred *cred;
118
119         super->pccs_cred = cred = prepare_creds();
120         if (!cred)
121                 return -ENOMEM;
122
123         /* Never override disk quota limits or use reserved space */
124         cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
125         init_rwsem(&super->pccs_rw_sem);
126         INIT_LIST_HEAD(&super->pccs_datasets);
127         super->pccs_generation = 1;
128
129         return 0;
130 }
131
132 /* Rule based auto caching */
133 static void pcc_id_list_free(struct list_head *id_list)
134 {
135         struct pcc_match_id *id, *n;
136
137         list_for_each_entry_safe(id, n, id_list, pmi_linkage) {
138                 list_del_init(&id->pmi_linkage);
139                 OBD_FREE_PTR(id);
140         }
141 }
142
143 static void pcc_fname_list_free(struct list_head *fname_list)
144 {
145         struct pcc_match_fname *fname, *n;
146
147         list_for_each_entry_safe(fname, n, fname_list, pmf_linkage) {
148                 OBD_FREE(fname->pmf_name, strlen(fname->pmf_name) + 1);
149                 list_del_init(&fname->pmf_linkage);
150                 OBD_FREE_PTR(fname);
151         }
152 }
153
154 static void pcc_expression_free(struct pcc_expression *expr)
155 {
156         LASSERT(expr->pe_field >= PCC_FIELD_UID &&
157                 expr->pe_field < PCC_FIELD_MAX);
158         switch (expr->pe_field) {
159         case PCC_FIELD_UID:
160         case PCC_FIELD_GID:
161         case PCC_FIELD_PROJID:
162                 pcc_id_list_free(&expr->pe_cond);
163                 break;
164         case PCC_FIELD_FNAME:
165                 pcc_fname_list_free(&expr->pe_cond);
166                 break;
167         default:
168                 LBUG();
169         }
170         OBD_FREE_PTR(expr);
171 }
172
173 static void pcc_conjunction_free(struct pcc_conjunction *conjunction)
174 {
175         struct pcc_expression *expression, *n;
176
177         LASSERT(list_empty(&conjunction->pc_linkage));
178         list_for_each_entry_safe(expression, n,
179                                  &conjunction->pc_expressions,
180                                  pe_linkage) {
181                 list_del_init(&expression->pe_linkage);
182                 pcc_expression_free(expression);
183         }
184         OBD_FREE_PTR(conjunction);
185 }
186
187 static void pcc_rule_conds_free(struct list_head *cond_list)
188 {
189         struct pcc_conjunction *conjunction, *n;
190
191         list_for_each_entry_safe(conjunction, n, cond_list, pc_linkage) {
192                 list_del_init(&conjunction->pc_linkage);
193                 pcc_conjunction_free(conjunction);
194         }
195 }
196
197 static void pcc_cmd_fini(struct pcc_cmd *cmd)
198 {
199         if (cmd->pccc_cmd == PCC_ADD_DATASET) {
200                 if (!list_empty(&cmd->u.pccc_add.pccc_conds))
201                         pcc_rule_conds_free(&cmd->u.pccc_add.pccc_conds);
202                 if (cmd->u.pccc_add.pccc_conds_str)
203                         OBD_FREE(cmd->u.pccc_add.pccc_conds_str,
204                                  strlen(cmd->u.pccc_add.pccc_conds_str) + 1);
205         }
206 }
207
208 #define PCC_DISJUNCTION_DELIM   (',')
209 #define PCC_CONJUNCTION_DELIM   ('&')
210 #define PCC_EXPRESSION_DELIM    ('=')
211
212 static int
213 pcc_fname_list_add(struct cfs_lstr *id, struct list_head *fname_list)
214 {
215         struct pcc_match_fname *fname;
216
217         OBD_ALLOC(fname, sizeof(struct pcc_match_fname));
218         if (fname == NULL)
219                 return -ENOMEM;
220
221         OBD_ALLOC(fname->pmf_name, id->ls_len + 1);
222         if (fname->pmf_name == NULL) {
223                 OBD_FREE(fname, sizeof(struct pcc_match_fname));
224                 return -ENOMEM;
225         }
226
227         memcpy(fname->pmf_name, id->ls_str, id->ls_len);
228         list_add_tail(&fname->pmf_linkage, fname_list);
229         return 0;
230 }
231
232 static int
233 pcc_fname_list_parse(char *str, int len, struct list_head *fname_list)
234 {
235         struct cfs_lstr src;
236         struct cfs_lstr res;
237         int rc = 0;
238
239         ENTRY;
240
241         src.ls_str = str;
242         src.ls_len = len;
243         INIT_LIST_HEAD(fname_list);
244         while (src.ls_str) {
245                 rc = cfs_gettok(&src, ' ', &res);
246                 if (rc == 0) {
247                         rc = -EINVAL;
248                         break;
249                 }
250                 rc = pcc_fname_list_add(&res, fname_list);
251                 if (rc)
252                         break;
253         }
254         if (rc)
255                 pcc_fname_list_free(fname_list);
256         RETURN(rc);
257 }
258
259 static int
260 pcc_id_list_parse(char *str, int len, struct list_head *id_list,
261                   enum pcc_field type)
262 {
263         struct cfs_lstr src;
264         struct cfs_lstr res;
265         int rc = 0;
266
267         ENTRY;
268
269         if (type != PCC_FIELD_UID && type != PCC_FIELD_GID &&
270             type != PCC_FIELD_PROJID)
271                 RETURN(-EINVAL);
272
273         src.ls_str = str;
274         src.ls_len = len;
275         INIT_LIST_HEAD(id_list);
276         while (src.ls_str) {
277                 struct pcc_match_id *id;
278                 __u32 id_val;
279
280                 if (cfs_gettok(&src, ' ', &res) == 0)
281                         GOTO(out, rc = -EINVAL);
282
283                 if (!cfs_str2num_check(res.ls_str, res.ls_len,
284                                        &id_val, 0, (u32)~0U))
285                         GOTO(out, rc = -EINVAL);
286
287                 OBD_ALLOC_PTR(id);
288                 if (id == NULL)
289                         GOTO(out, rc = -ENOMEM);
290
291                 id->pmi_id = id_val;
292                 list_add_tail(&id->pmi_linkage, id_list);
293         }
294 out:
295         if (rc)
296                 pcc_id_list_free(id_list);
297         RETURN(rc);
298 }
299
300 static inline bool
301 pcc_check_field(struct cfs_lstr *field, char *str)
302 {
303         int len = strlen(str);
304
305         return (field->ls_len == len &&
306                 strncmp(field->ls_str, str, len) == 0);
307 }
308
309 static int
310 pcc_expression_parse(struct cfs_lstr *src, struct list_head *cond_list)
311 {
312         struct pcc_expression *expr;
313         struct cfs_lstr field;
314         int rc = 0;
315
316         OBD_ALLOC(expr, sizeof(struct pcc_expression));
317         if (expr == NULL)
318                 return -ENOMEM;
319
320         rc = cfs_gettok(src, PCC_EXPRESSION_DELIM, &field);
321         if (rc == 0 || src->ls_len <= 2 || src->ls_str[0] != '{' ||
322             src->ls_str[src->ls_len - 1] != '}')
323                 GOTO(out, rc = -EINVAL);
324
325         /* Skip '{' and '}' */
326         src->ls_str++;
327         src->ls_len -= 2;
328
329         if (pcc_check_field(&field, "uid")) {
330                 if (pcc_id_list_parse(src->ls_str,
331                                       src->ls_len,
332                                       &expr->pe_cond,
333                                       PCC_FIELD_UID) < 0)
334                         GOTO(out, rc = -EINVAL);
335                 expr->pe_field = PCC_FIELD_UID;
336         } else if (pcc_check_field(&field, "gid")) {
337                 if (pcc_id_list_parse(src->ls_str,
338                                       src->ls_len,
339                                       &expr->pe_cond,
340                                       PCC_FIELD_GID) < 0)
341                         GOTO(out, rc = -EINVAL);
342                 expr->pe_field = PCC_FIELD_GID;
343         } else if (pcc_check_field(&field, "projid")) {
344                 if (pcc_id_list_parse(src->ls_str,
345                                       src->ls_len,
346                                       &expr->pe_cond,
347                                       PCC_FIELD_PROJID) < 0)
348                         GOTO(out, rc = -EINVAL);
349                 expr->pe_field = PCC_FIELD_PROJID;
350         } else if (pcc_check_field(&field, "fname")) {
351                 if (pcc_fname_list_parse(src->ls_str,
352                                          src->ls_len,
353                                          &expr->pe_cond) < 0)
354                         GOTO(out, rc = -EINVAL);
355                 expr->pe_field = PCC_FIELD_FNAME;
356         } else {
357                 GOTO(out, rc = -EINVAL);
358         }
359
360         list_add_tail(&expr->pe_linkage, cond_list);
361         return 0;
362 out:
363         OBD_FREE_PTR(expr);
364         return rc;
365 }
366
367 static int
368 pcc_conjunction_parse(struct cfs_lstr *src, struct list_head *cond_list)
369 {
370         struct pcc_conjunction *conjunction;
371         struct cfs_lstr expr;
372         int rc = 0;
373
374         OBD_ALLOC(conjunction, sizeof(struct pcc_conjunction));
375         if (conjunction == NULL)
376                 return -ENOMEM;
377
378         INIT_LIST_HEAD(&conjunction->pc_expressions);
379         list_add_tail(&conjunction->pc_linkage, cond_list);
380
381         while (src->ls_str) {
382                 rc = cfs_gettok(src, PCC_CONJUNCTION_DELIM, &expr);
383                 if (rc == 0) {
384                         rc = -EINVAL;
385                         break;
386                 }
387                 rc = pcc_expression_parse(&expr,
388                                           &conjunction->pc_expressions);
389                 if (rc)
390                         break;
391         }
392         return rc;
393 }
394
395 static int pcc_conds_parse(char *str, int len, struct list_head *cond_list)
396 {
397         struct cfs_lstr src;
398         struct cfs_lstr res;
399         int rc = 0;
400
401         src.ls_str = str;
402         src.ls_len = len;
403         INIT_LIST_HEAD(cond_list);
404         while (src.ls_str) {
405                 rc = cfs_gettok(&src, PCC_DISJUNCTION_DELIM, &res);
406                 if (rc == 0) {
407                         rc = -EINVAL;
408                         break;
409                 }
410                 rc = pcc_conjunction_parse(&res, cond_list);
411                 if (rc)
412                         break;
413         }
414         return rc;
415 }
416
417 static int pcc_id_parse(struct pcc_cmd *cmd, const char *id)
418 {
419         int rc;
420
421         OBD_ALLOC(cmd->u.pccc_add.pccc_conds_str, strlen(id) + 1);
422         if (cmd->u.pccc_add.pccc_conds_str == NULL)
423                 return -ENOMEM;
424
425         memcpy(cmd->u.pccc_add.pccc_conds_str, id, strlen(id));
426
427         rc = pcc_conds_parse(cmd->u.pccc_add.pccc_conds_str,
428                              strlen(cmd->u.pccc_add.pccc_conds_str),
429                              &cmd->u.pccc_add.pccc_conds);
430         if (rc)
431                 pcc_cmd_fini(cmd);
432
433         return rc;
434 }
435
436 static int
437 pcc_parse_value_pair(struct pcc_cmd *cmd, char *buffer)
438 {
439         char *key, *val;
440         unsigned long id;
441         int rc;
442
443         val = buffer;
444         key = strsep(&val, "=");
445         if (val == NULL || strlen(val) == 0)
446                 return -EINVAL;
447
448         /* Key of the value pair */
449         if (strcmp(key, "rwid") == 0) {
450                 rc = kstrtoul(val, 10, &id);
451                 if (rc)
452                         return rc;
453                 if (id <= 0)
454                         return -EINVAL;
455                 cmd->u.pccc_add.pccc_rwid = id;
456         } else if (strcmp(key, "roid") == 0) {
457                 rc = kstrtoul(val, 10, &id);
458                 if (rc)
459                         return rc;
460                 if (id <= 0)
461                         return -EINVAL;
462                 cmd->u.pccc_add.pccc_roid = id;
463         } else if (strcmp(key, "auto_attach") == 0) {
464                 rc = kstrtoul(val, 10, &id);
465                 if (rc)
466                         return rc;
467                 if (id == 0)
468                         cmd->u.pccc_add.pccc_flags &= ~PCC_DATASET_AUTO_ATTACH;
469         } else if (strcmp(key, "open_attach") == 0) {
470                 rc = kstrtoul(val, 10, &id);
471                 if (rc)
472                         return rc;
473                 if (id == 0)
474                         cmd->u.pccc_add.pccc_flags &= ~PCC_DATASET_OPEN_ATTACH;
475         } else if (strcmp(key, "io_attach") == 0) {
476                 rc = kstrtoul(val, 10, &id);
477                 if (rc)
478                         return rc;
479                 if (id == 0)
480                         cmd->u.pccc_add.pccc_flags &= ~PCC_DATASET_IO_ATTACH;
481         } else if (strcmp(key, "stat_attach") == 0) {
482                 rc = kstrtoul(val, 10, &id);
483                 if (rc)
484                         return rc;
485                 if (id == 0)
486                         cmd->u.pccc_add.pccc_flags &= ~PCC_DATASET_STAT_ATTACH;
487         } else if (strcmp(key, "rwpcc") == 0) {
488                 rc = kstrtoul(val, 10, &id);
489                 if (rc)
490                         return rc;
491                 if (id > 0)
492                         cmd->u.pccc_add.pccc_flags |= PCC_DATASET_RWPCC;
493         } else if (strcmp(key, "ropcc") == 0) {
494                 rc = kstrtoul(val, 10, &id);
495                 if (rc)
496                         return rc;
497                 if (id > 0)
498                         cmd->u.pccc_add.pccc_flags |= PCC_DATASET_ROPCC;
499         } else {
500                 return -EINVAL;
501         }
502
503         return 0;
504 }
505
506 static int
507 pcc_parse_value_pairs(struct pcc_cmd *cmd, char *buffer)
508 {
509         char *val;
510         char *token;
511         int rc;
512
513         switch (cmd->pccc_cmd) {
514         case PCC_ADD_DATASET:
515                 /* Enable auto attach by default */
516                 cmd->u.pccc_add.pccc_flags |= PCC_DATASET_AUTO_ATTACH;
517                 break;
518         case PCC_DEL_DATASET:
519         case PCC_CLEAR_ALL:
520                 break;
521         default:
522                 return -EINVAL;
523         }
524
525         val = buffer;
526         while (val != NULL && strlen(val) != 0) {
527                 token = strsep(&val, " ");
528                 rc = pcc_parse_value_pair(cmd, token);
529                 if (rc)
530                         return rc;
531         }
532
533         switch (cmd->pccc_cmd) {
534         case PCC_ADD_DATASET:
535                 if (cmd->u.pccc_add.pccc_flags & PCC_DATASET_RWPCC &&
536                     cmd->u.pccc_add.pccc_flags & PCC_DATASET_ROPCC)
537                         return -EINVAL;
538                 /*
539                  * By default, a PCC backend can provide caching service for
540                  * both RW-PCC and RO-PCC.
541                  */
542                 if ((cmd->u.pccc_add.pccc_flags & PCC_DATASET_PCC_ALL) == 0)
543                         cmd->u.pccc_add.pccc_flags |= PCC_DATASET_PCC_ALL;
544
545                 /* For RW-PCC, the value of @rwid must be non zero. */
546                 if (cmd->u.pccc_add.pccc_flags & PCC_DATASET_RWPCC &&
547                     cmd->u.pccc_add.pccc_rwid == 0)
548                         return -EINVAL;
549
550                 break;
551         case PCC_DEL_DATASET:
552         case PCC_CLEAR_ALL:
553                 break;
554         default:
555                 return -EINVAL;
556         }
557         return 0;
558 }
559
560 static void
561 pcc_dataset_rule_fini(struct pcc_match_rule *rule)
562 {
563         if (!list_empty(&rule->pmr_conds))
564                 pcc_rule_conds_free(&rule->pmr_conds);
565         LASSERT(rule->pmr_conds_str != NULL);
566         OBD_FREE(rule->pmr_conds_str, strlen(rule->pmr_conds_str) + 1);
567 }
568
569 static int
570 pcc_dataset_rule_init(struct pcc_match_rule *rule, struct pcc_cmd *cmd)
571 {
572         int rc = 0;
573
574         LASSERT(cmd->u.pccc_add.pccc_conds_str);
575         OBD_ALLOC(rule->pmr_conds_str,
576                   strlen(cmd->u.pccc_add.pccc_conds_str) + 1);
577         if (rule->pmr_conds_str == NULL)
578                 return -ENOMEM;
579
580         memcpy(rule->pmr_conds_str,
581                cmd->u.pccc_add.pccc_conds_str,
582                strlen(cmd->u.pccc_add.pccc_conds_str));
583
584         INIT_LIST_HEAD(&rule->pmr_conds);
585         if (!list_empty(&cmd->u.pccc_add.pccc_conds))
586                 rc = pcc_conds_parse(rule->pmr_conds_str,
587                                           strlen(rule->pmr_conds_str),
588                                           &rule->pmr_conds);
589
590         if (rc)
591                 pcc_dataset_rule_fini(rule);
592
593         return rc;
594 }
595
596 /* Rule Matching */
597 static int
598 pcc_id_list_match(struct list_head *id_list, __u32 id_val)
599 {
600         struct pcc_match_id *id;
601
602         list_for_each_entry(id, id_list, pmi_linkage) {
603                 if (id->pmi_id == id_val)
604                         return 1;
605         }
606         return 0;
607 }
608
609 static bool
610 cfs_match_wildcard(const char *pattern, const char *content)
611 {
612         if (*pattern == '\0' && *content == '\0')
613                 return true;
614
615         if (*pattern == '*' && *(pattern + 1) != '\0' && *content == '\0')
616                 return false;
617
618         while (*pattern == *content) {
619                 pattern++;
620                 content++;
621                 if (*pattern == '\0' && *content == '\0')
622                         return true;
623
624                 if (*pattern == '*' && *(pattern + 1) != '\0' &&
625                     *content == '\0')
626                         return false;
627         }
628
629         if (*pattern == '*')
630                 return (cfs_match_wildcard(pattern + 1, content) ||
631                         cfs_match_wildcard(pattern, content + 1));
632
633         return false;
634 }
635
636 static int
637 pcc_fname_list_match(struct list_head *fname_list, const char *name)
638 {
639         struct pcc_match_fname *fname;
640
641         list_for_each_entry(fname, fname_list, pmf_linkage) {
642                 if (cfs_match_wildcard(fname->pmf_name, name))
643                         return 1;
644         }
645         return 0;
646 }
647
648 static int
649 pcc_expression_match(struct pcc_expression *expr, struct pcc_matcher *matcher)
650 {
651         switch (expr->pe_field) {
652         case PCC_FIELD_UID:
653                 return pcc_id_list_match(&expr->pe_cond, matcher->pm_uid);
654         case PCC_FIELD_GID:
655                 return pcc_id_list_match(&expr->pe_cond, matcher->pm_gid);
656         case PCC_FIELD_PROJID:
657                 return pcc_id_list_match(&expr->pe_cond, matcher->pm_projid);
658         case PCC_FIELD_FNAME:
659                 return pcc_fname_list_match(&expr->pe_cond,
660                                             matcher->pm_name->name);
661         default:
662                 return 0;
663         }
664 }
665
666 static int
667 pcc_conjunction_match(struct pcc_conjunction *conjunction,
668                       struct pcc_matcher *matcher)
669 {
670         struct pcc_expression *expr;
671         int matched;
672
673         list_for_each_entry(expr, &conjunction->pc_expressions, pe_linkage) {
674                 matched = pcc_expression_match(expr, matcher);
675                 if (!matched)
676                         return 0;
677         }
678
679         return 1;
680 }
681
682 static int
683 pcc_cond_match(struct pcc_match_rule *rule, struct pcc_matcher *matcher)
684 {
685         struct pcc_conjunction *conjunction;
686         int matched;
687
688         list_for_each_entry(conjunction, &rule->pmr_conds, pc_linkage) {
689                 matched = pcc_conjunction_match(conjunction, matcher);
690                 if (matched)
691                         return 1;
692         }
693
694         return 0;
695 }
696
697 struct pcc_dataset*
698 pcc_dataset_match_get(struct pcc_super *super, struct pcc_matcher *matcher)
699 {
700         struct pcc_dataset *dataset;
701         struct pcc_dataset *selected = NULL;
702
703         down_read(&super->pccs_rw_sem);
704         list_for_each_entry(dataset, &super->pccs_datasets, pccd_linkage) {
705                 if (!(dataset->pccd_flags & PCC_DATASET_RWPCC))
706                         continue;
707
708                 if (pcc_cond_match(&dataset->pccd_rule, matcher)) {
709                         atomic_inc(&dataset->pccd_refcount);
710                         selected = dataset;
711                         break;
712                 }
713         }
714         up_read(&super->pccs_rw_sem);
715         if (selected)
716                 CDEBUG(D_CACHE, "PCC create, matched %s - %d:%d:%d:%s\n",
717                        dataset->pccd_rule.pmr_conds_str,
718                        matcher->pm_uid, matcher->pm_gid,
719                        matcher->pm_projid, matcher->pm_name->name);
720
721         return selected;
722 }
723
724 /**
725  * pcc_dataset_add - Add a Cache policy to control which files need be
726  * cached and where it will be cached.
727  *
728  * @super:      superblock of pcc
729  * @cmd:        pcc command
730  */
731 static int
732 pcc_dataset_add(struct pcc_super *super, struct pcc_cmd *cmd)
733 {
734         char *pathname = cmd->pccc_pathname;
735         struct pcc_dataset *dataset;
736         struct pcc_dataset *tmp;
737         bool found = false;
738         int rc;
739
740         OBD_ALLOC_PTR(dataset);
741         if (dataset == NULL)
742                 return -ENOMEM;
743
744         rc = kern_path(pathname, LOOKUP_DIRECTORY, &dataset->pccd_path);
745         if (unlikely(rc)) {
746                 OBD_FREE_PTR(dataset);
747                 return rc;
748         }
749         strncpy(dataset->pccd_pathname, pathname, PATH_MAX);
750         dataset->pccd_rwid = cmd->u.pccc_add.pccc_rwid;
751         dataset->pccd_roid = cmd->u.pccc_add.pccc_roid;
752         dataset->pccd_flags = cmd->u.pccc_add.pccc_flags;
753         atomic_set(&dataset->pccd_refcount, 1);
754
755         rc = pcc_dataset_rule_init(&dataset->pccd_rule, cmd);
756         if (rc) {
757                 pcc_dataset_put(dataset);
758                 return rc;
759         }
760
761         down_write(&super->pccs_rw_sem);
762         list_for_each_entry(tmp, &super->pccs_datasets, pccd_linkage) {
763                 if (strcmp(tmp->pccd_pathname, pathname) == 0 ||
764                     (dataset->pccd_rwid != 0 &&
765                      dataset->pccd_rwid == tmp->pccd_rwid) ||
766                     (dataset->pccd_roid != 0 &&
767                      dataset->pccd_roid == tmp->pccd_roid)) {
768                         found = true;
769                         break;
770                 }
771         }
772         if (!found)
773                 list_add(&dataset->pccd_linkage, &super->pccs_datasets);
774         up_write(&super->pccs_rw_sem);
775
776         if (found) {
777                 pcc_dataset_put(dataset);
778                 rc = -EEXIST;
779         }
780
781         return rc;
782 }
783
784 struct pcc_dataset *
785 pcc_dataset_get(struct pcc_super *super, enum lu_pcc_type type, __u32 id)
786 {
787         struct pcc_dataset *dataset;
788         struct pcc_dataset *selected = NULL;
789
790         if (id == 0)
791                 return NULL;
792
793         /*
794          * archive ID (read-write ID) or read-only ID is unique in the list,
795          * we just return last added one as first priority.
796          */
797         down_read(&super->pccs_rw_sem);
798         list_for_each_entry(dataset, &super->pccs_datasets, pccd_linkage) {
799                 if (type == LU_PCC_READWRITE && (dataset->pccd_rwid != id ||
800                     !(dataset->pccd_flags & PCC_DATASET_RWPCC)))
801                         continue;
802                 atomic_inc(&dataset->pccd_refcount);
803                 selected = dataset;
804                 break;
805         }
806         up_read(&super->pccs_rw_sem);
807         if (selected)
808                 CDEBUG(D_CACHE, "matched id %u, PCC mode %d\n", id, type);
809
810         return selected;
811 }
812
813 void
814 pcc_dataset_put(struct pcc_dataset *dataset)
815 {
816         if (atomic_dec_and_test(&dataset->pccd_refcount)) {
817                 pcc_dataset_rule_fini(&dataset->pccd_rule);
818                 path_put(&dataset->pccd_path);
819                 OBD_FREE_PTR(dataset);
820         }
821 }
822
823 static int
824 pcc_dataset_del(struct pcc_super *super, char *pathname)
825 {
826         struct list_head *l, *tmp;
827         struct pcc_dataset *dataset;
828         int rc = -ENOENT;
829
830         down_write(&super->pccs_rw_sem);
831         list_for_each_safe(l, tmp, &super->pccs_datasets) {
832                 dataset = list_entry(l, struct pcc_dataset, pccd_linkage);
833                 if (strcmp(dataset->pccd_pathname, pathname) == 0) {
834                         list_del_init(&dataset->pccd_linkage);
835                         pcc_dataset_put(dataset);
836                         super->pccs_generation++;
837                         rc = 0;
838                         break;
839                 }
840         }
841         up_write(&super->pccs_rw_sem);
842         return rc;
843 }
844
845 static void
846 pcc_dataset_dump(struct pcc_dataset *dataset, struct seq_file *m)
847 {
848         seq_printf(m, "%s:\n", dataset->pccd_pathname);
849         seq_printf(m, "  rwid: %u\n", dataset->pccd_rwid);
850         seq_printf(m, "  flags: %x\n", dataset->pccd_flags);
851         seq_printf(m, "  autocache: %s\n", dataset->pccd_rule.pmr_conds_str);
852 }
853
854 int
855 pcc_super_dump(struct pcc_super *super, struct seq_file *m)
856 {
857         struct pcc_dataset *dataset;
858
859         down_read(&super->pccs_rw_sem);
860         list_for_each_entry(dataset, &super->pccs_datasets, pccd_linkage) {
861                 pcc_dataset_dump(dataset, m);
862         }
863         up_read(&super->pccs_rw_sem);
864         return 0;
865 }
866
867 static void pcc_remove_datasets(struct pcc_super *super)
868 {
869         struct pcc_dataset *dataset, *tmp;
870
871         down_write(&super->pccs_rw_sem);
872         list_for_each_entry_safe(dataset, tmp,
873                                  &super->pccs_datasets, pccd_linkage) {
874                 list_del(&dataset->pccd_linkage);
875                 pcc_dataset_put(dataset);
876         }
877         super->pccs_generation++;
878         up_write(&super->pccs_rw_sem);
879 }
880
881 void pcc_super_fini(struct pcc_super *super)
882 {
883         pcc_remove_datasets(super);
884         put_cred(super->pccs_cred);
885 }
886
887 static bool pathname_is_valid(const char *pathname)
888 {
889         /* Needs to be absolute path */
890         if (pathname == NULL || strlen(pathname) == 0 ||
891             strlen(pathname) >= PATH_MAX || pathname[0] != '/')
892                 return false;
893         return true;
894 }
895
896 static struct pcc_cmd *
897 pcc_cmd_parse(char *buffer, unsigned long count)
898 {
899         static struct pcc_cmd *cmd;
900         char *token;
901         char *val;
902         int rc = 0;
903
904         OBD_ALLOC_PTR(cmd);
905         if (cmd == NULL)
906                 GOTO(out, rc = -ENOMEM);
907
908         /* clear all setting */
909         if (strncmp(buffer, "clear", 5) == 0) {
910                 cmd->pccc_cmd = PCC_CLEAR_ALL;
911                 GOTO(out, rc = 0);
912         }
913
914         val = buffer;
915         token = strsep(&val, " ");
916         if (val == NULL || strlen(val) == 0)
917                 GOTO(out_free_cmd, rc = -EINVAL);
918
919         /* Type of the command */
920         if (strcmp(token, "add") == 0)
921                 cmd->pccc_cmd = PCC_ADD_DATASET;
922         else if (strcmp(token, "del") == 0)
923                 cmd->pccc_cmd = PCC_DEL_DATASET;
924         else
925                 GOTO(out_free_cmd, rc = -EINVAL);
926
927         /* Pathname of the dataset */
928         token = strsep(&val, " ");
929         if ((val == NULL && cmd->pccc_cmd != PCC_DEL_DATASET) ||
930             !pathname_is_valid(token))
931                 GOTO(out_free_cmd, rc = -EINVAL);
932         cmd->pccc_pathname = token;
933
934         if (cmd->pccc_cmd == PCC_ADD_DATASET) {
935                 /* List of ID */
936                 LASSERT(val);
937                 token = val;
938                 val = strrchr(token, '}');
939                 if (!val)
940                         GOTO(out_free_cmd, rc = -EINVAL);
941
942                 /* Skip '}' */
943                 val++;
944                 if (*val == '\0') {
945                         val = NULL;
946                 } else if (*val == ' ') {
947                         *val = '\0';
948                         val++;
949                 } else {
950                         GOTO(out_free_cmd, rc = -EINVAL);
951                 }
952
953                 rc = pcc_id_parse(cmd, token);
954                 if (rc)
955                         GOTO(out_free_cmd, rc);
956
957                 rc = pcc_parse_value_pairs(cmd, val);
958                 if (rc)
959                         GOTO(out_cmd_fini, rc = -EINVAL);
960         }
961         goto out;
962 out_cmd_fini:
963         pcc_cmd_fini(cmd);
964 out_free_cmd:
965         OBD_FREE_PTR(cmd);
966 out:
967         if (rc)
968                 cmd = ERR_PTR(rc);
969         return cmd;
970 }
971
972 int pcc_cmd_handle(char *buffer, unsigned long count,
973                    struct pcc_super *super)
974 {
975         int rc = 0;
976         struct pcc_cmd *cmd;
977
978         cmd = pcc_cmd_parse(buffer, count);
979         if (IS_ERR(cmd))
980                 return PTR_ERR(cmd);
981
982         switch (cmd->pccc_cmd) {
983         case PCC_ADD_DATASET:
984                 rc = pcc_dataset_add(super, cmd);
985                 break;
986         case PCC_DEL_DATASET:
987                 rc = pcc_dataset_del(super, cmd->pccc_pathname);
988                 break;
989         case PCC_CLEAR_ALL:
990                 pcc_remove_datasets(super);
991                 break;
992         default:
993                 rc = -EINVAL;
994                 break;
995         }
996
997         pcc_cmd_fini(cmd);
998         OBD_FREE_PTR(cmd);
999         return rc;
1000 }
1001
1002 static inline void pcc_inode_lock(struct inode *inode)
1003 {
1004         mutex_lock(&ll_i2info(inode)->lli_pcc_lock);
1005 }
1006
1007 static inline void pcc_inode_unlock(struct inode *inode)
1008 {
1009         mutex_unlock(&ll_i2info(inode)->lli_pcc_lock);
1010 }
1011
1012 static void pcc_inode_init(struct pcc_inode *pcci, struct ll_inode_info *lli)
1013 {
1014         pcci->pcci_lli = lli;
1015         lli->lli_pcc_inode = pcci;
1016         atomic_set(&pcci->pcci_refcount, 0);
1017         pcci->pcci_type = LU_PCC_NONE;
1018         pcci->pcci_layout_gen = CL_LAYOUT_GEN_NONE;
1019         atomic_set(&pcci->pcci_active_ios, 0);
1020         init_waitqueue_head(&pcci->pcci_waitq);
1021 }
1022
1023 static void pcc_inode_fini(struct pcc_inode *pcci)
1024 {
1025         struct ll_inode_info *lli = pcci->pcci_lli;
1026
1027         path_put(&pcci->pcci_path);
1028         pcci->pcci_type = LU_PCC_NONE;
1029         OBD_SLAB_FREE_PTR(pcci, pcc_inode_slab);
1030         lli->lli_pcc_inode = NULL;
1031 }
1032
1033 static void pcc_inode_get(struct pcc_inode *pcci)
1034 {
1035         atomic_inc(&pcci->pcci_refcount);
1036 }
1037
1038 static void pcc_inode_put(struct pcc_inode *pcci)
1039 {
1040         if (atomic_dec_and_test(&pcci->pcci_refcount))
1041                 pcc_inode_fini(pcci);
1042 }
1043
1044 void pcc_inode_free(struct inode *inode)
1045 {
1046         struct pcc_inode *pcci = ll_i2pcci(inode);
1047
1048         if (pcci) {
1049                 WARN_ON(atomic_read(&pcci->pcci_refcount) > 1);
1050                 pcc_inode_put(pcci);
1051         }
1052 }
1053
1054 /*
1055  * TODO:
1056  * As Andreas suggested, we'd better use new layout to
1057  * reduce overhead:
1058  * (fid->f_oid >> 16 & oxFFFF)/FID
1059  */
1060 #define MAX_PCC_DATABASE_PATH (6 * 5 + FID_NOBRACE_LEN + 1)
1061 static int pcc_fid2dataset_path(char *buf, int sz, struct lu_fid *fid)
1062 {
1063         return snprintf(buf, sz, "%04x/%04x/%04x/%04x/%04x/%04x/"
1064                         DFID_NOBRACE,
1065                         (fid)->f_oid       & 0xFFFF,
1066                         (fid)->f_oid >> 16 & 0xFFFF,
1067                         (unsigned int)((fid)->f_seq       & 0xFFFF),
1068                         (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
1069                         (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
1070                         (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
1071                         PFID(fid));
1072 }
1073
1074 static inline const struct cred *pcc_super_cred(struct super_block *sb)
1075 {
1076         return ll_s2sbi(sb)->ll_pcc_super.pccs_cred;
1077 }
1078
1079 void pcc_file_init(struct pcc_file *pccf)
1080 {
1081         pccf->pccf_file = NULL;
1082         pccf->pccf_type = LU_PCC_NONE;
1083 }
1084
1085 static inline bool pcc_auto_attach_enabled(enum pcc_dataset_flags flags,
1086                                            enum pcc_io_type iot)
1087 {
1088         if (iot == PIT_OPEN)
1089                 return flags & PCC_DATASET_OPEN_ATTACH;
1090         if (iot == PIT_GETATTR)
1091                 return flags & PCC_DATASET_STAT_ATTACH;
1092         else
1093                 return flags & PCC_DATASET_AUTO_ATTACH;
1094 }
1095
1096 static const char pcc_xattr_layout[] = XATTR_USER_PREFIX "PCC.layout";
1097
1098 static int pcc_layout_xattr_set(struct pcc_inode *pcci, __u32 gen)
1099 {
1100         struct dentry *pcc_dentry = pcci->pcci_path.dentry;
1101         struct ll_inode_info *lli = pcci->pcci_lli;
1102         int rc;
1103
1104         ENTRY;
1105
1106         if (!(lli->lli_pcc_dsflags & PCC_DATASET_AUTO_ATTACH))
1107                 RETURN(0);
1108
1109 #ifndef HAVE_VFS_SETXATTR
1110         if (!pcc_dentry->d_inode->i_op->setxattr)
1111                 RETURN(-ENOTSUPP);
1112
1113         rc = pcc_dentry->d_inode->i_op->setxattr(pcc_dentry, pcc_xattr_layout,
1114                                                  &gen, sizeof(gen), 0);
1115 #else
1116         rc = __vfs_setxattr(pcc_dentry, pcc_dentry->d_inode, pcc_xattr_layout,
1117                             &gen, sizeof(gen), 0);
1118 #endif
1119         RETURN(rc);
1120 }
1121
1122 static int pcc_get_layout_info(struct inode *inode, struct cl_layout *clt)
1123 {
1124         struct lu_env *env;
1125         struct ll_inode_info *lli = ll_i2info(inode);
1126         __u16 refcheck;
1127         int rc;
1128
1129         ENTRY;
1130
1131         if (!lli->lli_clob)
1132                 RETURN(-EINVAL);
1133
1134         env = cl_env_get(&refcheck);
1135         if (IS_ERR(env))
1136                 RETURN(PTR_ERR(env));
1137
1138         rc = cl_object_layout_get(env, lli->lli_clob, clt);
1139         if (rc)
1140                 CDEBUG(D_INODE, "Cannot get layout for "DFID"\n",
1141                        PFID(ll_inode2fid(inode)));
1142
1143         cl_env_put(env, &refcheck);
1144         RETURN(rc);
1145 }
1146
1147 static int pcc_fid2dataset_fullpath(char *buf, int sz, struct lu_fid *fid,
1148                                     struct pcc_dataset *dataset)
1149 {
1150         return snprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
1151                         DFID_NOBRACE,
1152                         dataset->pccd_pathname,
1153                         (fid)->f_oid       & 0xFFFF,
1154                         (fid)->f_oid >> 16 & 0xFFFF,
1155                         (unsigned int)((fid)->f_seq       & 0xFFFF),
1156                         (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
1157                         (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
1158                         (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
1159                         PFID(fid));
1160 }
1161
1162 /* Must be called with pcci->pcci_lock held */
1163 static void pcc_inode_attach_init(struct pcc_dataset *dataset,
1164                                   struct pcc_inode *pcci,
1165                                   struct dentry *dentry,
1166                                   enum lu_pcc_type type)
1167 {
1168         pcci->pcci_path.mnt = mntget(dataset->pccd_path.mnt);
1169         pcci->pcci_path.dentry = dentry;
1170         LASSERT(atomic_read(&pcci->pcci_refcount) == 0);
1171         atomic_set(&pcci->pcci_refcount, 1);
1172         pcci->pcci_type = type;
1173         pcci->pcci_attr_valid = false;
1174 }
1175
1176 static inline void pcc_inode_dsflags_set(struct ll_inode_info *lli,
1177                                          struct pcc_dataset *dataset)
1178 {
1179         lli->lli_pcc_generation = ll_info2pccs(lli)->pccs_generation;
1180         lli->lli_pcc_dsflags = dataset->pccd_flags;
1181 }
1182
1183 static void pcc_inode_attach_set(struct pcc_super *super,
1184                                  struct pcc_dataset *dataset,
1185                                  struct ll_inode_info *lli,
1186                                  struct pcc_inode *pcci,
1187                                  struct dentry *dentry,
1188                                  enum lu_pcc_type type)
1189 {
1190         pcc_inode_init(pcci, lli);
1191         pcc_inode_attach_init(dataset, pcci, dentry, type);
1192         down_read(&super->pccs_rw_sem);
1193         pcc_inode_dsflags_set(lli, dataset);
1194         up_read(&super->pccs_rw_sem);
1195 }
1196
1197 static inline void pcc_layout_gen_set(struct pcc_inode *pcci,
1198                                       __u32 gen)
1199 {
1200         pcci->pcci_layout_gen = gen;
1201 }
1202
1203 static inline bool pcc_inode_has_layout(struct pcc_inode *pcci)
1204 {
1205         return pcci->pcci_layout_gen != CL_LAYOUT_GEN_NONE;
1206 }
1207
1208 static int pcc_try_dataset_attach(struct inode *inode, __u32 gen,
1209                                   enum lu_pcc_type type,
1210                                   struct pcc_dataset *dataset,
1211                                   bool *cached)
1212 {
1213         struct ll_inode_info *lli = ll_i2info(inode);
1214         struct pcc_inode *pcci = lli->lli_pcc_inode;
1215         const struct cred *old_cred;
1216         struct dentry *pcc_dentry;
1217         struct path path;
1218         char *pathname;
1219         __u32 pcc_gen;
1220         int rc;
1221
1222         ENTRY;
1223
1224         if (type == LU_PCC_READWRITE &&
1225             !(dataset->pccd_flags & PCC_DATASET_RWPCC))
1226                 RETURN(0);
1227
1228         OBD_ALLOC(pathname, PATH_MAX);
1229         if (pathname == NULL)
1230                 RETURN(-ENOMEM);
1231
1232         pcc_fid2dataset_fullpath(pathname, PATH_MAX, &lli->lli_fid, dataset);
1233
1234         old_cred = override_creds(pcc_super_cred(inode->i_sb));
1235         rc = kern_path(pathname, LOOKUP_FOLLOW, &path);
1236         if (rc)
1237                 /* ignore this error */
1238                 GOTO(out, rc = 0);
1239
1240         pcc_dentry = path.dentry;
1241 #ifndef HAVE_VFS_SETXATTR
1242         if (!pcc_dentry->d_inode->i_op->getxattr)
1243                 /* ignore this error */
1244                 GOTO(out_put_path, rc = 0);
1245
1246         rc = pcc_dentry->d_inode->i_op->getxattr(pcc_dentry, pcc_xattr_layout,
1247                                                  &pcc_gen, sizeof(pcc_gen));
1248 #else
1249         rc = __vfs_getxattr(pcc_dentry, pcc_dentry->d_inode, pcc_xattr_layout,
1250                             &pcc_gen, sizeof(pcc_gen));
1251 #endif
1252
1253         if (rc < 0)
1254                 /* ignore this error */
1255                 GOTO(out_put_path, rc = 0);
1256
1257         rc = 0;
1258         /* The file is still valid cached in PCC, attach it immediately. */
1259         if (pcc_gen == gen) {
1260                 CDEBUG(D_CACHE, DFID" L.Gen (%d) consistent, auto attached.\n",
1261                        PFID(&lli->lli_fid), gen);
1262                 if (!pcci) {
1263                         OBD_SLAB_ALLOC_PTR_GFP(pcci, pcc_inode_slab, GFP_NOFS);
1264                         if (pcci == NULL)
1265                                 GOTO(out_put_path, rc = -ENOMEM);
1266
1267                         pcc_inode_init(pcci, lli);
1268                         dget(pcc_dentry);
1269                         pcc_inode_attach_init(dataset, pcci, pcc_dentry, type);
1270                 } else {
1271                         /*
1272                          * This happened when a file was once attached into
1273                          * PCC, and some processes keep this file opened
1274                          * (pcci->refcount > 1) and corresponding PCC file
1275                          * without any I/O activity, and then this file was
1276                          * detached by the manual detach command or the
1277                          * revocation of the layout lock (i.e. cached LRU lock
1278                          * shrinking).
1279                          */
1280                         pcc_inode_get(pcci);
1281                         pcci->pcci_type = type;
1282                 }
1283                 pcc_inode_dsflags_set(lli, dataset);
1284                 pcc_layout_gen_set(pcci, gen);
1285                 *cached = true;
1286         }
1287 out_put_path:
1288         path_put(&path);
1289 out:
1290         revert_creds(old_cred);
1291         OBD_FREE(pathname, PATH_MAX);
1292         RETURN(rc);
1293 }
1294
1295 static int pcc_try_datasets_attach(struct inode *inode, enum pcc_io_type iot,
1296                                    __u32 gen, enum lu_pcc_type type,
1297                                    bool *cached)
1298 {
1299         struct pcc_super *super = &ll_i2sbi(inode)->ll_pcc_super;
1300         struct ll_inode_info *lli = ll_i2info(inode);
1301         struct pcc_dataset *dataset = NULL, *tmp;
1302         int rc = 0;
1303
1304         ENTRY;
1305
1306         down_read(&super->pccs_rw_sem);
1307         list_for_each_entry_safe(dataset, tmp,
1308                                  &super->pccs_datasets, pccd_linkage) {
1309                 if (!pcc_auto_attach_enabled(dataset->pccd_flags, iot))
1310                         break;
1311
1312                 rc = pcc_try_dataset_attach(inode, gen, type, dataset, cached);
1313                 if (rc < 0 || (!rc && *cached))
1314                         break;
1315         }
1316
1317         /*
1318          * Update the saved dataset flags for the inode accordingly if failed.
1319          */
1320         if (!rc && !*cached) {
1321                 /*
1322                  * Currently auto attach strategy for a PCC backend is
1323                  * unchangeable once once it was added into the PCC datasets on
1324                  * a client as the support to change auto attach strategy is
1325                  * not implemented yet.
1326                  */
1327                 /*
1328                  * If tried to attach from one PCC backend:
1329                  * @lli_pcc_generation > 0:
1330                  * 1) The file was once attached into PCC, but now the
1331                  * corresponding PCC backend should be removed from the client;
1332                  * 2) The layout generation was changed, the data has been
1333                  * restored;
1334                  * 3) The corresponding PCC copy is not existed on PCC
1335                  * @lli_pcc_generation == 0:
1336                  * The file is never attached into PCC but in a HSM released
1337                  * state, or once attached into PCC but the inode was evicted
1338                  * from icache later.
1339                  * Set the saved dataset flags with PCC_DATASET_NONE. Then this
1340                  * file will skip from the candidates to try auto attach until
1341                  * the file is attached into PCC again.
1342                  *
1343                  * If the file was never attached into PCC, or once attached but
1344                  * its inode was evicted from icache (lli_pcc_generation == 0),
1345                  * or the corresponding dataset was removed from the client,
1346                  * set the saved dataset flags with PCC_DATASET_NONE.
1347                  *
1348                  * TODO: If the file was once attached into PCC but not try to
1349                  * auto attach due to the change of the configuration parameters
1350                  * for this dataset (i.e. change from auto attach enabled to
1351                  * auto attach disabled for this dataset), update the saved
1352                  * dataset flags with the found one.
1353                  */
1354                 lli->lli_pcc_dsflags = PCC_DATASET_NONE;
1355         }
1356         up_read(&super->pccs_rw_sem);
1357
1358         RETURN(rc);
1359 }
1360
1361 /*
1362  * TODO: For RW-PCC, it is desirable to store HSM info as a layout (LU-10606).
1363  * Thus the client can get archive ID from the layout directly. When try to
1364  * attach the file automatically which is in HSM released state (according to
1365  * LOV_PATTERN_F_RELEASED in the layout), it can determine whether the file is
1366  * valid cached on PCC more precisely according to the @rwid (archive ID) in
1367  * the PCC dataset and the archive ID in HSM attrs.
1368  */
1369 static int pcc_try_auto_attach(struct inode *inode, bool *cached,
1370                                enum pcc_io_type iot)
1371 {
1372         struct pcc_super *super = &ll_i2sbi(inode)->ll_pcc_super;
1373         struct cl_layout clt = {
1374                 .cl_layout_gen = 0,
1375                 .cl_is_released = false,
1376         };
1377         struct ll_inode_info *lli = ll_i2info(inode);
1378         __u32 gen;
1379         int rc;
1380
1381         ENTRY;
1382
1383         /*
1384          * Quick check whether there is PCC device.
1385          */
1386         if (list_empty(&super->pccs_datasets))
1387                 RETURN(0);
1388
1389         /*
1390          * The file layout lock was cancelled. And this open does not
1391          * obtain valid layout lock from MDT (i.e. the file is being
1392          * HSM restoring).
1393          */
1394         if (iot == PIT_OPEN) {
1395                 if (ll_layout_version_get(lli) == CL_LAYOUT_GEN_NONE)
1396                         RETURN(0);
1397         } else {
1398                 rc = ll_layout_refresh(inode, &gen);
1399                 if (rc)
1400                         RETURN(rc);
1401         }
1402
1403         rc = pcc_get_layout_info(inode, &clt);
1404         if (rc)
1405                 RETURN(rc);
1406
1407         if (iot != PIT_OPEN && gen != clt.cl_layout_gen) {
1408                 CDEBUG(D_CACHE, DFID" layout changed from %d to %d.\n",
1409                        PFID(ll_inode2fid(inode)), gen, clt.cl_layout_gen);
1410                 RETURN(-EINVAL);
1411         }
1412
1413         if (clt.cl_is_released)
1414                 rc = pcc_try_datasets_attach(inode, iot, clt.cl_layout_gen,
1415                                              LU_PCC_READWRITE, cached);
1416
1417         RETURN(rc);
1418 }
1419
1420 static inline bool pcc_may_auto_attach(struct inode *inode,
1421                                        enum pcc_io_type iot)
1422 {
1423         struct ll_inode_info *lli = ll_i2info(inode);
1424         struct pcc_super *super = ll_i2pccs(inode);
1425
1426         /* Known the file was not in any PCC backend. */
1427         if (lli->lli_pcc_dsflags & PCC_DATASET_NONE)
1428                 return false;
1429
1430         /*
1431          * lli_pcc_generation == 0 means that the file was never attached into
1432          * PCC, or may be once attached into PCC but detached as the inode is
1433          * evicted from icache (i.e. "echo 3 > /proc/sys/vm/drop_caches" or
1434          * icache shrinking due to the memory pressure), which will cause the
1435          * file detach from PCC when releasing the inode from icache.
1436          * In either case, we still try to attach.
1437          */
1438         /* lli_pcc_generation == 0, or the PCC setting was changed,
1439          * or there is no PCC setup on the client and the try will return
1440          * immediately in pcc_try_auto_attach().
1441          */
1442         if (super->pccs_generation != lli->lli_pcc_generation)
1443                 return true;
1444
1445         /* The cached setting @lli_pcc_dsflags is valid */
1446         if (iot == PIT_OPEN)
1447                 return lli->lli_pcc_dsflags & PCC_DATASET_OPEN_ATTACH;
1448
1449         if (iot == PIT_GETATTR)
1450                 return lli->lli_pcc_dsflags & PCC_DATASET_STAT_ATTACH;
1451
1452         return lli->lli_pcc_dsflags & PCC_DATASET_IO_ATTACH;
1453 }
1454
1455 int pcc_file_open(struct inode *inode, struct file *file)
1456 {
1457         struct pcc_inode *pcci;
1458         struct ll_inode_info *lli = ll_i2info(inode);
1459         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1460         struct pcc_file *pccf = &fd->fd_pcc_file;
1461         struct file *pcc_file;
1462         struct path *path;
1463         struct qstr *dname;
1464         bool cached = false;
1465         int rc = 0;
1466
1467         ENTRY;
1468
1469         if (!S_ISREG(inode->i_mode))
1470                 RETURN(0);
1471
1472         pcc_inode_lock(inode);
1473         pcci = ll_i2pcci(inode);
1474
1475         if (lli->lli_pcc_state & PCC_STATE_FL_ATTACHING)
1476                 GOTO(out_unlock, rc = 0);
1477
1478         if (!pcci || !pcc_inode_has_layout(pcci)) {
1479                 if (pcc_may_auto_attach(inode, PIT_OPEN))
1480                         rc = pcc_try_auto_attach(inode, &cached, PIT_OPEN);
1481
1482                 if (rc < 0 || !cached)
1483                         GOTO(out_unlock, rc);
1484
1485                 if (!pcci)
1486                         pcci = ll_i2pcci(inode);
1487         }
1488
1489         pcc_inode_get(pcci);
1490         WARN_ON(pccf->pccf_file);
1491
1492         path = &pcci->pcci_path;
1493         dname = &path->dentry->d_name;
1494         CDEBUG(D_CACHE, "opening pcc file '%.*s'\n", dname->len,
1495                dname->name);
1496
1497 #ifdef HAVE_DENTRY_OPEN_USE_PATH
1498         pcc_file = dentry_open(path, file->f_flags,
1499                                pcc_super_cred(inode->i_sb));
1500 #else
1501         pcc_file = dentry_open(path->dentry, path->mnt, file->f_flags,
1502                                pcc_super_cred(inode->i_sb));
1503 #endif
1504         if (IS_ERR_OR_NULL(pcc_file)) {
1505                 rc = pcc_file == NULL ? -EINVAL : PTR_ERR(pcc_file);
1506                 pcc_inode_put(pcci);
1507         } else {
1508                 pccf->pccf_file = pcc_file;
1509                 pccf->pccf_type = pcci->pcci_type;
1510         }
1511
1512 out_unlock:
1513         pcc_inode_unlock(inode);
1514         RETURN(rc);
1515 }
1516
1517 void pcc_file_release(struct inode *inode, struct file *file)
1518 {
1519         struct pcc_inode *pcci;
1520         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1521         struct pcc_file *pccf;
1522         struct path *path;
1523         struct qstr *dname;
1524
1525         ENTRY;
1526
1527         if (!S_ISREG(inode->i_mode) || fd == NULL)
1528                 RETURN_EXIT;
1529
1530         pccf = &fd->fd_pcc_file;
1531         pcc_inode_lock(inode);
1532         if (pccf->pccf_file == NULL)
1533                 goto out;
1534
1535         pcci = ll_i2pcci(inode);
1536         LASSERT(pcci);
1537         path = &pcci->pcci_path;
1538         dname = &path->dentry->d_name;
1539         CDEBUG(D_CACHE, "releasing pcc file \"%.*s\"\n", dname->len,
1540                dname->name);
1541         pcc_inode_put(pcci);
1542         fput(pccf->pccf_file);
1543         pccf->pccf_file = NULL;
1544 out:
1545         pcc_inode_unlock(inode);
1546         RETURN_EXIT;
1547 }
1548
1549 static void pcc_io_init(struct inode *inode, enum pcc_io_type iot, bool *cached)
1550 {
1551         struct pcc_inode *pcci;
1552
1553         pcc_inode_lock(inode);
1554         pcci = ll_i2pcci(inode);
1555         if (pcci && pcc_inode_has_layout(pcci)) {
1556                 LASSERT(atomic_read(&pcci->pcci_refcount) > 0);
1557                 atomic_inc(&pcci->pcci_active_ios);
1558                 *cached = true;
1559         } else {
1560                 *cached = false;
1561                 if (pcc_may_auto_attach(inode, iot)) {
1562                         (void) pcc_try_auto_attach(inode, cached, iot);
1563                         if (*cached) {
1564                                 pcci = ll_i2pcci(inode);
1565                                 LASSERT(atomic_read(&pcci->pcci_refcount) > 0);
1566                                 atomic_inc(&pcci->pcci_active_ios);
1567                         }
1568                 }
1569         }
1570         pcc_inode_unlock(inode);
1571 }
1572
1573 static void pcc_io_fini(struct inode *inode)
1574 {
1575         struct pcc_inode *pcci = ll_i2pcci(inode);
1576
1577         LASSERT(pcci && atomic_read(&pcci->pcci_active_ios) > 0);
1578         if (atomic_dec_and_test(&pcci->pcci_active_ios))
1579                 wake_up_all(&pcci->pcci_waitq);
1580 }
1581
1582
1583 static ssize_t
1584 __pcc_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
1585 {
1586         struct file *file = iocb->ki_filp;
1587
1588 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
1589         return file->f_op->read_iter(iocb, iter);
1590 #else
1591         struct iovec iov;
1592         struct iov_iter i;
1593         ssize_t bytes = 0;
1594
1595         iov_for_each(iov, i, *iter) {
1596                 ssize_t res;
1597
1598                 res = file->f_op->aio_read(iocb, &iov, 1, iocb->ki_pos);
1599                 if (-EIOCBQUEUED == res)
1600                         res = wait_on_sync_kiocb(iocb);
1601                 if (res <= 0) {
1602                         if (bytes == 0)
1603                                 bytes = res;
1604                         break;
1605                 }
1606
1607                 bytes += res;
1608                 if (res < iov.iov_len)
1609                         break;
1610         }
1611
1612         if (bytes > 0)
1613                 iov_iter_advance(iter, bytes);
1614         return bytes;
1615 #endif
1616 }
1617
1618 ssize_t pcc_file_read_iter(struct kiocb *iocb,
1619                            struct iov_iter *iter, bool *cached)
1620 {
1621         struct file *file = iocb->ki_filp;
1622         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1623         struct pcc_file *pccf = &fd->fd_pcc_file;
1624         struct inode *inode = file_inode(file);
1625         ssize_t result;
1626
1627         ENTRY;
1628
1629         if (pccf->pccf_file == NULL) {
1630                 *cached = false;
1631                 RETURN(0);
1632         }
1633
1634         pcc_io_init(inode, PIT_READ, cached);
1635         if (!*cached)
1636                 RETURN(0);
1637
1638         iocb->ki_filp = pccf->pccf_file;
1639         /* generic_file_aio_read does not support ext4-dax,
1640          * __pcc_file_read_iter uses ->aio_read hook directly
1641          * to add support for ext4-dax.
1642          */
1643         result = __pcc_file_read_iter(iocb, iter);
1644         iocb->ki_filp = file;
1645
1646         pcc_io_fini(inode);
1647         RETURN(result);
1648 }
1649
1650 static ssize_t
1651 __pcc_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
1652 {
1653         struct file *file = iocb->ki_filp;
1654
1655 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
1656         return file->f_op->write_iter(iocb, iter);
1657 #else
1658         struct iovec iov;
1659         struct iov_iter i;
1660         ssize_t bytes = 0;
1661
1662         iov_for_each(iov, i, *iter) {
1663                 ssize_t res;
1664
1665                 res = file->f_op->aio_write(iocb, &iov, 1, iocb->ki_pos);
1666                 if (-EIOCBQUEUED == res)
1667                         res = wait_on_sync_kiocb(iocb);
1668                 if (res <= 0) {
1669                         if (bytes == 0)
1670                                 bytes = res;
1671                         break;
1672                 }
1673
1674                 bytes += res;
1675                 if (res < iov.iov_len)
1676                         break;
1677         }
1678
1679         if (bytes > 0)
1680                 iov_iter_advance(iter, bytes);
1681         return bytes;
1682 #endif
1683 }
1684
1685 ssize_t pcc_file_write_iter(struct kiocb *iocb,
1686                             struct iov_iter *iter, bool *cached)
1687 {
1688         struct file *file = iocb->ki_filp;
1689         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1690         struct pcc_file *pccf = &fd->fd_pcc_file;
1691         struct inode *inode = file_inode(file);
1692         ssize_t result;
1693
1694         ENTRY;
1695
1696         if (pccf->pccf_file == NULL) {
1697                 *cached = false;
1698                 RETURN(0);
1699         }
1700
1701         if (pccf->pccf_type != LU_PCC_READWRITE) {
1702                 *cached = false;
1703                 RETURN(-EAGAIN);
1704         }
1705
1706         pcc_io_init(inode, PIT_WRITE, cached);
1707         if (!*cached)
1708                 RETURN(0);
1709
1710         if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_PCC_FAKE_ERROR))
1711                 GOTO(out, result = -ENOSPC);
1712
1713         iocb->ki_filp = pccf->pccf_file;
1714
1715         /* Since __pcc_file_write_iter makes write calls via
1716          * the normal vfs interface to the local PCC file system,
1717          * the inode lock is not needed.
1718          */
1719         result = __pcc_file_write_iter(iocb, iter);
1720         iocb->ki_filp = file;
1721 out:
1722         pcc_io_fini(inode);
1723         RETURN(result);
1724 }
1725
1726 int pcc_inode_setattr(struct inode *inode, struct iattr *attr,
1727                       bool *cached)
1728 {
1729         int rc;
1730         const struct cred *old_cred;
1731         struct iattr attr2 = *attr;
1732         struct dentry *pcc_dentry;
1733         struct pcc_inode *pcci;
1734
1735         ENTRY;
1736
1737         if (!S_ISREG(inode->i_mode)) {
1738                 *cached = false;
1739                 RETURN(0);
1740         }
1741
1742         pcc_io_init(inode, PIT_SETATTR, cached);
1743         if (!*cached)
1744                 RETURN(0);
1745
1746         attr2.ia_valid = attr->ia_valid & (ATTR_SIZE | ATTR_ATIME |
1747                          ATTR_ATIME_SET | ATTR_MTIME | ATTR_MTIME_SET |
1748                          ATTR_CTIME | ATTR_UID | ATTR_GID);
1749         pcci = ll_i2pcci(inode);
1750         pcc_dentry = pcci->pcci_path.dentry;
1751         inode_lock(pcc_dentry->d_inode);
1752         old_cred = override_creds(pcc_super_cred(inode->i_sb));
1753         rc = pcc_dentry->d_inode->i_op->setattr(pcc_dentry, &attr2);
1754         revert_creds(old_cred);
1755         inode_unlock(pcc_dentry->d_inode);
1756
1757         pcc_io_fini(inode);
1758         RETURN(rc);
1759 }
1760
1761 int pcc_inode_getattr(struct inode *inode, bool *cached)
1762 {
1763         struct ll_inode_info *lli = ll_i2info(inode);
1764         const struct cred *old_cred;
1765         struct kstat stat;
1766         s64 atime;
1767         s64 mtime;
1768         s64 ctime;
1769         int rc;
1770
1771         ENTRY;
1772
1773         if (!S_ISREG(inode->i_mode)) {
1774                 *cached = false;
1775                 RETURN(0);
1776         }
1777
1778         pcc_io_init(inode, PIT_GETATTR, cached);
1779         if (!*cached)
1780                 RETURN(0);
1781
1782         old_cred = override_creds(pcc_super_cred(inode->i_sb));
1783         rc = ll_vfs_getattr(&ll_i2pcci(inode)->pcci_path, &stat);
1784         revert_creds(old_cred);
1785         if (rc)
1786                 GOTO(out, rc);
1787
1788         ll_inode_size_lock(inode);
1789         if (inode->i_atime.tv_sec < lli->lli_atime ||
1790             lli->lli_update_atime) {
1791                 inode->i_atime.tv_sec = lli->lli_atime;
1792                 lli->lli_update_atime = 0;
1793         }
1794         inode->i_mtime.tv_sec = lli->lli_mtime;
1795         inode->i_ctime.tv_sec = lli->lli_ctime;
1796
1797         atime = inode->i_atime.tv_sec;
1798         mtime = inode->i_mtime.tv_sec;
1799         ctime = inode->i_ctime.tv_sec;
1800
1801         if (atime < stat.atime.tv_sec)
1802                 atime = stat.atime.tv_sec;
1803
1804         if (ctime < stat.ctime.tv_sec)
1805                 ctime = stat.ctime.tv_sec;
1806
1807         if (mtime < stat.mtime.tv_sec)
1808                 mtime = stat.mtime.tv_sec;
1809
1810         i_size_write(inode, stat.size);
1811         inode->i_blocks = stat.blocks;
1812
1813         inode->i_atime.tv_sec = atime;
1814         inode->i_mtime.tv_sec = mtime;
1815         inode->i_ctime.tv_sec = ctime;
1816
1817         ll_inode_size_unlock(inode);
1818 out:
1819         pcc_io_fini(inode);
1820         RETURN(rc);
1821 }
1822
1823 ssize_t pcc_file_splice_read(struct file *in_file, loff_t *ppos,
1824                              struct pipe_inode_info *pipe,
1825                              size_t count, unsigned int flags,
1826                              bool *cached)
1827 {
1828         struct inode *inode = file_inode(in_file);
1829         struct ll_file_data *fd = LUSTRE_FPRIVATE(in_file);
1830         struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1831         ssize_t result;
1832
1833         ENTRY;
1834
1835         *cached = false;
1836         if (!pcc_file)
1837                 RETURN(0);
1838
1839         if (!file_inode(pcc_file)->i_fop->splice_read)
1840                 RETURN(-ENOTSUPP);
1841
1842         pcc_io_init(inode, PIT_SPLICE_READ, cached);
1843         if (!*cached)
1844                 RETURN(0);
1845
1846         result = file_inode(pcc_file)->i_fop->splice_read(pcc_file,
1847                                                           ppos, pipe, count,
1848                                                           flags);
1849
1850         pcc_io_fini(inode);
1851         RETURN(result);
1852 }
1853
1854 int pcc_fsync(struct file *file, loff_t start, loff_t end,
1855               int datasync, bool *cached)
1856 {
1857         struct inode *inode = file_inode(file);
1858         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1859         struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1860         int rc;
1861
1862         ENTRY;
1863
1864         if (!pcc_file) {
1865                 *cached = false;
1866                 RETURN(0);
1867         }
1868
1869         pcc_io_init(inode, PIT_FSYNC, cached);
1870         if (!*cached)
1871                 RETURN(0);
1872
1873         rc = file_inode(pcc_file)->i_fop->fsync(pcc_file,
1874                                                 start, end, datasync);
1875
1876         pcc_io_fini(inode);
1877         RETURN(rc);
1878 }
1879
1880 int pcc_file_mmap(struct file *file, struct vm_area_struct *vma,
1881                   bool *cached)
1882 {
1883         struct inode *inode = file_inode(file);
1884         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1885         struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1886         struct pcc_inode *pcci;
1887         int rc = 0;
1888
1889         ENTRY;
1890
1891         if (!pcc_file || !file_inode(pcc_file)->i_fop->mmap) {
1892                 *cached = false;
1893                 RETURN(0);
1894         }
1895
1896         pcc_inode_lock(inode);
1897         pcci = ll_i2pcci(inode);
1898         if (pcci && pcc_inode_has_layout(pcci)) {
1899                 LASSERT(atomic_read(&pcci->pcci_refcount) > 1);
1900                 *cached = true;
1901                 vma->vm_file = pcc_file;
1902                 rc = file_inode(pcc_file)->i_fop->mmap(pcc_file, vma);
1903                 vma->vm_file = file;
1904                 /* Save the vm ops of backend PCC */
1905                 vma->vm_private_data = (void *)vma->vm_ops;
1906         } else {
1907                 *cached = false;
1908         }
1909         pcc_inode_unlock(inode);
1910
1911         RETURN(rc);
1912 }
1913
1914 void pcc_vm_open(struct vm_area_struct *vma)
1915 {
1916         struct pcc_inode *pcci;
1917         struct file *file = vma->vm_file;
1918         struct inode *inode = file_inode(file);
1919         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1920         struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1921         struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
1922
1923         ENTRY;
1924
1925         if (!pcc_file || !pcc_vm_ops || !pcc_vm_ops->open)
1926                 RETURN_EXIT;
1927
1928         pcc_inode_lock(inode);
1929         pcci = ll_i2pcci(inode);
1930         if (pcci && pcc_inode_has_layout(pcci)) {
1931                 vma->vm_file = pcc_file;
1932                 pcc_vm_ops->open(vma);
1933                 vma->vm_file = file;
1934         }
1935         pcc_inode_unlock(inode);
1936         EXIT;
1937 }
1938
1939 void pcc_vm_close(struct vm_area_struct *vma)
1940 {
1941         struct file *file = vma->vm_file;
1942         struct inode *inode = file_inode(file);
1943         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1944         struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1945         struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
1946
1947         ENTRY;
1948
1949         if (!pcc_file || !pcc_vm_ops || !pcc_vm_ops->close)
1950                 RETURN_EXIT;
1951
1952         pcc_inode_lock(inode);
1953         /* Layout lock maybe revoked here */
1954         vma->vm_file = pcc_file;
1955         pcc_vm_ops->close(vma);
1956         vma->vm_file = file;
1957         pcc_inode_unlock(inode);
1958         EXIT;
1959 }
1960
1961 int pcc_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
1962                      bool *cached)
1963 {
1964         struct page *page = vmf->page;
1965         struct mm_struct *mm = vma->vm_mm;
1966         struct file *file = vma->vm_file;
1967         struct inode *inode = file_inode(file);
1968         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1969         struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1970         struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
1971         int rc;
1972
1973         ENTRY;
1974
1975         if (!pcc_file || !pcc_vm_ops) {
1976                 *cached = false;
1977                 RETURN(0);
1978         }
1979
1980         if (!pcc_vm_ops->page_mkwrite &&
1981             page->mapping == pcc_file->f_mapping) {
1982                 CDEBUG(D_MMAP,
1983                        "%s: PCC backend fs not support ->page_mkwrite()\n",
1984                        ll_i2sbi(inode)->ll_fsname);
1985                 pcc_ioctl_detach(inode, PCC_DETACH_OPT_UNCACHE);
1986                 up_read(&mm->mmap_sem);
1987                 *cached = true;
1988                 RETURN(VM_FAULT_RETRY | VM_FAULT_NOPAGE);
1989         }
1990         /* Pause to allow for a race with concurrent detach */
1991         OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_PCC_MKWRITE_PAUSE, cfs_fail_val);
1992
1993         pcc_io_init(inode, PIT_PAGE_MKWRITE, cached);
1994         if (!*cached) {
1995                 /* This happens when the file is detached from PCC after got
1996                  * the fault page via ->fault() on the inode of the PCC copy.
1997                  * Here it can not simply fall back to normal Lustre I/O path.
1998                  * The reason is that the address space of fault page used by
1999                  * ->page_mkwrite() is still the one of PCC inode. In the
2000                  * normal Lustre ->page_mkwrite() I/O path, it will be wrongly
2001                  * handled as the address space of the fault page is not
2002                  * consistent with the one of the Lustre inode (though the
2003                  * fault page was truncated).
2004                  * As the file is detached from PCC, the fault page must
2005                  * be released frist, and retry the mmap write (->fault() and
2006                  * ->page_mkwrite).
2007                  * We use an ugly and tricky method by returning
2008                  * VM_FAULT_NOPAGE | VM_FAULT_RETRY to the caller
2009                  * __do_page_fault and retry the memory fault handling.
2010                  */
2011                 if (page->mapping == pcc_file->f_mapping) {
2012                         *cached = true;
2013                         up_read(&mm->mmap_sem);
2014                         RETURN(VM_FAULT_RETRY | VM_FAULT_NOPAGE);
2015                 }
2016
2017                 RETURN(0);
2018         }
2019
2020         /*
2021          * This fault injection can also be used to simulate -ENOSPC and
2022          * -EDQUOT failure of underlying PCC backend fs.
2023          */
2024         if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_PCC_DETACH_MKWRITE)) {
2025                 pcc_io_fini(inode);
2026                 pcc_ioctl_detach(inode, PCC_DETACH_OPT_UNCACHE);
2027                 up_read(&mm->mmap_sem);
2028                 RETURN(VM_FAULT_RETRY | VM_FAULT_NOPAGE);
2029         }
2030
2031         vma->vm_file = pcc_file;
2032 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
2033         rc = pcc_vm_ops->page_mkwrite(vmf);
2034 #else
2035         rc = pcc_vm_ops->page_mkwrite(vma, vmf);
2036 #endif
2037         vma->vm_file = file;
2038
2039         pcc_io_fini(inode);
2040         RETURN(rc);
2041 }
2042
2043 int pcc_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
2044               bool *cached)
2045 {
2046         struct file *file = vma->vm_file;
2047         struct inode *inode = file_inode(file);
2048         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2049         struct file *pcc_file = fd->fd_pcc_file.pccf_file;
2050         struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
2051         int rc;
2052
2053         ENTRY;
2054
2055         if (!pcc_file || !pcc_vm_ops || !pcc_vm_ops->fault) {
2056                 *cached = false;
2057                 RETURN(0);
2058         }
2059
2060         pcc_io_init(inode, PIT_FAULT, cached);
2061         if (!*cached)
2062                 RETURN(0);
2063
2064         vma->vm_file = pcc_file;
2065 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
2066         rc = pcc_vm_ops->fault(vmf);
2067 #else
2068         rc = pcc_vm_ops->fault(vma, vmf);
2069 #endif
2070         vma->vm_file = file;
2071
2072         pcc_io_fini(inode);
2073         RETURN(rc);
2074 }
2075
2076 static void pcc_layout_wait(struct pcc_inode *pcci)
2077 {
2078         struct l_wait_info lwi = { 0 };
2079
2080         while (atomic_read(&pcci->pcci_active_ios) > 0) {
2081                 CDEBUG(D_CACHE, "Waiting for IO completion: %d\n",
2082                        atomic_read(&pcci->pcci_active_ios));
2083                 l_wait_event(pcci->pcci_waitq,
2084                              atomic_read(&pcci->pcci_active_ios) == 0, &lwi);
2085         }
2086 }
2087
2088 static void __pcc_layout_invalidate(struct pcc_inode *pcci)
2089 {
2090         pcci->pcci_type = LU_PCC_NONE;
2091         pcc_layout_gen_set(pcci, CL_LAYOUT_GEN_NONE);
2092         pcc_layout_wait(pcci);
2093 }
2094
2095 void pcc_layout_invalidate(struct inode *inode)
2096 {
2097         struct pcc_inode *pcci;
2098
2099         ENTRY;
2100
2101         pcc_inode_lock(inode);
2102         pcci = ll_i2pcci(inode);
2103         if (pcci && pcc_inode_has_layout(pcci)) {
2104                 LASSERT(atomic_read(&pcci->pcci_refcount) > 0);
2105                 __pcc_layout_invalidate(pcci);
2106
2107                 CDEBUG(D_CACHE, "Invalidate "DFID" layout gen %d\n",
2108                        PFID(&ll_i2info(inode)->lli_fid), pcci->pcci_layout_gen);
2109
2110                 pcc_inode_put(pcci);
2111         }
2112         pcc_inode_unlock(inode);
2113
2114         EXIT;
2115 }
2116
2117 static int pcc_inode_remove(struct inode *inode, struct dentry *pcc_dentry)
2118 {
2119         int rc;
2120
2121         rc = ll_vfs_unlink(pcc_dentry->d_parent->d_inode, pcc_dentry);
2122         if (rc)
2123                 CWARN("%s: failed to unlink PCC file %.*s, rc = %d\n",
2124                       ll_i2sbi(inode)->ll_fsname, pcc_dentry->d_name.len,
2125                       pcc_dentry->d_name.name, rc);
2126
2127         return rc;
2128 }
2129
2130 /* Create directory under base if directory does not exist */
2131 static struct dentry *
2132 pcc_mkdir(struct dentry *base, const char *name, umode_t mode)
2133 {
2134         int rc;
2135         struct dentry *dentry;
2136         struct inode *dir = base->d_inode;
2137
2138         inode_lock(dir);
2139         dentry = lookup_one_len(name, base, strlen(name));
2140         if (IS_ERR(dentry))
2141                 goto out;
2142
2143         if (d_is_positive(dentry))
2144                 goto out;
2145
2146         rc = vfs_mkdir(dir, dentry, mode);
2147         if (rc) {
2148                 dput(dentry);
2149                 dentry = ERR_PTR(rc);
2150                 goto out;
2151         }
2152 out:
2153         inode_unlock(dir);
2154         return dentry;
2155 }
2156
2157 static struct dentry *
2158 pcc_mkdir_p(struct dentry *root, char *path, umode_t mode)
2159 {
2160         char *ptr, *entry_name;
2161         struct dentry *parent;
2162         struct dentry *child = ERR_PTR(-EINVAL);
2163
2164         ptr = path;
2165         while (*ptr == '/')
2166                 ptr++;
2167
2168         entry_name = ptr;
2169         parent = dget(root);
2170         while ((ptr = strchr(ptr, '/')) != NULL) {
2171                 *ptr = '\0';
2172                 child = pcc_mkdir(parent, entry_name, mode);
2173                 *ptr = '/';
2174                 dput(parent);
2175                 if (IS_ERR(child))
2176                         break;
2177
2178                 parent = child;
2179                 ptr++;
2180                 entry_name = ptr;
2181         }
2182
2183         return child;
2184 }
2185
2186 /* Create file under base. If file already exist, return failure */
2187 static struct dentry *
2188 pcc_create(struct dentry *base, const char *name, umode_t mode)
2189 {
2190         int rc;
2191         struct dentry *dentry;
2192         struct inode *dir = base->d_inode;
2193
2194         inode_lock(dir);
2195         dentry = lookup_one_len(name, base, strlen(name));
2196         if (IS_ERR(dentry))
2197                 goto out;
2198
2199         if (d_is_positive(dentry))
2200                 goto out;
2201
2202         rc = vfs_create(dir, dentry, mode, LL_VFS_CREATE_FALSE);
2203         if (rc) {
2204                 dput(dentry);
2205                 dentry = ERR_PTR(rc);
2206                 goto out;
2207         }
2208 out:
2209         inode_unlock(dir);
2210         return dentry;
2211 }
2212
2213 static int __pcc_inode_create(struct pcc_dataset *dataset,
2214                               struct lu_fid *fid,
2215                               struct dentry **dentry)
2216 {
2217         char *path;
2218         struct dentry *base;
2219         struct dentry *child;
2220         int rc = 0;
2221
2222         OBD_ALLOC(path, MAX_PCC_DATABASE_PATH);
2223         if (path == NULL)
2224                 return -ENOMEM;
2225
2226         pcc_fid2dataset_path(path, MAX_PCC_DATABASE_PATH, fid);
2227
2228         base = pcc_mkdir_p(dataset->pccd_path.dentry, path, 0);
2229         if (IS_ERR(base)) {
2230                 rc = PTR_ERR(base);
2231                 GOTO(out, rc);
2232         }
2233
2234         snprintf(path, MAX_PCC_DATABASE_PATH, DFID_NOBRACE, PFID(fid));
2235         child = pcc_create(base, path, 0);
2236         if (IS_ERR(child)) {
2237                 rc = PTR_ERR(child);
2238                 GOTO(out_base, rc);
2239         }
2240         *dentry = child;
2241
2242 out_base:
2243         dput(base);
2244 out:
2245         OBD_FREE(path, MAX_PCC_DATABASE_PATH);
2246         return rc;
2247 }
2248
2249 /*
2250  * Reset uid, gid or size for the PCC copy masked by @valid.
2251  * TODO: Set the project ID for PCC copy.
2252  */
2253 int pcc_inode_reset_iattr(struct dentry *dentry, unsigned int valid,
2254                           kuid_t uid, kgid_t gid, loff_t size)
2255 {
2256         struct inode *inode = dentry->d_inode;
2257         struct iattr attr;
2258         int rc;
2259
2260         ENTRY;
2261
2262         attr.ia_valid = valid;
2263         attr.ia_uid = uid;
2264         attr.ia_gid = gid;
2265         attr.ia_size = size;
2266
2267         inode_lock(inode);
2268         rc = notify_change(dentry, &attr, NULL);
2269         inode_unlock(inode);
2270
2271         RETURN(rc);
2272 }
2273
2274 int pcc_inode_create(struct super_block *sb, struct pcc_dataset *dataset,
2275                      struct lu_fid *fid, struct dentry **pcc_dentry)
2276 {
2277         const struct cred *old_cred;
2278         int rc;
2279
2280         old_cred = override_creds(pcc_super_cred(sb));
2281         rc = __pcc_inode_create(dataset, fid, pcc_dentry);
2282         revert_creds(old_cred);
2283         return rc;
2284 }
2285
2286 int pcc_inode_create_fini(struct inode *inode, struct pcc_create_attach *pca)
2287 {
2288         struct dentry *pcc_dentry = pca->pca_dentry;
2289         struct pcc_super *super = ll_i2pccs(inode);
2290         const struct cred *old_cred;
2291         struct pcc_inode *pcci;
2292         int rc;
2293
2294         ENTRY;
2295
2296         if (!pca->pca_dataset)
2297                 RETURN(0);
2298
2299         if (!inode)
2300                 GOTO(out_dataset_put, rc = 0);
2301
2302         LASSERT(pcc_dentry);
2303
2304         old_cred = override_creds(super->pccs_cred);
2305         pcc_inode_lock(inode);
2306         LASSERT(ll_i2pcci(inode) == NULL);
2307         OBD_SLAB_ALLOC_PTR_GFP(pcci, pcc_inode_slab, GFP_NOFS);
2308         if (pcci == NULL)
2309                 GOTO(out_put, rc = -ENOMEM);
2310
2311         rc = pcc_inode_reset_iattr(pcc_dentry, ATTR_UID | ATTR_GID,
2312                                    old_cred->suid, old_cred->sgid, 0);
2313         if (rc)
2314                 GOTO(out_put, rc);
2315
2316         pcc_inode_attach_set(super, pca->pca_dataset, ll_i2info(inode),
2317                              pcci, pcc_dentry, LU_PCC_READWRITE);
2318
2319         rc = pcc_layout_xattr_set(pcci, 0);
2320         if (rc) {
2321                 (void) pcc_inode_remove(inode, pcci->pcci_path.dentry);
2322                 pcc_inode_put(pcci);
2323                 GOTO(out_unlock, rc);
2324         }
2325
2326         /* Set the layout generation of newly created file with 0 */
2327         pcc_layout_gen_set(pcci, 0);
2328
2329 out_put:
2330         if (rc) {
2331                 (void) pcc_inode_remove(inode, pcc_dentry);
2332                 dput(pcc_dentry);
2333
2334                 if (pcci)
2335                         OBD_SLAB_FREE_PTR(pcci, pcc_inode_slab);
2336         }
2337 out_unlock:
2338         pcc_inode_unlock(inode);
2339         revert_creds(old_cred);
2340 out_dataset_put:
2341         pcc_dataset_put(pca->pca_dataset);
2342         RETURN(rc);
2343 }
2344
2345 void pcc_create_attach_cleanup(struct super_block *sb,
2346                                struct pcc_create_attach *pca)
2347 {
2348         if (!pca->pca_dataset)
2349                 return;
2350
2351         if (pca->pca_dentry) {
2352                 const struct cred *old_cred;
2353                 int rc;
2354
2355                 old_cred = override_creds(pcc_super_cred(sb));
2356                 rc = ll_vfs_unlink(pca->pca_dentry->d_parent->d_inode,
2357                                    pca->pca_dentry);
2358                 if (rc)
2359                         CWARN("failed to unlink PCC file %.*s, rc = %d\n",
2360                               pca->pca_dentry->d_name.len,
2361                               pca->pca_dentry->d_name.name, rc);
2362                 /* ignore the unlink failure */
2363                 revert_creds(old_cred);
2364                 dput(pca->pca_dentry);
2365         }
2366
2367         pcc_dataset_put(pca->pca_dataset);
2368 }
2369
2370 static int pcc_filp_write(struct file *filp, const void *buf, ssize_t count,
2371                           loff_t *offset)
2372 {
2373         while (count > 0) {
2374                 ssize_t size;
2375
2376                 size = cfs_kernel_write(filp, buf, count, offset);
2377                 if (size < 0)
2378                         return size;
2379                 count -= size;
2380                 buf += size;
2381         }
2382         return 0;
2383 }
2384
2385 static ssize_t pcc_copy_data(struct file *src, struct file *dst)
2386 {
2387         ssize_t rc = 0;
2388         ssize_t rc2;
2389         loff_t pos, offset = 0;
2390         size_t buf_len = 1048576;
2391         void *buf;
2392
2393         ENTRY;
2394
2395         OBD_ALLOC_LARGE(buf, buf_len);
2396         if (buf == NULL)
2397                 RETURN(-ENOMEM);
2398
2399         while (1) {
2400                 if (signal_pending(current))
2401                         GOTO(out_free, rc = -EINTR);
2402
2403                 pos = offset;
2404                 rc2 = cfs_kernel_read(src, buf, buf_len, &pos);
2405                 if (rc2 < 0)
2406                         GOTO(out_free, rc = rc2);
2407                 else if (rc2 == 0)
2408                         break;
2409
2410                 pos = offset;
2411                 rc = pcc_filp_write(dst, buf, rc2, &pos);
2412                 if (rc < 0)
2413                         GOTO(out_free, rc);
2414                 offset += rc2;
2415         }
2416
2417         rc = offset;
2418 out_free:
2419         OBD_FREE_LARGE(buf, buf_len);
2420         RETURN(rc);
2421 }
2422
2423 static int pcc_attach_allowed_check(struct inode *inode)
2424 {
2425         struct ll_inode_info *lli = ll_i2info(inode);
2426         struct pcc_inode *pcci;
2427         int rc = 0;
2428
2429         ENTRY;
2430
2431         pcc_inode_lock(inode);
2432         if (lli->lli_pcc_state & PCC_STATE_FL_ATTACHING)
2433                 GOTO(out_unlock, rc = -EBUSY);
2434
2435         pcci = ll_i2pcci(inode);
2436         if (pcci && pcc_inode_has_layout(pcci))
2437                 GOTO(out_unlock, rc = -EEXIST);
2438
2439         lli->lli_pcc_state |= PCC_STATE_FL_ATTACHING;
2440 out_unlock:
2441         pcc_inode_unlock(inode);
2442         RETURN(rc);
2443 }
2444
2445 int pcc_readwrite_attach(struct file *file, struct inode *inode,
2446                          __u32 archive_id)
2447 {
2448         struct pcc_dataset *dataset;
2449         struct ll_inode_info *lli = ll_i2info(inode);
2450         struct pcc_super *super = ll_i2pccs(inode);
2451         struct pcc_inode *pcci;
2452         const struct cred *old_cred;
2453         struct dentry *dentry;
2454         struct file *pcc_filp;
2455         struct path path;
2456         ssize_t ret;
2457         int rc;
2458
2459         ENTRY;
2460
2461         rc = pcc_attach_allowed_check(inode);
2462         if (rc)
2463                 RETURN(rc);
2464
2465         dataset = pcc_dataset_get(&ll_i2sbi(inode)->ll_pcc_super,
2466                                   LU_PCC_READWRITE, archive_id);
2467         if (dataset == NULL)
2468                 RETURN(-ENOENT);
2469
2470         old_cred = override_creds(super->pccs_cred);
2471         rc = __pcc_inode_create(dataset, &lli->lli_fid, &dentry);
2472         if (rc)
2473                 GOTO(out_dataset_put, rc);
2474
2475         path.mnt = dataset->pccd_path.mnt;
2476         path.dentry = dentry;
2477 #ifdef HAVE_DENTRY_OPEN_USE_PATH
2478         pcc_filp = dentry_open(&path, O_WRONLY | O_LARGEFILE, current_cred());
2479 #else
2480         pcc_filp = dentry_open(path.dentry, path.mnt, O_WRONLY | O_LARGEFILE,
2481                                current_cred());
2482 #endif
2483         if (IS_ERR_OR_NULL(pcc_filp)) {
2484                 rc = pcc_filp == NULL ? -EINVAL : PTR_ERR(pcc_filp);
2485                 GOTO(out_dentry, rc);
2486         }
2487
2488         rc = pcc_inode_reset_iattr(dentry, ATTR_UID | ATTR_GID,
2489                                    old_cred->uid, old_cred->gid, 0);
2490         if (rc)
2491                 GOTO(out_fput, rc);
2492
2493         ret = pcc_copy_data(file, pcc_filp);
2494         if (ret < 0)
2495                 GOTO(out_fput, rc = ret);
2496
2497         /*
2498          * It must to truncate the PCC copy to the same size of the Lustre
2499          * copy after copy data. Otherwise, it may get wrong file size after
2500          * re-attach a file. See LU-13023 for details.
2501          */
2502         rc = pcc_inode_reset_iattr(dentry, ATTR_SIZE, KUIDT_INIT(0),
2503                                    KGIDT_INIT(0), ret);
2504         if (rc)
2505                 GOTO(out_fput, rc);
2506
2507         /* Pause to allow for a race with concurrent HSM remove */
2508         OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_PCC_ATTACH_PAUSE, cfs_fail_val);
2509
2510         pcc_inode_lock(inode);
2511         pcci = ll_i2pcci(inode);
2512         LASSERT(!pcci);
2513         OBD_SLAB_ALLOC_PTR_GFP(pcci, pcc_inode_slab, GFP_NOFS);
2514         if (pcci == NULL)
2515                 GOTO(out_unlock, rc = -ENOMEM);
2516
2517         pcc_inode_attach_set(super, dataset, lli, pcci,
2518                              dentry, LU_PCC_READWRITE);
2519 out_unlock:
2520         pcc_inode_unlock(inode);
2521 out_fput:
2522         fput(pcc_filp);
2523 out_dentry:
2524         if (rc) {
2525                 (void) pcc_inode_remove(inode, dentry);
2526                 dput(dentry);
2527         }
2528 out_dataset_put:
2529         pcc_dataset_put(dataset);
2530         revert_creds(old_cred);
2531
2532         RETURN(rc);
2533 }
2534
2535 int pcc_readwrite_attach_fini(struct file *file, struct inode *inode,
2536                               __u32 gen, bool lease_broken, int rc,
2537                               bool attached)
2538 {
2539         struct ll_inode_info *lli = ll_i2info(inode);
2540         const struct cred *old_cred;
2541         struct pcc_inode *pcci;
2542         __u32 gen2;
2543
2544         ENTRY;
2545
2546         old_cred = override_creds(pcc_super_cred(inode->i_sb));
2547         pcc_inode_lock(inode);
2548         pcci = ll_i2pcci(inode);
2549         if (rc || lease_broken) {
2550                 if (attached && pcci)
2551                         pcc_inode_put(pcci);
2552
2553                 GOTO(out_unlock, rc);
2554         }
2555
2556         /* PCC inode may be released due to layout lock revocatioin */
2557         if (!pcci)
2558                 GOTO(out_unlock, rc = -ESTALE);
2559
2560         LASSERT(attached);
2561         rc = pcc_layout_xattr_set(pcci, gen);
2562         if (rc)
2563                 GOTO(out_put, rc);
2564
2565         LASSERT(lli->lli_pcc_state & PCC_STATE_FL_ATTACHING);
2566         rc = ll_layout_refresh(inode, &gen2);
2567         if (!rc) {
2568                 if (gen2 == gen) {
2569                         pcc_layout_gen_set(pcci, gen);
2570                 } else {
2571                         CDEBUG(D_CACHE,
2572                                DFID" layout changed from %d to %d.\n",
2573                                PFID(ll_inode2fid(inode)), gen, gen2);
2574                         GOTO(out_put, rc = -ESTALE);
2575                 }
2576         }
2577
2578 out_put:
2579         if (rc) {
2580                 (void) pcc_inode_remove(inode, pcci->pcci_path.dentry);
2581                 pcc_inode_put(pcci);
2582         }
2583 out_unlock:
2584         lli->lli_pcc_state &= ~PCC_STATE_FL_ATTACHING;
2585         pcc_inode_unlock(inode);
2586         revert_creds(old_cred);
2587         RETURN(rc);
2588 }
2589
2590 static int pcc_hsm_remove(struct inode *inode)
2591 {
2592         struct hsm_user_request *hur;
2593         __u32 gen;
2594         int len;
2595         int rc;
2596
2597         ENTRY;
2598
2599         rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
2600         if (rc) {
2601                 CDEBUG(D_CACHE, DFID" RESTORE failure: %d\n",
2602                        PFID(&ll_i2info(inode)->lli_fid), rc);
2603                 RETURN(rc);
2604         }
2605
2606         ll_layout_refresh(inode, &gen);
2607
2608         len = sizeof(struct hsm_user_request) +
2609               sizeof(struct hsm_user_item);
2610         OBD_ALLOC(hur, len);
2611         if (hur == NULL)
2612                 RETURN(-ENOMEM);
2613
2614         hur->hur_request.hr_action = HUA_REMOVE;
2615         hur->hur_request.hr_archive_id = 0;
2616         hur->hur_request.hr_flags = 0;
2617         memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
2618                sizeof(hur->hur_user_item[0].hui_fid));
2619         hur->hur_user_item[0].hui_extent.offset = 0;
2620         hur->hur_user_item[0].hui_extent.length = OBD_OBJECT_EOF;
2621         hur->hur_request.hr_itemcount = 1;
2622         rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,
2623                            len, hur, NULL);
2624         if (rc)
2625                 CDEBUG(D_CACHE, DFID" HSM REMOVE failure: %d\n",
2626                        PFID(&ll_i2info(inode)->lli_fid), rc);
2627
2628         OBD_FREE(hur, len);
2629         RETURN(rc);
2630 }
2631
2632 int pcc_ioctl_detach(struct inode *inode, __u32 opt)
2633 {
2634         struct ll_inode_info *lli = ll_i2info(inode);
2635         struct pcc_inode *pcci;
2636         bool hsm_remove = false;
2637         int rc = 0;
2638
2639         ENTRY;
2640
2641         pcc_inode_lock(inode);
2642         pcci = lli->lli_pcc_inode;
2643         if (!pcci || lli->lli_pcc_state & PCC_STATE_FL_ATTACHING ||
2644             !pcc_inode_has_layout(pcci))
2645                 GOTO(out_unlock, rc = 0);
2646
2647         LASSERT(atomic_read(&pcci->pcci_refcount) > 0);
2648
2649         if (pcci->pcci_type == LU_PCC_READWRITE) {
2650                 if (opt == PCC_DETACH_OPT_UNCACHE) {
2651                         hsm_remove = true;
2652                         /*
2653                          * The file will be removed from PCC, set the flags
2654                          * with PCC_DATASET_NONE even the later removal of the
2655                          * PCC copy fails.
2656                          */
2657                         lli->lli_pcc_dsflags = PCC_DATASET_NONE;
2658                 }
2659
2660                 __pcc_layout_invalidate(pcci);
2661                 pcc_inode_put(pcci);
2662         }
2663
2664 out_unlock:
2665         pcc_inode_unlock(inode);
2666         if (hsm_remove) {
2667                 const struct cred *old_cred;
2668
2669                 old_cred = override_creds(pcc_super_cred(inode->i_sb));
2670                 rc = pcc_hsm_remove(inode);
2671                 revert_creds(old_cred);
2672         }
2673
2674         RETURN(rc);
2675 }
2676
2677 int pcc_ioctl_state(struct file *file, struct inode *inode,
2678                     struct lu_pcc_state *state)
2679 {
2680         int rc = 0;
2681         int count;
2682         char *buf;
2683         char *path;
2684         int buf_len = sizeof(state->pccs_path);
2685         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2686         struct pcc_file *pccf = &fd->fd_pcc_file;
2687         struct pcc_inode *pcci;
2688
2689         ENTRY;
2690
2691         if (buf_len <= 0)
2692                 RETURN(-EINVAL);
2693
2694         OBD_ALLOC(buf, buf_len);
2695         if (buf == NULL)
2696                 RETURN(-ENOMEM);
2697
2698         pcc_inode_lock(inode);
2699         pcci = ll_i2pcci(inode);
2700         if (pcci == NULL) {
2701                 state->pccs_type = LU_PCC_NONE;
2702                 GOTO(out_unlock, rc = 0);
2703         }
2704
2705         count = atomic_read(&pcci->pcci_refcount);
2706         if (count == 0) {
2707                 state->pccs_type = LU_PCC_NONE;
2708                 state->pccs_open_count = 0;
2709                 GOTO(out_unlock, rc = 0);
2710         }
2711
2712         if (pcc_inode_has_layout(pcci))
2713                 count--;
2714         if (pccf->pccf_file != NULL)
2715                 count--;
2716         state->pccs_type = pcci->pcci_type;
2717         state->pccs_open_count = count;
2718         state->pccs_flags = ll_i2info(inode)->lli_pcc_state;
2719         path = dentry_path_raw(pcci->pcci_path.dentry, buf, buf_len);
2720         if (IS_ERR(path))
2721                 GOTO(out_unlock, rc = PTR_ERR(path));
2722
2723         if (strlcpy(state->pccs_path, path, buf_len) >= buf_len)
2724                 GOTO(out_unlock, rc = -ENAMETOOLONG);
2725
2726 out_unlock:
2727         pcc_inode_unlock(inode);
2728         OBD_FREE(buf, buf_len);
2729         RETURN(rc);
2730 }