4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2017, DDN Storage Corporation.
26 * Persistent Client Cache
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.
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.
45 * Following is what will happen in different conditions for RW-PCC:
47 * > When file is being created on RW-PCC
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.
54 * > When file is being prefetched to RW-PCC
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.
60 * > When file is being accessed from PCC
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.
66 * > When PCC cached file is being accessed on another client
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
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
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.
85 * The main advantages to use this SSD cache on the Lustre clients via PCC
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.
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
101 * Author: Li Xi <lixi@ddn.com>
102 * Author: Qian Yingjin <qian@ddn.com>
105 #define DEBUG_SUBSYSTEM S_LLITE
108 #include <linux/namei.h>
109 #include <linux/file.h>
110 #include <lustre_compat.h>
111 #include "llite_internal.h"
113 struct kmem_cache *pcc_inode_slab;
115 int pcc_super_init(struct pcc_super *super)
119 super->pccs_cred = cred = prepare_creds();
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;
132 /* Rule based auto caching */
133 static void pcc_id_list_free(struct list_head *id_list)
135 struct pcc_match_id *id, *n;
137 list_for_each_entry_safe(id, n, id_list, pmi_linkage) {
138 list_del_init(&id->pmi_linkage);
143 static void pcc_fname_list_free(struct list_head *fname_list)
145 struct pcc_match_fname *fname, *n;
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);
154 static void pcc_expression_free(struct pcc_expression *expr)
156 LASSERT(expr->pe_field >= PCC_FIELD_UID &&
157 expr->pe_field < PCC_FIELD_MAX);
158 switch (expr->pe_field) {
161 case PCC_FIELD_PROJID:
162 pcc_id_list_free(&expr->pe_cond);
164 case PCC_FIELD_FNAME:
165 pcc_fname_list_free(&expr->pe_cond);
173 static void pcc_conjunction_free(struct pcc_conjunction *conjunction)
175 struct pcc_expression *expression, *n;
177 LASSERT(list_empty(&conjunction->pc_linkage));
178 list_for_each_entry_safe(expression, n,
179 &conjunction->pc_expressions,
181 list_del_init(&expression->pe_linkage);
182 pcc_expression_free(expression);
184 OBD_FREE_PTR(conjunction);
187 static void pcc_rule_conds_free(struct list_head *cond_list)
189 struct pcc_conjunction *conjunction, *n;
191 list_for_each_entry_safe(conjunction, n, cond_list, pc_linkage) {
192 list_del_init(&conjunction->pc_linkage);
193 pcc_conjunction_free(conjunction);
197 static void pcc_cmd_fini(struct pcc_cmd *cmd)
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);
208 #define PCC_DISJUNCTION_DELIM (",")
209 #define PCC_CONJUNCTION_DELIM ("&")
210 #define PCC_EXPRESSION_DELIM ("=")
213 pcc_fname_list_add(char *id, struct list_head *fname_list)
215 struct pcc_match_fname *fname;
217 OBD_ALLOC_PTR(fname);
221 OBD_ALLOC(fname->pmf_name, strlen(id) + 1);
222 if (fname->pmf_name == NULL) {
227 strcpy(fname->pmf_name, id);
228 list_add_tail(&fname->pmf_linkage, fname_list);
233 pcc_fname_list_parse(char *str, struct list_head *fname_list)
239 INIT_LIST_HEAD(fname_list);
240 while (rc == 0 && str) {
241 char *fname = strsep(&str, " ");
244 rc = pcc_fname_list_add(fname, fname_list);
246 if (list_empty(fname_list))
249 pcc_fname_list_free(fname_list);
254 pcc_id_list_parse(char *str, struct list_head *id_list,
261 if (type != PCC_FIELD_UID && type != PCC_FIELD_GID &&
262 type != PCC_FIELD_PROJID)
265 INIT_LIST_HEAD(id_list);
268 struct pcc_match_id *id;
269 unsigned long id_val;
271 num = strsep(&str, " ");
274 rc = kstrtoul(num, 0, &id_val);
280 GOTO(out, rc = -ENOMEM);
283 list_add_tail(&id->pmi_linkage, id_list);
285 if (list_empty(id_list))
289 pcc_id_list_free(id_list);
294 pcc_expression_parse(char *str, struct list_head *cond_list)
296 struct pcc_expression *expr;
305 field = strim(strsep(&str, PCC_EXPRESSION_DELIM));
307 /* No LHS or no '=' */
308 GOTO(out, rc = -EINVAL);
309 str = skip_spaces(str);
311 if (str[0] != '{' || str[len - 1] != '}')
312 GOTO(out, rc = -EINVAL);
314 /* Skip '{' and '}' */
318 if (strcmp(field, "uid") == 0) {
319 if (pcc_id_list_parse(str,
322 GOTO(out, rc = -EINVAL);
323 expr->pe_field = PCC_FIELD_UID;
324 } else if (strcmp(field, "gid") == 0) {
325 if (pcc_id_list_parse(str,
328 GOTO(out, rc = -EINVAL);
329 expr->pe_field = PCC_FIELD_GID;
330 } else if (strcmp(field, "projid") == 0) {
331 if (pcc_id_list_parse(str,
333 PCC_FIELD_PROJID) < 0)
334 GOTO(out, rc = -EINVAL);
335 expr->pe_field = PCC_FIELD_PROJID;
336 } else if (strcmp(field, "fname") == 0) {
337 if (pcc_fname_list_parse(str, &expr->pe_cond) < 0)
338 GOTO(out, rc = -EINVAL);
339 expr->pe_field = PCC_FIELD_FNAME;
341 GOTO(out, rc = -EINVAL);
344 list_add_tail(&expr->pe_linkage, cond_list);
352 pcc_conjunction_parse(char *str, struct list_head *cond_list)
354 struct pcc_conjunction *conjunction;
357 OBD_ALLOC_PTR(conjunction);
358 if (conjunction == NULL)
361 INIT_LIST_HEAD(&conjunction->pc_expressions);
362 list_add_tail(&conjunction->pc_linkage, cond_list);
364 while (rc == 0 && str) {
365 char *expr = strsep(&str, PCC_CONJUNCTION_DELIM);
367 rc = pcc_expression_parse(expr, &conjunction->pc_expressions);
372 static int pcc_conds_parse(char *orig, struct list_head *cond_list)
377 orig = kstrdup(orig, GFP_KERNEL);
382 INIT_LIST_HEAD(cond_list);
383 while (rc == 0 && str) {
384 char *term = strsep(&str, PCC_DISJUNCTION_DELIM);
386 rc = pcc_conjunction_parse(term, cond_list);
392 static int pcc_id_parse(struct pcc_cmd *cmd, const char *id)
396 OBD_ALLOC(cmd->u.pccc_add.pccc_conds_str, strlen(id) + 1);
397 if (cmd->u.pccc_add.pccc_conds_str == NULL)
400 memcpy(cmd->u.pccc_add.pccc_conds_str, id, strlen(id));
402 rc = pcc_conds_parse(cmd->u.pccc_add.pccc_conds_str,
403 &cmd->u.pccc_add.pccc_conds);
411 pcc_parse_value_pair(struct pcc_cmd *cmd, char *buffer)
418 key = strsep(&val, "=");
419 if (val == NULL || strlen(val) == 0)
422 /* Key of the value pair */
423 if (strcmp(key, "rwid") == 0) {
424 rc = kstrtoul(val, 10, &id);
429 cmd->u.pccc_add.pccc_rwid = id;
430 } else if (strcmp(key, "roid") == 0) {
431 rc = kstrtoul(val, 10, &id);
436 cmd->u.pccc_add.pccc_roid = id;
437 } else if (strcmp(key, "auto_attach") == 0) {
438 rc = kstrtoul(val, 10, &id);
442 cmd->u.pccc_add.pccc_flags &= ~PCC_DATASET_AUTO_ATTACH;
443 } else if (strcmp(key, "open_attach") == 0) {
444 rc = kstrtoul(val, 10, &id);
448 cmd->u.pccc_add.pccc_flags &= ~PCC_DATASET_OPEN_ATTACH;
449 } else if (strcmp(key, "io_attach") == 0) {
450 rc = kstrtoul(val, 10, &id);
454 cmd->u.pccc_add.pccc_flags &= ~PCC_DATASET_IO_ATTACH;
455 } else if (strcmp(key, "stat_attach") == 0) {
456 rc = kstrtoul(val, 10, &id);
460 cmd->u.pccc_add.pccc_flags &= ~PCC_DATASET_STAT_ATTACH;
461 } else if (strcmp(key, "rwpcc") == 0) {
462 rc = kstrtoul(val, 10, &id);
466 cmd->u.pccc_add.pccc_flags |= PCC_DATASET_RWPCC;
467 } else if (strcmp(key, "ropcc") == 0) {
468 rc = kstrtoul(val, 10, &id);
472 cmd->u.pccc_add.pccc_flags |= PCC_DATASET_ROPCC;
481 pcc_parse_value_pairs(struct pcc_cmd *cmd, char *buffer)
487 switch (cmd->pccc_cmd) {
488 case PCC_ADD_DATASET:
489 /* Enable auto attach by default */
490 cmd->u.pccc_add.pccc_flags |= PCC_DATASET_AUTO_ATTACH;
492 case PCC_DEL_DATASET:
500 while (val != NULL && strlen(val) != 0) {
501 token = strsep(&val, " ");
502 rc = pcc_parse_value_pair(cmd, token);
507 switch (cmd->pccc_cmd) {
508 case PCC_ADD_DATASET:
509 if (cmd->u.pccc_add.pccc_flags & PCC_DATASET_RWPCC &&
510 cmd->u.pccc_add.pccc_flags & PCC_DATASET_ROPCC)
513 * By default, a PCC backend can provide caching service for
514 * both PCC-RW and PCC-RO.
516 if ((cmd->u.pccc_add.pccc_flags & PCC_DATASET_PCC_ALL) == 0)
517 cmd->u.pccc_add.pccc_flags |= PCC_DATASET_PCC_ALL;
519 if (cmd->u.pccc_add.pccc_rwid == 0 &&
520 cmd->u.pccc_add.pccc_roid == 0)
523 if (cmd->u.pccc_add.pccc_rwid == 0 &&
524 cmd->u.pccc_add.pccc_flags & PCC_DATASET_RWPCC)
525 cmd->u.pccc_add.pccc_rwid = cmd->u.pccc_add.pccc_roid;
527 if (cmd->u.pccc_add.pccc_roid == 0 &&
528 cmd->u.pccc_add.pccc_flags & PCC_DATASET_ROPCC)
529 cmd->u.pccc_add.pccc_roid = cmd->u.pccc_add.pccc_rwid;
532 case PCC_DEL_DATASET:
542 pcc_dataset_rule_fini(struct pcc_match_rule *rule)
544 if (!list_empty(&rule->pmr_conds))
545 pcc_rule_conds_free(&rule->pmr_conds);
546 LASSERT(rule->pmr_conds_str != NULL);
547 OBD_FREE(rule->pmr_conds_str, strlen(rule->pmr_conds_str) + 1);
551 pcc_dataset_rule_init(struct pcc_match_rule *rule, struct pcc_cmd *cmd)
555 LASSERT(cmd->u.pccc_add.pccc_conds_str);
556 OBD_ALLOC(rule->pmr_conds_str,
557 strlen(cmd->u.pccc_add.pccc_conds_str) + 1);
558 if (rule->pmr_conds_str == NULL)
561 memcpy(rule->pmr_conds_str,
562 cmd->u.pccc_add.pccc_conds_str,
563 strlen(cmd->u.pccc_add.pccc_conds_str));
565 INIT_LIST_HEAD(&rule->pmr_conds);
566 if (!list_empty(&cmd->u.pccc_add.pccc_conds))
567 rc = pcc_conds_parse(rule->pmr_conds_str,
571 pcc_dataset_rule_fini(rule);
578 pcc_id_list_match(struct list_head *id_list, __u32 id_val)
580 struct pcc_match_id *id;
582 list_for_each_entry(id, id_list, pmi_linkage) {
583 if (id->pmi_id == id_val)
590 cfs_match_wildcard(const char *pattern, const char *content)
592 if (*pattern == '\0' && *content == '\0')
595 if (*pattern == '*' && *(pattern + 1) != '\0' && *content == '\0')
598 while (*pattern == *content) {
601 if (*pattern == '\0' && *content == '\0')
604 if (*pattern == '*' && *(pattern + 1) != '\0' &&
610 return (cfs_match_wildcard(pattern + 1, content) ||
611 cfs_match_wildcard(pattern, content + 1));
617 pcc_fname_list_match(struct list_head *fname_list, const char *name)
619 struct pcc_match_fname *fname;
621 list_for_each_entry(fname, fname_list, pmf_linkage) {
622 if (cfs_match_wildcard(fname->pmf_name, name))
629 pcc_expression_match(struct pcc_expression *expr, struct pcc_matcher *matcher)
631 switch (expr->pe_field) {
633 return pcc_id_list_match(&expr->pe_cond, matcher->pm_uid);
635 return pcc_id_list_match(&expr->pe_cond, matcher->pm_gid);
636 case PCC_FIELD_PROJID:
637 return pcc_id_list_match(&expr->pe_cond, matcher->pm_projid);
638 case PCC_FIELD_FNAME:
639 return pcc_fname_list_match(&expr->pe_cond,
640 matcher->pm_name->name);
647 pcc_conjunction_match(struct pcc_conjunction *conjunction,
648 struct pcc_matcher *matcher)
650 struct pcc_expression *expr;
653 list_for_each_entry(expr, &conjunction->pc_expressions, pe_linkage) {
654 matched = pcc_expression_match(expr, matcher);
663 pcc_cond_match(struct pcc_match_rule *rule, struct pcc_matcher *matcher)
665 struct pcc_conjunction *conjunction;
668 list_for_each_entry(conjunction, &rule->pmr_conds, pc_linkage) {
669 matched = pcc_conjunction_match(conjunction, matcher);
678 pcc_dataset_match_get(struct pcc_super *super, struct pcc_matcher *matcher)
680 struct pcc_dataset *dataset;
681 struct pcc_dataset *selected = NULL;
683 down_read(&super->pccs_rw_sem);
684 list_for_each_entry(dataset, &super->pccs_datasets, pccd_linkage) {
685 if (!(dataset->pccd_flags & PCC_DATASET_RWPCC))
688 if (pcc_cond_match(&dataset->pccd_rule, matcher)) {
689 atomic_inc(&dataset->pccd_refcount);
694 up_read(&super->pccs_rw_sem);
696 CDEBUG(D_CACHE, "PCC create, matched %s - %d:%d:%d:%s\n",
697 dataset->pccd_rule.pmr_conds_str,
698 matcher->pm_uid, matcher->pm_gid,
699 matcher->pm_projid, matcher->pm_name->name);
705 * pcc_dataset_add - Add a Cache policy to control which files need be
706 * cached and where it will be cached.
708 * @super: superblock of pcc
712 pcc_dataset_add(struct pcc_super *super, struct pcc_cmd *cmd)
714 char *pathname = cmd->pccc_pathname;
715 struct pcc_dataset *dataset;
716 struct pcc_dataset *tmp;
720 OBD_ALLOC_PTR(dataset);
724 rc = kern_path(pathname, LOOKUP_DIRECTORY, &dataset->pccd_path);
726 OBD_FREE_PTR(dataset);
729 strncpy(dataset->pccd_pathname, pathname, PATH_MAX);
730 dataset->pccd_rwid = cmd->u.pccc_add.pccc_rwid;
731 dataset->pccd_roid = cmd->u.pccc_add.pccc_roid;
732 dataset->pccd_flags = cmd->u.pccc_add.pccc_flags;
733 atomic_set(&dataset->pccd_refcount, 1);
735 rc = pcc_dataset_rule_init(&dataset->pccd_rule, cmd);
737 pcc_dataset_put(dataset);
741 down_write(&super->pccs_rw_sem);
742 list_for_each_entry(tmp, &super->pccs_datasets, pccd_linkage) {
743 if (strcmp(tmp->pccd_pathname, pathname) == 0 ||
744 (dataset->pccd_rwid != 0 &&
745 dataset->pccd_rwid == tmp->pccd_rwid) ||
746 (dataset->pccd_roid != 0 &&
747 dataset->pccd_roid == tmp->pccd_roid)) {
753 list_add(&dataset->pccd_linkage, &super->pccs_datasets);
754 up_write(&super->pccs_rw_sem);
757 pcc_dataset_put(dataset);
764 static struct pcc_dataset *
765 pcc_dataset_get(struct pcc_super *super, enum lu_pcc_type type, __u32 id)
767 struct pcc_dataset *dataset;
768 struct pcc_dataset *selected = NULL;
774 * archive ID (read-write ID) or read-only ID is unique in the list,
775 * we just return last added one as first priority.
777 down_read(&super->pccs_rw_sem);
778 list_for_each_entry(dataset, &super->pccs_datasets, pccd_linkage) {
779 if (type == LU_PCC_READWRITE && (dataset->pccd_rwid != id ||
780 !(dataset->pccd_flags & PCC_DATASET_RWPCC)))
782 if (type == LU_PCC_READONLY && (dataset->pccd_roid != id ||
783 !(dataset->pccd_flags & PCC_DATASET_ROPCC)))
785 atomic_inc(&dataset->pccd_refcount);
789 up_read(&super->pccs_rw_sem);
791 CDEBUG(D_CACHE, "matched id %u, PCC mode %d\n", id, type);
797 pcc_dataset_put(struct pcc_dataset *dataset)
799 if (atomic_dec_and_test(&dataset->pccd_refcount)) {
800 pcc_dataset_rule_fini(&dataset->pccd_rule);
801 path_put(&dataset->pccd_path);
802 OBD_FREE_PTR(dataset);
807 pcc_dataset_del(struct pcc_super *super, char *pathname)
809 struct list_head *l, *tmp;
810 struct pcc_dataset *dataset;
813 down_write(&super->pccs_rw_sem);
814 list_for_each_safe(l, tmp, &super->pccs_datasets) {
815 dataset = list_entry(l, struct pcc_dataset, pccd_linkage);
816 if (strcmp(dataset->pccd_pathname, pathname) == 0) {
817 list_del_init(&dataset->pccd_linkage);
818 pcc_dataset_put(dataset);
819 super->pccs_generation++;
824 up_write(&super->pccs_rw_sem);
829 pcc_dataset_dump(struct pcc_dataset *dataset, struct seq_file *m)
831 seq_printf(m, "%s:\n", dataset->pccd_pathname);
832 seq_printf(m, " rwid: %u\n", dataset->pccd_rwid);
833 seq_printf(m, " flags: %x\n", dataset->pccd_flags);
834 seq_printf(m, " autocache: %s\n", dataset->pccd_rule.pmr_conds_str);
838 pcc_super_dump(struct pcc_super *super, struct seq_file *m)
840 struct pcc_dataset *dataset;
842 down_read(&super->pccs_rw_sem);
843 list_for_each_entry(dataset, &super->pccs_datasets, pccd_linkage) {
844 pcc_dataset_dump(dataset, m);
846 up_read(&super->pccs_rw_sem);
850 static void pcc_remove_datasets(struct pcc_super *super)
852 struct pcc_dataset *dataset, *tmp;
854 down_write(&super->pccs_rw_sem);
855 list_for_each_entry_safe(dataset, tmp,
856 &super->pccs_datasets, pccd_linkage) {
857 list_del(&dataset->pccd_linkage);
858 pcc_dataset_put(dataset);
860 super->pccs_generation++;
861 up_write(&super->pccs_rw_sem);
864 void pcc_super_fini(struct pcc_super *super)
866 pcc_remove_datasets(super);
867 put_cred(super->pccs_cred);
870 static bool pathname_is_valid(const char *pathname)
872 /* Needs to be absolute path */
873 if (pathname == NULL || strlen(pathname) == 0 ||
874 strlen(pathname) >= PATH_MAX || pathname[0] != '/')
879 static struct pcc_cmd *
880 pcc_cmd_parse(char *buffer, unsigned long count)
882 static struct pcc_cmd *cmd;
889 GOTO(out, rc = -ENOMEM);
891 /* clear all setting */
892 if (strncmp(buffer, "clear", 5) == 0) {
893 cmd->pccc_cmd = PCC_CLEAR_ALL;
898 token = strsep(&val, " ");
899 if (val == NULL || strlen(val) == 0)
900 GOTO(out_free_cmd, rc = -EINVAL);
902 /* Type of the command */
903 if (strcmp(token, "add") == 0)
904 cmd->pccc_cmd = PCC_ADD_DATASET;
905 else if (strcmp(token, "del") == 0)
906 cmd->pccc_cmd = PCC_DEL_DATASET;
908 GOTO(out_free_cmd, rc = -EINVAL);
910 /* Pathname of the dataset */
911 token = strsep(&val, " ");
912 if ((val == NULL && cmd->pccc_cmd != PCC_DEL_DATASET) ||
913 !pathname_is_valid(token))
914 GOTO(out_free_cmd, rc = -EINVAL);
915 cmd->pccc_pathname = token;
917 if (cmd->pccc_cmd == PCC_ADD_DATASET) {
921 val = strrchr(token, '}');
923 GOTO(out_free_cmd, rc = -EINVAL);
929 } else if (*val == ' ') {
933 GOTO(out_free_cmd, rc = -EINVAL);
936 rc = pcc_id_parse(cmd, token);
938 GOTO(out_free_cmd, rc);
940 rc = pcc_parse_value_pairs(cmd, val);
942 GOTO(out_cmd_fini, rc = -EINVAL);
955 int pcc_cmd_handle(char *buffer, unsigned long count,
956 struct pcc_super *super)
961 cmd = pcc_cmd_parse(buffer, count);
965 switch (cmd->pccc_cmd) {
966 case PCC_ADD_DATASET:
967 rc = pcc_dataset_add(super, cmd);
969 case PCC_DEL_DATASET:
970 rc = pcc_dataset_del(super, cmd->pccc_pathname);
973 pcc_remove_datasets(super);
985 static inline void pcc_inode_lock(struct inode *inode)
987 mutex_lock(&ll_i2info(inode)->lli_pcc_lock);
990 static inline void pcc_inode_unlock(struct inode *inode)
992 mutex_unlock(&ll_i2info(inode)->lli_pcc_lock);
995 static void pcc_inode_init(struct pcc_inode *pcci, struct ll_inode_info *lli)
997 pcci->pcci_lli = lli;
998 lli->lli_pcc_inode = pcci;
999 atomic_set(&pcci->pcci_refcount, 0);
1000 pcci->pcci_type = LU_PCC_NONE;
1001 pcci->pcci_layout_gen = CL_LAYOUT_GEN_NONE;
1002 atomic_set(&pcci->pcci_active_ios, 0);
1003 init_waitqueue_head(&pcci->pcci_waitq);
1006 static void pcc_inode_fini(struct pcc_inode *pcci)
1008 struct ll_inode_info *lli = pcci->pcci_lli;
1010 path_put(&pcci->pcci_path);
1011 pcci->pcci_type = LU_PCC_NONE;
1012 OBD_SLAB_FREE_PTR(pcci, pcc_inode_slab);
1013 lli->lli_pcc_inode = NULL;
1016 static void pcc_inode_get(struct pcc_inode *pcci)
1018 atomic_inc(&pcci->pcci_refcount);
1021 static void pcc_inode_put(struct pcc_inode *pcci)
1023 if (atomic_dec_and_test(&pcci->pcci_refcount))
1024 pcc_inode_fini(pcci);
1027 void pcc_inode_free(struct inode *inode)
1029 struct pcc_inode *pcci = ll_i2pcci(inode);
1032 WARN_ON(atomic_read(&pcci->pcci_refcount) > 1);
1033 pcc_inode_put(pcci);
1039 * As Andreas suggested, we'd better use new layout to
1041 * (fid->f_oid >> 16 & oxFFFF)/FID
1043 #define PCC_DATASET_MAX_PATH (6 * 5 + FID_NOBRACE_LEN + 1)
1044 static int pcc_fid2dataset_path(char *buf, int sz, struct lu_fid *fid)
1046 return scnprintf(buf, sz, "%04x/%04x/%04x/%04x/%04x/%04x/"
1048 (fid)->f_oid & 0xFFFF,
1049 (fid)->f_oid >> 16 & 0xFFFF,
1050 (unsigned int)((fid)->f_seq & 0xFFFF),
1051 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
1052 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
1053 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
1057 static inline const struct cred *pcc_super_cred(struct super_block *sb)
1059 return ll_s2sbi(sb)->ll_pcc_super.pccs_cred;
1062 void pcc_file_init(struct pcc_file *pccf)
1064 pccf->pccf_file = NULL;
1065 pccf->pccf_type = LU_PCC_NONE;
1068 static inline bool pcc_auto_attach_enabled(enum pcc_dataset_flags flags,
1069 enum pcc_io_type iot)
1071 if (iot == PIT_OPEN)
1072 return flags & PCC_DATASET_OPEN_ATTACH;
1073 if (iot == PIT_GETATTR)
1074 return flags & PCC_DATASET_STAT_ATTACH;
1076 return flags & PCC_DATASET_AUTO_ATTACH;
1079 static const char pcc_xattr_layout[] = XATTR_USER_PREFIX "PCC.layout";
1081 static int pcc_layout_xattr_set(struct pcc_inode *pcci, __u32 gen)
1083 struct dentry *pcc_dentry = pcci->pcci_path.dentry;
1084 struct ll_inode_info *lli = pcci->pcci_lli;
1089 if (!(lli->lli_pcc_dsflags & PCC_DATASET_AUTO_ATTACH))
1092 rc = ll_vfs_setxattr(pcc_dentry, pcc_dentry->d_inode, pcc_xattr_layout,
1093 &gen, sizeof(gen), 0);
1098 static int pcc_get_layout_info(struct inode *inode, struct cl_layout *clt)
1101 struct ll_inode_info *lli = ll_i2info(inode);
1110 env = cl_env_get(&refcheck);
1112 RETURN(PTR_ERR(env));
1114 rc = cl_object_layout_get(env, lli->lli_clob, clt);
1116 CDEBUG(D_INODE, "Cannot get layout for "DFID"\n",
1117 PFID(ll_inode2fid(inode)));
1119 cl_env_put(env, &refcheck);
1120 RETURN(rc < 0 ? rc : 0);
1123 /* Must be called with pcci->pcci_lock held */
1124 static void pcc_inode_attach_init(struct pcc_dataset *dataset,
1125 struct pcc_inode *pcci,
1126 struct dentry *dentry,
1127 enum lu_pcc_type type)
1129 pcci->pcci_path.mnt = mntget(dataset->pccd_path.mnt);
1130 pcci->pcci_path.dentry = dentry;
1131 LASSERT(atomic_read(&pcci->pcci_refcount) == 0);
1132 atomic_set(&pcci->pcci_refcount, 1);
1133 pcci->pcci_type = type;
1134 pcci->pcci_attr_valid = false;
1137 static inline void pcc_inode_dsflags_set(struct ll_inode_info *lli,
1138 struct pcc_dataset *dataset)
1140 lli->lli_pcc_generation = ll_info2pccs(lli)->pccs_generation;
1141 lli->lli_pcc_dsflags = dataset->pccd_flags;
1144 static void pcc_inode_attach_set(struct pcc_super *super,
1145 struct pcc_dataset *dataset,
1146 struct ll_inode_info *lli,
1147 struct pcc_inode *pcci,
1148 struct dentry *dentry,
1149 enum lu_pcc_type type)
1151 pcc_inode_init(pcci, lli);
1152 pcc_inode_attach_init(dataset, pcci, dentry, type);
1153 down_read(&super->pccs_rw_sem);
1154 pcc_inode_dsflags_set(lli, dataset);
1155 up_read(&super->pccs_rw_sem);
1158 static inline void pcc_layout_gen_set(struct pcc_inode *pcci,
1161 pcci->pcci_layout_gen = gen;
1164 static inline bool pcc_inode_has_layout(struct pcc_inode *pcci)
1166 return pcci->pcci_layout_gen != CL_LAYOUT_GEN_NONE;
1169 static struct dentry *pcc_lookup(struct dentry *base, char *pathname)
1171 char *ptr = NULL, *component;
1172 struct dentry *parent;
1173 struct dentry *child = ERR_PTR(-ENOENT);
1177 /* move past any initial '/' to the start of the first path component*/
1181 /* store the start of the first path component */
1184 parent = dget(base);
1186 /* find the start of the next component - if we don't find it,
1187 * the current component is the last component
1189 ptr = strchr(ptr, '/');
1190 /* put a NUL char in place of the '/' before the next compnent
1191 * so we can treat this component as a string; note the full
1192 * path string is NUL terminated to this is not needed for the
1198 /* look up the current component */
1199 inode_lock(parent->d_inode);
1200 child = lookup_one_len(component, parent, strlen(component));
1201 inode_unlock(parent->d_inode);
1203 /* repair the path string: put '/' back in place of the NUL */
1209 if (IS_ERR_OR_NULL(child))
1212 /* we may find a cached negative dentry */
1213 if (!d_is_positive(child)) {
1219 /* descend in to the next level of the path */
1222 /* move the pointer past the '/' to the next component */
1228 /* NULL child means we didn't find anything */
1230 child = ERR_PTR(-ENOENT);
1235 static int pcc_try_dataset_attach(struct inode *inode, __u32 gen,
1236 enum lu_pcc_type type,
1237 struct pcc_dataset *dataset,
1240 struct ll_inode_info *lli = ll_i2info(inode);
1241 struct pcc_inode *pcci = lli->lli_pcc_inode;
1242 const struct cred *old_cred;
1243 struct dentry *pcc_dentry = NULL;
1244 char pathname[PCC_DATASET_MAX_PATH];
1250 if (type == LU_PCC_READWRITE &&
1251 !(dataset->pccd_flags & PCC_DATASET_RWPCC))
1254 if (type == LU_PCC_READONLY &&
1255 !(dataset->pccd_flags & PCC_DATASET_ROPCC))
1258 rc = pcc_fid2dataset_path(pathname, PCC_DATASET_MAX_PATH,
1261 old_cred = override_creds(pcc_super_cred(inode->i_sb));
1262 pcc_dentry = pcc_lookup(dataset->pccd_path.dentry, pathname);
1263 if (IS_ERR(pcc_dentry)) {
1264 rc = PTR_ERR(pcc_dentry);
1265 CDEBUG(D_CACHE, "%s: path lookup error on "DFID":%s: rc = %d\n",
1266 ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid),
1268 /* ignore this error */
1272 rc = ll_vfs_getxattr(pcc_dentry, pcc_dentry->d_inode, pcc_xattr_layout,
1273 &pcc_gen, sizeof(pcc_gen));
1275 /* ignore this error */
1276 GOTO(out_put_pcc_dentry, rc = 0);
1279 /* The file is still valid cached in PCC, attach it immediately. */
1280 if (pcc_gen == gen) {
1281 CDEBUG(D_CACHE, DFID" L.Gen (%d) consistent, auto attached.\n",
1282 PFID(&lli->lli_fid), gen);
1284 OBD_SLAB_ALLOC_PTR_GFP(pcci, pcc_inode_slab, GFP_NOFS);
1286 GOTO(out_put_pcc_dentry, rc = -ENOMEM);
1288 pcc_inode_init(pcci, lli);
1290 pcc_inode_attach_init(dataset, pcci, pcc_dentry, type);
1293 * This happened when a file was once attached into
1294 * PCC, and some processes keep this file opened
1295 * (pcci->refcount > 1) and corresponding PCC file
1296 * without any I/O activity, and then this file was
1297 * detached by the manual detach command or the
1298 * revocation of the layout lock (i.e. cached LRU lock
1301 pcc_inode_get(pcci);
1302 pcci->pcci_type = type;
1304 pcc_inode_dsflags_set(lli, dataset);
1305 pcc_layout_gen_set(pcci, gen);
1311 revert_creds(old_cred);
1315 static int pcc_try_datasets_attach(struct inode *inode, enum pcc_io_type iot,
1316 __u32 gen, enum lu_pcc_type type,
1319 struct pcc_super *super = &ll_i2sbi(inode)->ll_pcc_super;
1320 struct ll_inode_info *lli = ll_i2info(inode);
1321 struct pcc_dataset *dataset = NULL, *tmp;
1326 down_read(&super->pccs_rw_sem);
1327 list_for_each_entry_safe(dataset, tmp,
1328 &super->pccs_datasets, pccd_linkage) {
1329 if (!pcc_auto_attach_enabled(dataset->pccd_flags, iot))
1332 rc = pcc_try_dataset_attach(inode, gen, type, dataset, cached);
1333 if (rc < 0 || (!rc && *cached))
1338 * Update the saved dataset flags for the inode accordingly if failed.
1340 if (!rc && !*cached) {
1342 * Currently auto attach strategy for a PCC backend is
1343 * unchangeable once once it was added into the PCC datasets on
1344 * a client as the support to change auto attach strategy is
1345 * not implemented yet.
1348 * If tried to attach from one PCC backend:
1349 * @lli_pcc_generation > 0:
1350 * 1) The file was once attached into PCC, but now the
1351 * corresponding PCC backend should be removed from the client;
1352 * 2) The layout generation was changed, the data has been
1354 * 3) The corresponding PCC copy is not existed on PCC
1355 * @lli_pcc_generation == 0:
1356 * The file is never attached into PCC but in a HSM released
1357 * state, or once attached into PCC but the inode was evicted
1358 * from icache later.
1359 * Set the saved dataset flags with PCC_DATASET_NONE. Then this
1360 * file will skip from the candidates to try auto attach until
1361 * the file is attached into PCC again.
1363 * If the file was never attached into PCC, or once attached but
1364 * its inode was evicted from icache (lli_pcc_generation == 0),
1365 * or the corresponding dataset was removed from the client,
1366 * set the saved dataset flags with PCC_DATASET_NONE.
1368 * TODO: If the file was once attached into PCC but not try to
1369 * auto attach due to the change of the configuration parameters
1370 * for this dataset (i.e. change from auto attach enabled to
1371 * auto attach disabled for this dataset), update the saved
1372 * dataset flags with the found one.
1374 lli->lli_pcc_dsflags = PCC_DATASET_NONE;
1376 up_read(&super->pccs_rw_sem);
1382 * TODO: For RW-PCC, it is desirable to store HSM info as a layout (LU-10606).
1383 * Thus the client can get archive ID from the layout directly. When try to
1384 * attach the file automatically which is in HSM released state (according to
1385 * LOV_PATTERN_F_RELEASED in the layout), it can determine whether the file is
1386 * valid cached on PCC more precisely according to the @rwid (archive ID) in
1387 * the PCC dataset and the archive ID in HSM attrs.
1389 static int pcc_try_auto_attach(struct inode *inode, bool *cached,
1390 enum pcc_io_type iot)
1392 struct pcc_super *super = &ll_i2sbi(inode)->ll_pcc_super;
1393 struct cl_layout clt = {
1395 .cl_is_released = false,
1397 struct ll_inode_info *lli = ll_i2info(inode);
1404 * Quick check whether there is PCC device.
1406 if (list_empty(&super->pccs_datasets))
1410 * The file layout lock was cancelled. And this open does not
1411 * obtain valid layout lock from MDT (i.e. the file is being
1414 if (iot == PIT_OPEN) {
1415 if (ll_layout_version_get(lli) == CL_LAYOUT_GEN_NONE)
1418 rc = ll_layout_refresh(inode, &gen);
1423 rc = pcc_get_layout_info(inode, &clt);
1427 if (iot != PIT_OPEN && gen != clt.cl_layout_gen) {
1428 CDEBUG(D_CACHE, DFID" layout changed from %d to %d.\n",
1429 PFID(ll_inode2fid(inode)), gen, clt.cl_layout_gen);
1433 if (clt.cl_is_released)
1434 rc = pcc_try_datasets_attach(inode, iot, clt.cl_layout_gen,
1435 LU_PCC_READWRITE, cached);
1436 else if (clt.cl_is_rdonly)
1437 rc = pcc_try_datasets_attach(inode, iot, clt.cl_layout_gen,
1438 LU_PCC_READONLY, cached);
1443 static inline bool pcc_may_auto_attach(struct inode *inode,
1444 enum pcc_io_type iot)
1446 struct ll_inode_info *lli = ll_i2info(inode);
1447 struct pcc_super *super = ll_i2pccs(inode);
1451 /* Known the file was not in any PCC backend. */
1452 if (lli->lli_pcc_dsflags & PCC_DATASET_NONE)
1456 * lli_pcc_generation == 0 means that the file was never attached into
1457 * PCC, or may be once attached into PCC but detached as the inode is
1458 * evicted from icache (i.e. "echo 3 > /proc/sys/vm/drop_caches" or
1459 * icache shrinking due to the memory pressure), which will cause the
1460 * file detach from PCC when releasing the inode from icache.
1461 * In either case, we still try to attach.
1463 /* lli_pcc_generation == 0, or the PCC setting was changed,
1464 * or there is no PCC setup on the client and the try will return
1465 * immediately in pcc_try_auto_attach().
1467 if (super->pccs_generation != lli->lli_pcc_generation)
1470 /* The cached setting @lli_pcc_dsflags is valid */
1471 if (iot == PIT_OPEN)
1472 RETURN(lli->lli_pcc_dsflags & PCC_DATASET_OPEN_ATTACH);
1474 if (iot == PIT_GETATTR)
1475 RETURN(lli->lli_pcc_dsflags & PCC_DATASET_STAT_ATTACH);
1477 RETURN(lli->lli_pcc_dsflags & PCC_DATASET_IO_ATTACH);
1480 int pcc_file_open(struct inode *inode, struct file *file)
1482 struct pcc_inode *pcci;
1483 struct ll_inode_info *lli = ll_i2info(inode);
1484 struct ll_file_data *fd = file->private_data;
1485 struct pcc_file *pccf = &fd->fd_pcc_file;
1486 struct file *pcc_file;
1488 bool cached = false;
1493 if (!S_ISREG(inode->i_mode))
1496 if (IS_ENCRYPTED(inode))
1499 pcc_inode_lock(inode);
1500 pcci = ll_i2pcci(inode);
1502 if (lli->lli_pcc_state & PCC_STATE_FL_ATTACHING)
1503 GOTO(out_unlock, rc = 0);
1505 if (!pcci || !pcc_inode_has_layout(pcci)) {
1506 if (pcc_may_auto_attach(inode, PIT_OPEN))
1507 rc = pcc_try_auto_attach(inode, &cached, PIT_OPEN);
1509 if (rc < 0 || !cached)
1510 GOTO(out_unlock, rc);
1513 pcci = ll_i2pcci(inode);
1516 pcc_inode_get(pcci);
1517 WARN_ON(pccf->pccf_file);
1519 path = &pcci->pcci_path;
1520 CDEBUG(D_CACHE, "opening pcc file '%pd'\n", path->dentry);
1522 pcc_file = dentry_open(path, file->f_flags,
1523 pcc_super_cred(inode->i_sb));
1524 if (IS_ERR_OR_NULL(pcc_file)) {
1525 rc = pcc_file == NULL ? -EINVAL : PTR_ERR(pcc_file);
1526 pcc_inode_put(pcci);
1528 pccf->pccf_file = pcc_file;
1529 pccf->pccf_type = pcci->pcci_type;
1533 pcc_inode_unlock(inode);
1537 void pcc_file_release(struct inode *inode, struct file *file)
1539 struct pcc_inode *pcci;
1540 struct ll_file_data *fd = file->private_data;
1541 struct pcc_file *pccf;
1546 if (!S_ISREG(inode->i_mode) || fd == NULL)
1549 pccf = &fd->fd_pcc_file;
1550 pcc_inode_lock(inode);
1551 if (pccf->pccf_file == NULL)
1554 pcci = ll_i2pcci(inode);
1556 path = &pcci->pcci_path;
1557 CDEBUG(D_CACHE, "releasing pcc file \"%pd\"\n", path->dentry);
1558 pcc_inode_put(pcci);
1559 fput(pccf->pccf_file);
1560 pccf->pccf_file = NULL;
1562 pcc_inode_unlock(inode);
1566 /* Tolerate the IO failure on PCC and fall back to normal Lustre IO path */
1567 static bool pcc_io_tolerate(struct pcc_inode *pcci,
1568 enum pcc_io_type iot, int rc)
1570 if (pcci->pcci_type == LU_PCC_READWRITE) {
1571 if (iot == PIT_WRITE && (rc == -ENOSPC || rc == -EDQUOT))
1573 /* Handle the ->page_mkwrite failure tolerance separately
1574 * in pcc_page_mkwrite().
1576 } else if (pcci->pcci_type == LU_PCC_READONLY) {
1577 if ((iot == PIT_READ || iot == PIT_GETATTR ||
1578 iot == PIT_SPLICE_READ) && rc < 0 && rc != -ENOMEM)
1580 if (iot == PIT_FAULT && (rc & VM_FAULT_SIGBUS) &&
1581 !(rc & VM_FAULT_OOM))
1588 static void pcc_io_init(struct inode *inode, enum pcc_io_type iot, bool *cached)
1590 struct pcc_inode *pcci;
1592 pcc_inode_lock(inode);
1593 pcci = ll_i2pcci(inode);
1594 if (pcci && pcc_inode_has_layout(pcci)) {
1595 LASSERT(atomic_read(&pcci->pcci_refcount) > 0);
1596 if (pcci->pcci_type == LU_PCC_READONLY &&
1597 (iot == PIT_WRITE || iot == PIT_SETATTR ||
1598 iot == PIT_PAGE_MKWRITE)) {
1599 /* Fall back to normal I/O path */
1601 /* For mmap write, we need to detach the file from
1602 * RO-PCC, release the page got from ->fault(), and
1603 * then retry the memory fault handling (->fault()
1604 * and ->page_mkwrite()).
1605 * These are done in pcc_page_mkwrite();
1608 atomic_inc(&pcci->pcci_active_ios);
1613 if (pcc_may_auto_attach(inode, iot)) {
1614 (void) pcc_try_auto_attach(inode, cached, iot);
1616 pcci = ll_i2pcci(inode);
1617 LASSERT(atomic_read(&pcci->pcci_refcount) > 0);
1618 atomic_inc(&pcci->pcci_active_ios);
1622 pcc_inode_unlock(inode);
1625 static void pcc_io_fini(struct inode *inode, enum pcc_io_type iot,
1626 int rc, bool *cached)
1628 struct pcc_inode *pcci = ll_i2pcci(inode);
1630 LASSERT(pcci && atomic_read(&pcci->pcci_active_ios) > 0 && *cached);
1632 *cached = pcc_io_tolerate(pcci, iot, rc);
1633 if (atomic_dec_and_test(&pcci->pcci_active_ios))
1634 wake_up(&pcci->pcci_waitq);
1639 __pcc_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
1641 struct file *file = iocb->ki_filp;
1643 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
1644 return file->f_op->read_iter(iocb, iter);
1650 iov_for_each(iov, i, *iter) {
1653 res = file->f_op->aio_read(iocb, &iov, 1, iocb->ki_pos);
1654 if (-EIOCBQUEUED == res)
1655 res = wait_on_sync_kiocb(iocb);
1663 if (res < iov.iov_len)
1668 iov_iter_advance(iter, bytes);
1673 ssize_t pcc_file_read_iter(struct kiocb *iocb,
1674 struct iov_iter *iter, bool *cached)
1676 struct file *file = iocb->ki_filp;
1677 struct ll_file_data *fd = file->private_data;
1678 struct pcc_file *pccf = &fd->fd_pcc_file;
1679 struct inode *inode = file_inode(file);
1684 if (pccf->pccf_file == NULL) {
1689 pcc_io_init(inode, PIT_READ, cached);
1693 /* Fake I/O error on RO-PCC */
1694 if (CFS_FAIL_CHECK(OBD_FAIL_LLITE_PCC_FAKE_ERROR))
1695 GOTO(out, result = -EIO);
1697 iocb->ki_filp = pccf->pccf_file;
1698 /* generic_file_aio_read does not support ext4-dax,
1699 * __pcc_file_read_iter uses ->aio_read hook directly
1700 * to add support for ext4-dax.
1702 result = __pcc_file_read_iter(iocb, iter);
1703 iocb->ki_filp = file;
1705 pcc_io_fini(inode, PIT_READ, result, cached);
1710 __pcc_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
1712 struct file *file = iocb->ki_filp;
1714 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
1715 return file->f_op->write_iter(iocb, iter);
1721 iov_for_each(iov, i, *iter) {
1724 res = file->f_op->aio_write(iocb, &iov, 1, iocb->ki_pos);
1725 if (-EIOCBQUEUED == res)
1726 res = wait_on_sync_kiocb(iocb);
1734 if (res < iov.iov_len)
1739 iov_iter_advance(iter, bytes);
1744 ssize_t pcc_file_write_iter(struct kiocb *iocb,
1745 struct iov_iter *iter, bool *cached)
1747 struct file *file = iocb->ki_filp;
1748 struct ll_file_data *fd = file->private_data;
1749 struct pcc_file *pccf = &fd->fd_pcc_file;
1750 struct inode *inode = file_inode(file);
1755 if (pccf->pccf_file == NULL) {
1760 if (pccf->pccf_type != LU_PCC_READWRITE) {
1765 pcc_io_init(inode, PIT_WRITE, cached);
1769 if (CFS_FAIL_CHECK(OBD_FAIL_LLITE_PCC_FAKE_ERROR))
1770 GOTO(out, result = -ENOSPC);
1772 iocb->ki_filp = pccf->pccf_file;
1774 /* Since __pcc_file_write_iter makes write calls via
1775 * the normal vfs interface to the local PCC file system,
1776 * the inode lock is not needed.
1778 result = __pcc_file_write_iter(iocb, iter);
1779 iocb->ki_filp = file;
1781 pcc_io_fini(inode, PIT_WRITE, result, cached);
1785 int pcc_inode_setattr(struct inode *inode, struct iattr *attr,
1789 const struct cred *old_cred;
1790 struct iattr attr2 = *attr;
1791 struct dentry *pcc_dentry;
1792 struct pcc_inode *pcci;
1796 if (!S_ISREG(inode->i_mode)) {
1801 pcc_io_init(inode, PIT_SETATTR, cached);
1805 attr2.ia_valid = attr->ia_valid & (ATTR_SIZE | ATTR_ATIME |
1806 ATTR_ATIME_SET | ATTR_MTIME | ATTR_MTIME_SET |
1807 ATTR_CTIME | ATTR_UID | ATTR_GID);
1808 pcci = ll_i2pcci(inode);
1809 pcc_dentry = pcci->pcci_path.dentry;
1810 inode_lock(pcc_dentry->d_inode);
1811 old_cred = override_creds(pcc_super_cred(inode->i_sb));
1812 #ifdef HAVE_USER_NAMESPACE_ARG
1813 rc = pcc_dentry->d_inode->i_op->setattr(&nop_mnt_idmap, pcc_dentry,
1816 rc = pcc_dentry->d_inode->i_op->setattr(pcc_dentry, &attr2);
1818 revert_creds(old_cred);
1819 inode_unlock(pcc_dentry->d_inode);
1821 pcc_io_fini(inode, PIT_SETATTR, rc, cached);
1825 int pcc_inode_getattr(struct inode *inode, u32 request_mask,
1826 unsigned int flags, bool *cached)
1828 struct ll_inode_info *lli = ll_i2info(inode);
1829 const struct cred *old_cred;
1838 if (!S_ISREG(inode->i_mode)) {
1843 pcc_io_init(inode, PIT_GETATTR, cached);
1847 old_cred = override_creds(pcc_super_cred(inode->i_sb));
1848 rc = ll_vfs_getattr(&ll_i2pcci(inode)->pcci_path, &stat, request_mask,
1850 revert_creds(old_cred);
1854 ll_inode_size_lock(inode);
1855 if (test_and_clear_bit(LLIF_UPDATE_ATIME, &lli->lli_flags) ||
1856 inode_get_atime_sec(inode) < lli->lli_atime)
1857 inode_set_atime(inode, lli->lli_atime, 0);
1859 inode_set_mtime(inode, lli->lli_mtime, 0);
1860 inode_set_ctime(inode, lli->lli_ctime, 0);
1862 atime = inode_get_atime_sec(inode);
1863 mtime = inode_get_mtime_sec(inode);
1864 ctime = inode_get_ctime_sec(inode);
1866 if (atime < stat.atime.tv_sec)
1867 atime = stat.atime.tv_sec;
1869 if (ctime < stat.ctime.tv_sec)
1870 ctime = stat.ctime.tv_sec;
1872 if (mtime < stat.mtime.tv_sec)
1873 mtime = stat.mtime.tv_sec;
1875 i_size_write(inode, stat.size);
1876 inode->i_blocks = stat.blocks;
1878 inode_set_atime(inode, atime, 0);
1879 inode_set_mtime(inode, mtime, 0);
1880 inode_set_ctime(inode, ctime, 0);
1882 ll_inode_size_unlock(inode);
1884 pcc_io_fini(inode, PIT_GETATTR, rc, cached);
1888 #ifdef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
1889 ssize_t pcc_file_splice_read(struct file *in_file, loff_t *ppos,
1890 struct pipe_inode_info *pipe,
1891 size_t count, unsigned int flags)
1893 struct inode *inode = file_inode(in_file);
1894 struct ll_file_data *fd = in_file->private_data;
1895 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1896 bool cached = false;
1902 RETURN(default_file_splice_read(in_file, ppos, pipe,
1905 pcc_io_init(inode, PIT_SPLICE_READ, &cached);
1907 RETURN(default_file_splice_read(in_file, ppos, pipe,
1910 result = default_file_splice_read(pcc_file, ppos, pipe, count, flags);
1912 pcc_io_fini(inode, PIT_SPLICE_READ, result, &cached);
1915 #endif /* HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT */
1917 int pcc_fsync(struct file *file, loff_t start, loff_t end,
1918 int datasync, bool *cached)
1920 struct inode *inode = file_inode(file);
1921 struct ll_file_data *fd = file->private_data;
1922 struct pcc_file *pccf = &fd->fd_pcc_file;
1923 struct file *pcc_file = pccf->pccf_file;
1933 if (!S_ISREG(inode->i_mode)) {
1939 * After the file is attached into RO-PCC, its dirty pages on this
1940 * client may not be flushed. So fsync() should fall back to normal
1941 * Lustre I/O path flushing dirty data to OSTs. And flush on RO-PCC
1942 * copy is meaningless.
1944 if (pccf->pccf_type == LU_PCC_READONLY) {
1949 pcc_io_init(inode, PIT_FSYNC, cached);
1953 rc = file_inode(pcc_file)->i_fop->fsync(pcc_file,
1954 start, end, datasync);
1956 pcc_io_fini(inode, PIT_FSYNC, rc, cached);
1960 int pcc_file_mmap(struct file *file, struct vm_area_struct *vma,
1963 struct inode *inode = file_inode(file);
1964 struct ll_file_data *fd = file->private_data;
1965 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1966 struct pcc_inode *pcci;
1971 if (!pcc_file || !file_inode(pcc_file)->i_fop->mmap) {
1976 pcc_inode_lock(inode);
1977 pcci = ll_i2pcci(inode);
1978 if (pcci && pcc_inode_has_layout(pcci)) {
1979 LASSERT(atomic_read(&pcci->pcci_refcount) > 1);
1981 vma->vm_file = pcc_file;
1982 rc = file_inode(pcc_file)->i_fop->mmap(pcc_file, vma);
1983 vma->vm_file = file;
1984 /* Save the vm ops of backend PCC */
1985 vma->vm_private_data = (void *)vma->vm_ops;
1989 pcc_inode_unlock(inode);
1994 void pcc_vm_open(struct vm_area_struct *vma)
1996 struct pcc_inode *pcci;
1997 struct file *file = vma->vm_file;
1998 struct inode *inode = file_inode(file);
1999 struct ll_file_data *fd = file->private_data;
2000 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
2001 struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
2005 if (!pcc_file || !pcc_vm_ops || !pcc_vm_ops->open)
2008 pcc_inode_lock(inode);
2009 pcci = ll_i2pcci(inode);
2010 if (pcci && pcc_inode_has_layout(pcci)) {
2011 vma->vm_file = pcc_file;
2012 pcc_vm_ops->open(vma);
2013 vma->vm_file = file;
2015 pcc_inode_unlock(inode);
2019 void pcc_vm_close(struct vm_area_struct *vma)
2021 struct file *file = vma->vm_file;
2022 struct inode *inode = file_inode(file);
2023 struct ll_file_data *fd = file->private_data;
2024 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
2025 struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
2029 if (!pcc_file || !pcc_vm_ops || !pcc_vm_ops->close)
2032 pcc_inode_lock(inode);
2033 /* Layout lock maybe revoked here */
2034 vma->vm_file = pcc_file;
2035 pcc_vm_ops->close(vma);
2036 vma->vm_file = file;
2037 pcc_inode_unlock(inode);
2041 int pcc_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
2044 struct page *page = vmf->page;
2045 struct mm_struct *mm = vma->vm_mm;
2046 struct file *file = vma->vm_file;
2047 struct inode *inode = file_inode(file);
2048 struct ll_file_data *fd = file->private_data;
2049 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
2050 struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
2055 if (!pcc_file || !pcc_vm_ops) {
2060 if (!pcc_vm_ops->page_mkwrite &&
2061 page->mapping == pcc_file->f_mapping) {
2063 "%s: PCC backend fs not support ->page_mkwrite()\n",
2064 ll_i2sbi(inode)->ll_fsname);
2065 pcc_ioctl_detach(inode, PCC_DETACH_OPT_UNCACHE);
2066 mmap_read_unlock(mm);
2068 RETURN(VM_FAULT_RETRY | VM_FAULT_NOPAGE);
2070 /* Pause to allow for a race with concurrent detach */
2071 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_PCC_MKWRITE_PAUSE, cfs_fail_val);
2073 pcc_io_init(inode, PIT_PAGE_MKWRITE, cached);
2075 /* This happens when the file is detached from PCC after got
2076 * the fault page via ->fault() on the inode of the PCC copy.
2077 * Here it can not simply fall back to normal Lustre I/O path.
2078 * The reason is that the address space of fault page used by
2079 * ->page_mkwrite() is still the one of PCC inode. In the
2080 * normal Lustre ->page_mkwrite() I/O path, it will be wrongly
2081 * handled as the address space of the fault page is not
2082 * consistent with the one of the Lustre inode (though the
2083 * fault page was truncated).
2084 * As the file is detached from PCC, the fault page must
2085 * be released frist, and retry the mmap write (->fault() and
2087 * We use an ugly and tricky method by returning
2088 * VM_FAULT_NOPAGE | VM_FAULT_RETRY to the caller
2089 * __do_page_fault and retry the memory fault handling.
2091 if (page->mapping == pcc_file->f_mapping) {
2092 pcc_ioctl_detach(inode, PCC_DETACH_OPT_UNCACHE);
2094 mmap_read_unlock(mm);
2095 RETURN(VM_FAULT_RETRY | VM_FAULT_NOPAGE);
2102 * This fault injection can also be used to simulate -ENOSPC and
2103 * -EDQUOT failure of underlying PCC backend fs.
2105 if (CFS_FAIL_CHECK(OBD_FAIL_LLITE_PCC_DETACH_MKWRITE))
2106 GOTO(out, rc = VM_FAULT_SIGBUS);
2108 vma->vm_file = pcc_file;
2109 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
2110 rc = pcc_vm_ops->page_mkwrite(vmf);
2112 rc = pcc_vm_ops->page_mkwrite(vma, vmf);
2114 vma->vm_file = file;
2117 pcc_io_fini(inode, PIT_PAGE_MKWRITE, rc, cached);
2119 /* VM_FAULT_SIGBUG usually means that underlying PCC backend fs returns
2120 * -EIO, -ENOSPC or -EDQUOT. Thus we can retry this IO from the normal
2123 if (rc & VM_FAULT_SIGBUS) {
2124 pcc_ioctl_detach(inode, PCC_DETACH_OPT_UNCACHE);
2125 mmap_read_unlock(mm);
2126 RETURN(VM_FAULT_RETRY | VM_FAULT_NOPAGE);
2131 int pcc_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
2134 struct file *file = vma->vm_file;
2135 struct inode *inode = file_inode(file);
2136 struct ll_file_data *fd = file->private_data;
2137 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
2138 struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
2143 if (!pcc_file || !pcc_vm_ops || !pcc_vm_ops->fault) {
2148 if (!S_ISREG(inode->i_mode)) {
2153 pcc_io_init(inode, PIT_FAULT, cached);
2157 /* Tolerate the mmap read failure for RO-PCC */
2158 if (CFS_FAIL_CHECK(OBD_FAIL_LLITE_PCC_FAKE_ERROR))
2159 GOTO(out, rc = VM_FAULT_SIGBUS);
2161 vma->vm_file = pcc_file;
2162 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
2163 rc = pcc_vm_ops->fault(vmf);
2165 rc = pcc_vm_ops->fault(vma, vmf);
2167 vma->vm_file = file;
2169 pcc_io_fini(inode, PIT_FAULT, rc, cached);
2173 static void __pcc_layout_invalidate(struct pcc_inode *pcci)
2175 pcci->pcci_type = LU_PCC_NONE;
2176 pcc_layout_gen_set(pcci, CL_LAYOUT_GEN_NONE);
2177 if (atomic_read(&pcci->pcci_active_ios) == 0)
2180 CDEBUG(D_CACHE, "Waiting for IO completion: %d\n",
2181 atomic_read(&pcci->pcci_active_ios));
2182 wait_event_idle(pcci->pcci_waitq,
2183 atomic_read(&pcci->pcci_active_ios) == 0);
2186 void pcc_layout_invalidate(struct inode *inode)
2188 struct pcc_inode *pcci;
2192 pcc_inode_lock(inode);
2193 pcci = ll_i2pcci(inode);
2194 if (pcci && pcc_inode_has_layout(pcci)) {
2195 LASSERT(atomic_read(&pcci->pcci_refcount) > 0);
2196 __pcc_layout_invalidate(pcci);
2198 CDEBUG(D_CACHE, "Invalidate "DFID" layout gen %d\n",
2199 PFID(&ll_i2info(inode)->lli_fid), pcci->pcci_layout_gen);
2201 pcc_inode_put(pcci);
2203 pcc_inode_unlock(inode);
2208 static int pcc_inode_remove(struct inode *inode, struct dentry *pcc_dentry)
2210 struct dentry *parent = dget_parent(pcc_dentry);
2213 rc = vfs_unlink(&nop_mnt_idmap, d_inode(parent), pcc_dentry);
2215 CWARN("%s: failed to unlink PCC file %pd, rc = %d\n",
2216 ll_i2sbi(inode)->ll_fsname, pcc_dentry, rc);
2222 /* Create directory under base if directory does not exist */
2223 static struct dentry *
2224 pcc_mkdir(struct dentry *base, const char *name, umode_t mode)
2227 struct dentry *dentry;
2228 struct inode *dir = base->d_inode;
2231 dentry = lookup_one_len(name, base, strlen(name));
2235 if (d_is_positive(dentry))
2238 rc = vfs_mkdir(&nop_mnt_idmap, dir, dentry, mode);
2241 dentry = ERR_PTR(rc);
2249 static struct dentry *
2250 pcc_mkdir_p(struct dentry *root, char *path, umode_t mode)
2252 char *ptr, *entry_name;
2253 struct dentry *parent;
2254 struct dentry *child = ERR_PTR(-EINVAL);
2261 parent = dget(root);
2262 while ((ptr = strchr(ptr, '/')) != NULL) {
2264 child = pcc_mkdir(parent, entry_name, mode);
2278 /* Create file under base. If file already exist, return failure */
2279 static struct dentry *
2280 pcc_create(struct dentry *base, const char *name, umode_t mode)
2283 struct dentry *dentry;
2284 struct inode *dir = base->d_inode;
2287 dentry = lookup_one_len(name, base, strlen(name));
2291 if (d_is_positive(dentry))
2294 rc = vfs_create(&nop_mnt_idmap, dir, dentry, mode, false);
2297 dentry = ERR_PTR(rc);
2305 static int __pcc_inode_create(struct pcc_dataset *dataset,
2307 struct dentry **dentry)
2310 struct dentry *base;
2311 struct dentry *child;
2314 OBD_ALLOC(path, PCC_DATASET_MAX_PATH);
2318 pcc_fid2dataset_path(path, PCC_DATASET_MAX_PATH, fid);
2320 base = pcc_mkdir_p(dataset->pccd_path.dentry, path, 0);
2326 snprintf(path, PCC_DATASET_MAX_PATH, DFID_NOBRACE, PFID(fid));
2327 child = pcc_create(base, path, 0);
2328 if (IS_ERR(child)) {
2329 rc = PTR_ERR(child);
2337 OBD_FREE(path, PCC_DATASET_MAX_PATH);
2342 * Reset uid, gid or size for the PCC copy masked by @valid.
2343 * TODO: Set the project ID for PCC copy.
2345 static int pcc_inode_reset_iattr(struct dentry *dentry, unsigned int valid,
2346 kuid_t uid, kgid_t gid, loff_t size)
2348 struct inode *inode = dentry->d_inode;
2354 attr.ia_valid = valid;
2357 attr.ia_size = size;
2360 rc = notify_change(&nop_mnt_idmap, dentry, &attr, NULL);
2361 inode_unlock(inode);
2366 int pcc_inode_create(struct super_block *sb, struct pcc_dataset *dataset,
2367 struct lu_fid *fid, struct dentry **pcc_dentry)
2369 const struct cred *old_cred;
2372 old_cred = override_creds(pcc_super_cred(sb));
2373 rc = __pcc_inode_create(dataset, fid, pcc_dentry);
2374 revert_creds(old_cred);
2378 int pcc_inode_create_fini(struct inode *inode, struct pcc_create_attach *pca)
2380 struct dentry *pcc_dentry = pca->pca_dentry;
2381 const struct cred *old_cred;
2382 struct pcc_super *super;
2383 struct pcc_inode *pcci;
2388 if (!pca->pca_dataset)
2392 GOTO(out_dataset_put, rc = 0);
2394 super = ll_i2pccs(inode);
2396 LASSERT(pcc_dentry);
2398 old_cred = override_creds(super->pccs_cred);
2399 pcc_inode_lock(inode);
2400 LASSERT(ll_i2pcci(inode) == NULL);
2401 OBD_SLAB_ALLOC_PTR_GFP(pcci, pcc_inode_slab, GFP_NOFS);
2403 GOTO(out_put, rc = -ENOMEM);
2405 rc = pcc_inode_reset_iattr(pcc_dentry, ATTR_UID | ATTR_GID,
2406 old_cred->suid, old_cred->sgid, 0);
2410 pcc_inode_attach_set(super, pca->pca_dataset, ll_i2info(inode),
2411 pcci, pcc_dentry, LU_PCC_READWRITE);
2413 rc = pcc_layout_xattr_set(pcci, 0);
2415 if (!pcci->pcci_unlinked)
2416 (void) pcc_inode_remove(inode, pcci->pcci_path.dentry);
2417 pcc_inode_put(pcci);
2418 GOTO(out_unlock, rc);
2421 /* Set the layout generation of newly created file with 0 */
2422 pcc_layout_gen_set(pcci, 0);
2426 (void) pcc_inode_remove(inode, pcc_dentry);
2430 OBD_SLAB_FREE_PTR(pcci, pcc_inode_slab);
2433 pcc_inode_unlock(inode);
2434 revert_creds(old_cred);
2436 pcc_dataset_put(pca->pca_dataset);
2440 void pcc_create_attach_cleanup(struct super_block *sb,
2441 struct pcc_create_attach *pca)
2443 if (!pca->pca_dataset)
2446 if (pca->pca_dentry) {
2447 struct dentry *parent;
2448 struct inode *i_dir;
2449 const struct cred *old_cred;
2452 old_cred = override_creds(pcc_super_cred(sb));
2453 parent = dget_parent(pca->pca_dentry);
2454 i_dir = d_inode(parent);
2455 rc = vfs_unlink(&nop_mnt_idmap, i_dir, pca->pca_dentry);
2458 CWARN("%s: failed to unlink PCC file %pd: rc = %d\n",
2459 ll_s2sbi(sb)->ll_fsname, pca->pca_dentry, rc);
2460 /* ignore the unlink failure */
2461 revert_creds(old_cred);
2462 dput(pca->pca_dentry);
2465 pcc_dataset_put(pca->pca_dataset);
2468 static int pcc_filp_write(struct file *filp, const void *buf, ssize_t count,
2474 size = cfs_kernel_write(filp, buf, count, offset);
2483 static ssize_t pcc_copy_data(struct file *src, struct file *dst)
2487 loff_t pos, offset = 0;
2488 size_t buf_len = 1048576;
2493 OBD_ALLOC_LARGE(buf, buf_len);
2498 if (signal_pending(current))
2499 GOTO(out_free, rc = -EINTR);
2502 rc2 = cfs_kernel_read(src, buf, buf_len, &pos);
2504 GOTO(out_free, rc = rc2);
2509 rc = pcc_filp_write(dst, buf, rc2, &pos);
2517 OBD_FREE_LARGE(buf, buf_len);
2521 static int pcc_attach_allowed_check(struct inode *inode)
2523 struct ll_inode_info *lli = ll_i2info(inode);
2524 struct pcc_inode *pcci;
2529 pcc_inode_lock(inode);
2530 if (lli->lli_pcc_state & PCC_STATE_FL_ATTACHING)
2531 GOTO(out_unlock, rc = -EBUSY);
2533 pcci = ll_i2pcci(inode);
2534 if (pcci && pcc_inode_has_layout(pcci))
2535 GOTO(out_unlock, rc = -EEXIST);
2537 lli->lli_pcc_state |= PCC_STATE_FL_ATTACHING;
2539 pcc_inode_unlock(inode);
2543 static int pcc_attach_data_archive(struct file *file, struct inode *inode,
2544 struct pcc_dataset *dataset,
2545 struct dentry **dentry)
2547 const struct cred *old_cred;
2548 struct file *pcc_filp;
2555 old_cred = override_creds(pcc_super_cred(inode->i_sb));
2556 rc = __pcc_inode_create(dataset, &ll_i2info(inode)->lli_fid, dentry);
2560 path.mnt = dataset->pccd_path.mnt;
2561 path.dentry = *dentry;
2562 pcc_filp = dentry_open(&path, O_WRONLY | O_LARGEFILE, current_cred());
2563 if (IS_ERR_OR_NULL(pcc_filp)) {
2564 rc = pcc_filp == NULL ? -EINVAL : PTR_ERR(pcc_filp);
2565 GOTO(out_dentry, rc);
2568 rc = pcc_inode_reset_iattr(*dentry, ATTR_UID | ATTR_GID,
2569 old_cred->uid, old_cred->gid, 0);
2573 ret = pcc_copy_data(file, pcc_filp);
2575 GOTO(out_fput, rc = ret);
2578 * It must to truncate the PCC copy to the same size of the Lustre
2579 * copy after copy data. Otherwise, it may get wrong file size after
2580 * re-attach a file. See LU-13023 for details.
2582 rc = pcc_inode_reset_iattr(*dentry, ATTR_SIZE, KUIDT_INIT(0),
2583 KGIDT_INIT(0), ret);
2588 pcc_inode_remove(inode, *dentry);
2592 revert_creds(old_cred);
2596 int pcc_readwrite_attach(struct file *file, struct inode *inode,
2599 struct pcc_dataset *dataset;
2600 struct ll_inode_info *lli = ll_i2info(inode);
2601 struct pcc_super *super = ll_i2pccs(inode);
2602 struct pcc_inode *pcci;
2603 struct dentry *dentry;
2608 rc = pcc_attach_allowed_check(inode);
2612 dataset = pcc_dataset_get(&ll_i2sbi(inode)->ll_pcc_super,
2613 LU_PCC_READWRITE, archive_id);
2614 if (dataset == NULL)
2617 rc = pcc_attach_data_archive(file, inode, dataset, &dentry);
2619 GOTO(out_dataset_put, rc);
2621 /* Pause to allow for a race with concurrent HSM remove */
2622 CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_PCC_ATTACH_PAUSE, cfs_fail_val);
2624 pcc_inode_lock(inode);
2625 pcci = ll_i2pcci(inode);
2627 OBD_SLAB_ALLOC_PTR_GFP(pcci, pcc_inode_slab, GFP_NOFS);
2629 GOTO(out_unlock, rc = -ENOMEM);
2631 pcc_inode_attach_set(super, dataset, lli, pcci,
2632 dentry, LU_PCC_READWRITE);
2634 pcc_inode_unlock(inode);
2636 const struct cred *old_cred;
2638 old_cred = override_creds(pcc_super_cred(inode->i_sb));
2639 (void) pcc_inode_remove(inode, dentry);
2640 revert_creds(old_cred);
2644 pcc_dataset_put(dataset);
2649 int pcc_readwrite_attach_fini(struct file *file, struct inode *inode,
2650 __u32 gen, bool lease_broken, int rc,
2653 struct ll_inode_info *lli = ll_i2info(inode);
2654 const struct cred *old_cred;
2655 struct pcc_inode *pcci;
2660 old_cred = override_creds(pcc_super_cred(inode->i_sb));
2661 pcc_inode_lock(inode);
2662 pcci = ll_i2pcci(inode);
2663 if (rc || lease_broken) {
2664 if (attached && pcci)
2665 pcc_inode_put(pcci);
2667 GOTO(out_unlock, rc);
2670 /* PCC inode may be released due to layout lock revocatioin */
2672 GOTO(out_unlock, rc = -ESTALE);
2675 rc = pcc_layout_xattr_set(pcci, gen);
2679 LASSERT(lli->lli_pcc_state & PCC_STATE_FL_ATTACHING);
2680 rc = ll_layout_refresh(inode, &gen2);
2683 pcc_layout_gen_set(pcci, gen);
2686 DFID" layout changed from %d to %d.\n",
2687 PFID(ll_inode2fid(inode)), gen, gen2);
2688 GOTO(out_put, rc = -ESTALE);
2694 if (!pcci->pcci_unlinked)
2695 (void) pcc_inode_remove(inode, pcci->pcci_path.dentry);
2696 pcc_inode_put(pcci);
2699 lli->lli_pcc_state &= ~PCC_STATE_FL_ATTACHING;
2700 pcc_inode_unlock(inode);
2701 revert_creds(old_cred);
2705 static int pcc_layout_rdonly_set(struct inode *inode, __u32 *gen)
2708 struct ll_inode_info *lli = ll_i2info(inode);
2709 struct lu_extent ext = {
2711 .e_end = OBD_OBJECT_EOF,
2713 struct cl_layout clt = {
2715 .cl_is_released = false,
2716 .cl_is_rdonly = false,
2724 rc = pcc_get_layout_info(inode, &clt);
2729 * For the HSM released file, restore the data first.
2731 if (clt.cl_is_released) {
2736 if (ll_layout_version_get(lli) != CL_LAYOUT_GEN_NONE) {
2737 rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
2739 CDEBUG(D_CACHE, DFID" RESTORE failure: %d\n",
2740 PFID(&lli->lli_fid), rc);
2744 rc = ll_layout_refresh(inode, gen);
2752 if (!clt.cl_is_rdonly) {
2753 rc = ll_layout_write_intent(inode, LAYOUT_INTENT_PCCRO_SET,
2758 rc = ll_layout_refresh(inode, gen);
2761 } else { /* Readonly layout */
2762 *gen = clt.cl_layout_gen;
2768 static int pcc_readonly_ioctl_attach(struct file *file,
2769 struct inode *inode,
2770 struct lu_pcc_attach *attach)
2772 struct ll_sb_info *sbi = ll_i2sbi(inode);
2773 struct pcc_super *super = ll_i2pccs(inode);
2774 struct ll_inode_info *lli = ll_i2info(inode);
2775 const struct cred *old_cred;
2776 struct pcc_dataset *dataset;
2777 struct pcc_inode *pcci;
2778 struct dentry *dentry;
2779 bool attached = false;
2780 bool unlinked = false;
2786 if (!test_bit(LL_SBI_LAYOUT_LOCK, sbi->ll_flags))
2787 RETURN(-EOPNOTSUPP);
2789 rc = pcc_attach_allowed_check(inode);
2793 rc = pcc_layout_rdonly_set(inode, &gen);
2797 dataset = pcc_dataset_get(&ll_s2sbi(inode->i_sb)->ll_pcc_super,
2798 LU_PCC_READONLY, attach->pcca_id);
2799 if (dataset == NULL)
2802 rc = pcc_attach_data_archive(file, inode, dataset, &dentry);
2804 GOTO(out_dataset_put, rc);
2806 mutex_lock(&lli->lli_layout_mutex);
2807 pcc_inode_lock(inode);
2808 old_cred = override_creds(super->pccs_cred);
2809 lli->lli_pcc_state &= ~PCC_STATE_FL_ATTACHING;
2810 if (gen != ll_layout_version_get(lli))
2811 GOTO(out_put_unlock, rc = -ESTALE);
2813 pcci = ll_i2pcci(inode);
2815 OBD_SLAB_ALLOC_PTR_GFP(pcci, pcc_inode_slab, GFP_NOFS);
2817 GOTO(out_put_unlock, rc = -ENOMEM);
2819 pcc_inode_attach_set(super, dataset, lli, pcci,
2820 dentry, LU_PCC_READONLY);
2822 atomic_inc(&pcci->pcci_refcount);
2823 path_put(&pcci->pcci_path);
2824 pcci->pcci_path.mnt = mntget(dataset->pccd_path.mnt);
2825 pcci->pcci_path.dentry = dentry;
2826 pcci->pcci_type = LU_PCC_READONLY;
2829 rc = pcc_layout_xattr_set(pcci, gen);
2831 pcci->pcci_type = LU_PCC_NONE;
2832 unlinked = pcci->pcci_unlinked;
2833 GOTO(out_put_unlock, rc);
2836 pcc_layout_gen_set(pcci, gen);
2840 (void) pcc_inode_remove(inode, dentry);
2842 pcc_inode_put(pcci);
2846 revert_creds(old_cred);
2847 pcc_inode_unlock(inode);
2848 mutex_unlock(&lli->lli_layout_mutex);
2850 pcc_dataset_put(dataset);
2855 int pcc_ioctl_attach(struct file *file, struct inode *inode,
2856 struct lu_pcc_attach *attach)
2862 switch (attach->pcca_type) {
2863 case LU_PCC_READWRITE:
2866 case LU_PCC_READONLY:
2867 rc = pcc_readonly_ioctl_attach(file, inode, attach);
2877 static int pcc_hsm_remove(struct inode *inode)
2879 struct hsm_user_request *hur;
2886 rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
2888 CDEBUG(D_CACHE, DFID" RESTORE failure: %d\n",
2889 PFID(&ll_i2info(inode)->lli_fid), rc);
2893 ll_layout_refresh(inode, &gen);
2895 len = sizeof(struct hsm_user_request) +
2896 sizeof(struct hsm_user_item);
2897 OBD_ALLOC(hur, len);
2901 hur->hur_request.hr_action = HUA_REMOVE;
2902 hur->hur_request.hr_archive_id = 0;
2903 hur->hur_request.hr_flags = 0;
2904 memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
2905 sizeof(hur->hur_user_item[0].hui_fid));
2906 hur->hur_user_item[0].hui_extent.offset = 0;
2907 hur->hur_user_item[0].hui_extent.length = OBD_OBJECT_EOF;
2908 hur->hur_request.hr_itemcount = 1;
2909 rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,
2912 CDEBUG(D_CACHE, DFID" HSM REMOVE failure: %d\n",
2913 PFID(&ll_i2info(inode)->lli_fid), rc);
2919 int pcc_ioctl_detach(struct inode *inode, __u32 opt)
2921 struct ll_inode_info *lli = ll_i2info(inode);
2922 struct pcc_inode *pcci;
2923 const struct cred *old_cred;
2924 bool hsm_remove = false;
2929 pcc_inode_lock(inode);
2930 pcci = lli->lli_pcc_inode;
2931 if (!pcci || lli->lli_pcc_state & PCC_STATE_FL_ATTACHING ||
2932 !pcc_inode_has_layout(pcci))
2933 GOTO(out_unlock, rc = 0);
2935 LASSERT(atomic_read(&pcci->pcci_refcount) > 0);
2937 if (pcci->pcci_type == LU_PCC_READWRITE) {
2938 if (opt == PCC_DETACH_OPT_UNCACHE) {
2941 * The file will be removed from PCC, set the flags
2942 * with PCC_DATASET_NONE even the later removal of the
2945 lli->lli_pcc_dsflags = PCC_DATASET_NONE;
2948 __pcc_layout_invalidate(pcci);
2949 pcc_inode_put(pcci);
2950 } else if (pcci->pcci_type == LU_PCC_READONLY) {
2951 __pcc_layout_invalidate(pcci);
2953 if (opt == PCC_DETACH_OPT_UNCACHE && !pcci->pcci_unlinked) {
2954 old_cred = override_creds(pcc_super_cred(inode->i_sb));
2955 rc = pcc_inode_remove(inode, pcci->pcci_path.dentry);
2956 revert_creds(old_cred);
2958 pcci->pcci_unlinked = true;
2961 pcc_inode_put(pcci);
2967 pcc_inode_unlock(inode);
2969 old_cred = override_creds(pcc_super_cred(inode->i_sb));
2970 rc = pcc_hsm_remove(inode);
2971 revert_creds(old_cred);
2977 int pcc_ioctl_state(struct file *file, struct inode *inode,
2978 struct lu_pcc_state *state)
2984 int buf_len = sizeof(state->pccs_path);
2985 struct ll_file_data *fd = file->private_data;
2986 struct pcc_file *pccf = &fd->fd_pcc_file;
2987 struct pcc_inode *pcci;
2994 OBD_ALLOC(buf, buf_len);
2998 pcc_inode_lock(inode);
2999 pcci = ll_i2pcci(inode);
3001 state->pccs_type = LU_PCC_NONE;
3002 GOTO(out_unlock, rc = 0);
3005 count = atomic_read(&pcci->pcci_refcount);
3007 state->pccs_type = LU_PCC_NONE;
3008 state->pccs_open_count = 0;
3009 GOTO(out_unlock, rc = 0);
3012 if (pcc_inode_has_layout(pcci))
3014 if (pccf->pccf_file != NULL)
3016 state->pccs_type = pcci->pcci_type;
3017 state->pccs_open_count = count;
3018 state->pccs_flags = ll_i2info(inode)->lli_pcc_state;
3019 path = dentry_path_raw(pcci->pcci_path.dentry, buf, buf_len);
3021 GOTO(out_unlock, rc = PTR_ERR(path));
3023 if (strlcpy(state->pccs_path, path, buf_len) >= buf_len)
3024 GOTO(out_unlock, rc = -ENAMETOOLONG);
3027 pcc_inode_unlock(inode);
3028 OBD_FREE(buf, buf_len);