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);
131 /* Rule based auto caching */
132 static void pcc_id_list_free(struct list_head *id_list)
134 struct pcc_match_id *id, *n;
136 list_for_each_entry_safe(id, n, id_list, pmi_linkage) {
137 list_del_init(&id->pmi_linkage);
142 static void pcc_fname_list_free(struct list_head *fname_list)
144 struct pcc_match_fname *fname, *n;
146 list_for_each_entry_safe(fname, n, fname_list, pmf_linkage) {
147 OBD_FREE(fname->pmf_name, strlen(fname->pmf_name) + 1);
148 list_del_init(&fname->pmf_linkage);
153 static void pcc_expression_free(struct pcc_expression *expr)
155 LASSERT(expr->pe_field >= PCC_FIELD_UID &&
156 expr->pe_field < PCC_FIELD_MAX);
157 switch (expr->pe_field) {
160 case PCC_FIELD_PROJID:
161 pcc_id_list_free(&expr->pe_cond);
163 case PCC_FIELD_FNAME:
164 pcc_fname_list_free(&expr->pe_cond);
172 static void pcc_conjunction_free(struct pcc_conjunction *conjunction)
174 struct pcc_expression *expression, *n;
176 LASSERT(list_empty(&conjunction->pc_linkage));
177 list_for_each_entry_safe(expression, n,
178 &conjunction->pc_expressions,
180 list_del_init(&expression->pe_linkage);
181 pcc_expression_free(expression);
183 OBD_FREE_PTR(conjunction);
186 static void pcc_rule_conds_free(struct list_head *cond_list)
188 struct pcc_conjunction *conjunction, *n;
190 list_for_each_entry_safe(conjunction, n, cond_list, pc_linkage) {
191 list_del_init(&conjunction->pc_linkage);
192 pcc_conjunction_free(conjunction);
196 static void pcc_cmd_fini(struct pcc_cmd *cmd)
198 if (cmd->pccc_cmd == PCC_ADD_DATASET) {
199 if (!list_empty(&cmd->u.pccc_add.pccc_conds))
200 pcc_rule_conds_free(&cmd->u.pccc_add.pccc_conds);
201 if (cmd->u.pccc_add.pccc_conds_str)
202 OBD_FREE(cmd->u.pccc_add.pccc_conds_str,
203 strlen(cmd->u.pccc_add.pccc_conds_str) + 1);
207 #define PCC_DISJUNCTION_DELIM (',')
208 #define PCC_CONJUNCTION_DELIM ('&')
209 #define PCC_EXPRESSION_DELIM ('=')
212 pcc_fname_list_add(struct cfs_lstr *id, struct list_head *fname_list)
214 struct pcc_match_fname *fname;
216 OBD_ALLOC(fname, sizeof(struct pcc_match_fname));
220 OBD_ALLOC(fname->pmf_name, id->ls_len + 1);
221 if (fname->pmf_name == NULL) {
222 OBD_FREE(fname, sizeof(struct pcc_match_fname));
226 memcpy(fname->pmf_name, id->ls_str, id->ls_len);
227 list_add_tail(&fname->pmf_linkage, fname_list);
232 pcc_fname_list_parse(char *str, int len, struct list_head *fname_list)
242 INIT_LIST_HEAD(fname_list);
244 rc = cfs_gettok(&src, ' ', &res);
249 rc = pcc_fname_list_add(&res, fname_list);
254 pcc_fname_list_free(fname_list);
259 pcc_id_list_parse(char *str, int len, struct list_head *id_list,
268 if (type != PCC_FIELD_UID && type != PCC_FIELD_GID &&
269 type != PCC_FIELD_PROJID)
274 INIT_LIST_HEAD(id_list);
276 struct pcc_match_id *id;
279 if (cfs_gettok(&src, ' ', &res) == 0)
280 GOTO(out, rc = -EINVAL);
282 if (!cfs_str2num_check(res.ls_str, res.ls_len,
283 &id_val, 0, (u32)~0U))
284 GOTO(out, rc = -EINVAL);
288 GOTO(out, rc = -ENOMEM);
291 list_add_tail(&id->pmi_linkage, id_list);
295 pcc_id_list_free(id_list);
300 pcc_check_field(struct cfs_lstr *field, char *str)
302 int len = strlen(str);
304 return (field->ls_len == len &&
305 strncmp(field->ls_str, str, len) == 0);
309 pcc_expression_parse(struct cfs_lstr *src, struct list_head *cond_list)
311 struct pcc_expression *expr;
312 struct cfs_lstr field;
315 OBD_ALLOC(expr, sizeof(struct pcc_expression));
319 rc = cfs_gettok(src, PCC_EXPRESSION_DELIM, &field);
320 if (rc == 0 || src->ls_len <= 2 || src->ls_str[0] != '{' ||
321 src->ls_str[src->ls_len - 1] != '}')
322 GOTO(out, rc = -EINVAL);
324 /* Skip '{' and '}' */
328 if (pcc_check_field(&field, "uid")) {
329 if (pcc_id_list_parse(src->ls_str,
333 GOTO(out, rc = -EINVAL);
334 expr->pe_field = PCC_FIELD_UID;
335 } else if (pcc_check_field(&field, "gid")) {
336 if (pcc_id_list_parse(src->ls_str,
340 GOTO(out, rc = -EINVAL);
341 expr->pe_field = PCC_FIELD_GID;
342 } else if (pcc_check_field(&field, "projid")) {
343 if (pcc_id_list_parse(src->ls_str,
346 PCC_FIELD_PROJID) < 0)
347 GOTO(out, rc = -EINVAL);
348 expr->pe_field = PCC_FIELD_PROJID;
349 } else if (pcc_check_field(&field, "fname")) {
350 if (pcc_fname_list_parse(src->ls_str,
353 GOTO(out, rc = -EINVAL);
354 expr->pe_field = PCC_FIELD_FNAME;
356 GOTO(out, rc = -EINVAL);
359 list_add_tail(&expr->pe_linkage, cond_list);
367 pcc_conjunction_parse(struct cfs_lstr *src, struct list_head *cond_list)
369 struct pcc_conjunction *conjunction;
370 struct cfs_lstr expr;
373 OBD_ALLOC(conjunction, sizeof(struct pcc_conjunction));
374 if (conjunction == NULL)
377 INIT_LIST_HEAD(&conjunction->pc_expressions);
378 list_add_tail(&conjunction->pc_linkage, cond_list);
380 while (src->ls_str) {
381 rc = cfs_gettok(src, PCC_CONJUNCTION_DELIM, &expr);
386 rc = pcc_expression_parse(&expr,
387 &conjunction->pc_expressions);
394 static int pcc_conds_parse(char *str, int len, struct list_head *cond_list)
402 INIT_LIST_HEAD(cond_list);
404 rc = cfs_gettok(&src, PCC_DISJUNCTION_DELIM, &res);
409 rc = pcc_conjunction_parse(&res, cond_list);
416 static int pcc_id_parse(struct pcc_cmd *cmd, const char *id)
420 OBD_ALLOC(cmd->u.pccc_add.pccc_conds_str, strlen(id) + 1);
421 if (cmd->u.pccc_add.pccc_conds_str == NULL)
424 memcpy(cmd->u.pccc_add.pccc_conds_str, id, strlen(id));
426 rc = pcc_conds_parse(cmd->u.pccc_add.pccc_conds_str,
427 strlen(cmd->u.pccc_add.pccc_conds_str),
428 &cmd->u.pccc_add.pccc_conds);
436 pcc_parse_value_pair(struct pcc_cmd *cmd, char *buffer)
443 key = strsep(&val, "=");
444 if (val == NULL || strlen(val) == 0)
447 /* Key of the value pair */
448 if (strcmp(key, "rwid") == 0) {
449 rc = kstrtoul(val, 10, &id);
454 cmd->u.pccc_add.pccc_rwid = id;
455 } else if (strcmp(key, "roid") == 0) {
456 rc = kstrtoul(val, 10, &id);
461 cmd->u.pccc_add.pccc_roid = id;
462 } else if (strcmp(key, "open_attach") == 0) {
463 rc = kstrtoul(val, 10, &id);
467 cmd->u.pccc_add.pccc_flags |= PCC_DATASET_OPEN_ATTACH;
468 } else if (strcmp(key, "rwpcc") == 0) {
469 rc = kstrtoul(val, 10, &id);
473 cmd->u.pccc_add.pccc_flags |= PCC_DATASET_RWPCC;
474 } else if (strcmp(key, "ropcc") == 0) {
475 rc = kstrtoul(val, 10, &id);
479 cmd->u.pccc_add.pccc_flags |= PCC_DATASET_ROPCC;
488 pcc_parse_value_pairs(struct pcc_cmd *cmd, char *buffer)
495 while (val != NULL && strlen(val) != 0) {
496 token = strsep(&val, " ");
497 rc = pcc_parse_value_pair(cmd, token);
502 switch (cmd->pccc_cmd) {
503 case PCC_ADD_DATASET:
504 if (cmd->u.pccc_add.pccc_flags & PCC_DATASET_RWPCC &&
505 cmd->u.pccc_add.pccc_flags & PCC_DATASET_ROPCC)
508 * By default, a PCC backend can provide caching service for
509 * both RW-PCC and RO-PCC.
511 if ((cmd->u.pccc_add.pccc_flags & PCC_DATASET_PCC_ALL) == 0)
512 cmd->u.pccc_add.pccc_flags |= PCC_DATASET_PCC_ALL;
514 case PCC_DEL_DATASET:
524 pcc_dataset_rule_fini(struct pcc_match_rule *rule)
526 if (!list_empty(&rule->pmr_conds))
527 pcc_rule_conds_free(&rule->pmr_conds);
528 LASSERT(rule->pmr_conds_str != NULL);
529 OBD_FREE(rule->pmr_conds_str, strlen(rule->pmr_conds_str) + 1);
533 pcc_dataset_rule_init(struct pcc_match_rule *rule, struct pcc_cmd *cmd)
537 LASSERT(cmd->u.pccc_add.pccc_conds_str);
538 OBD_ALLOC(rule->pmr_conds_str,
539 strlen(cmd->u.pccc_add.pccc_conds_str) + 1);
540 if (rule->pmr_conds_str == NULL)
543 memcpy(rule->pmr_conds_str,
544 cmd->u.pccc_add.pccc_conds_str,
545 strlen(cmd->u.pccc_add.pccc_conds_str));
547 INIT_LIST_HEAD(&rule->pmr_conds);
548 if (!list_empty(&cmd->u.pccc_add.pccc_conds))
549 rc = pcc_conds_parse(rule->pmr_conds_str,
550 strlen(rule->pmr_conds_str),
554 pcc_dataset_rule_fini(rule);
561 pcc_id_list_match(struct list_head *id_list, __u32 id_val)
563 struct pcc_match_id *id;
565 list_for_each_entry(id, id_list, pmi_linkage) {
566 if (id->pmi_id == id_val)
573 cfs_match_wildcard(const char *pattern, const char *content)
575 if (*pattern == '\0' && *content == '\0')
578 if (*pattern == '*' && *(pattern + 1) != '\0' && *content == '\0')
581 while (*pattern == *content) {
584 if (*pattern == '\0' && *content == '\0')
587 if (*pattern == '*' && *(pattern + 1) != '\0' &&
593 return (cfs_match_wildcard(pattern + 1, content) ||
594 cfs_match_wildcard(pattern, content + 1));
600 pcc_fname_list_match(struct list_head *fname_list, const char *name)
602 struct pcc_match_fname *fname;
604 list_for_each_entry(fname, fname_list, pmf_linkage) {
605 if (cfs_match_wildcard(fname->pmf_name, name))
612 pcc_expression_match(struct pcc_expression *expr, struct pcc_matcher *matcher)
614 switch (expr->pe_field) {
616 return pcc_id_list_match(&expr->pe_cond, matcher->pm_uid);
618 return pcc_id_list_match(&expr->pe_cond, matcher->pm_gid);
619 case PCC_FIELD_PROJID:
620 return pcc_id_list_match(&expr->pe_cond, matcher->pm_projid);
621 case PCC_FIELD_FNAME:
622 return pcc_fname_list_match(&expr->pe_cond,
623 matcher->pm_name->name);
630 pcc_conjunction_match(struct pcc_conjunction *conjunction,
631 struct pcc_matcher *matcher)
633 struct pcc_expression *expr;
636 list_for_each_entry(expr, &conjunction->pc_expressions, pe_linkage) {
637 matched = pcc_expression_match(expr, matcher);
646 pcc_cond_match(struct pcc_match_rule *rule, struct pcc_matcher *matcher)
648 struct pcc_conjunction *conjunction;
651 list_for_each_entry(conjunction, &rule->pmr_conds, pc_linkage) {
652 matched = pcc_conjunction_match(conjunction, matcher);
661 pcc_dataset_match_get(struct pcc_super *super, struct pcc_matcher *matcher)
663 struct pcc_dataset *dataset;
664 struct pcc_dataset *selected = NULL;
666 down_read(&super->pccs_rw_sem);
667 list_for_each_entry(dataset, &super->pccs_datasets, pccd_linkage) {
668 if (!(dataset->pccd_flags & PCC_DATASET_RWPCC))
671 if (pcc_cond_match(&dataset->pccd_rule, matcher)) {
672 atomic_inc(&dataset->pccd_refcount);
677 up_read(&super->pccs_rw_sem);
679 CDEBUG(D_CACHE, "PCC create, matched %s - %d:%d:%d:%s\n",
680 dataset->pccd_rule.pmr_conds_str,
681 matcher->pm_uid, matcher->pm_gid,
682 matcher->pm_projid, matcher->pm_name->name);
688 * pcc_dataset_add - Add a Cache policy to control which files need be
689 * cached and where it will be cached.
691 * @super: superblock of pcc
695 pcc_dataset_add(struct pcc_super *super, struct pcc_cmd *cmd)
697 char *pathname = cmd->pccc_pathname;
698 struct pcc_dataset *dataset;
699 struct pcc_dataset *tmp;
703 OBD_ALLOC_PTR(dataset);
707 rc = kern_path(pathname, LOOKUP_DIRECTORY, &dataset->pccd_path);
709 OBD_FREE_PTR(dataset);
712 strncpy(dataset->pccd_pathname, pathname, PATH_MAX);
713 dataset->pccd_rwid = cmd->u.pccc_add.pccc_rwid;
714 dataset->pccd_roid = cmd->u.pccc_add.pccc_roid;
715 dataset->pccd_flags = cmd->u.pccc_add.pccc_flags;
716 atomic_set(&dataset->pccd_refcount, 1);
718 rc = pcc_dataset_rule_init(&dataset->pccd_rule, cmd);
720 pcc_dataset_put(dataset);
724 down_write(&super->pccs_rw_sem);
725 list_for_each_entry(tmp, &super->pccs_datasets, pccd_linkage) {
726 if (strcmp(tmp->pccd_pathname, pathname) == 0 ||
727 (dataset->pccd_rwid != 0 &&
728 dataset->pccd_rwid == tmp->pccd_rwid) ||
729 (dataset->pccd_roid != 0 &&
730 dataset->pccd_roid == tmp->pccd_roid)) {
736 list_add(&dataset->pccd_linkage, &super->pccs_datasets);
737 up_write(&super->pccs_rw_sem);
740 pcc_dataset_put(dataset);
748 pcc_dataset_get(struct pcc_super *super, enum lu_pcc_type type, __u32 id)
750 struct pcc_dataset *dataset;
751 struct pcc_dataset *selected = NULL;
757 * archive ID (read-write ID) or read-only ID is unique in the list,
758 * we just return last added one as first priority.
760 down_read(&super->pccs_rw_sem);
761 list_for_each_entry(dataset, &super->pccs_datasets, pccd_linkage) {
762 if (type == LU_PCC_READWRITE && (dataset->pccd_rwid != id ||
763 !(dataset->pccd_flags & PCC_DATASET_RWPCC)))
765 atomic_inc(&dataset->pccd_refcount);
769 up_read(&super->pccs_rw_sem);
771 CDEBUG(D_CACHE, "matched id %u, PCC mode %d\n", id, type);
777 pcc_dataset_put(struct pcc_dataset *dataset)
779 if (atomic_dec_and_test(&dataset->pccd_refcount)) {
780 pcc_dataset_rule_fini(&dataset->pccd_rule);
781 path_put(&dataset->pccd_path);
782 OBD_FREE_PTR(dataset);
787 pcc_dataset_del(struct pcc_super *super, char *pathname)
789 struct list_head *l, *tmp;
790 struct pcc_dataset *dataset;
793 down_write(&super->pccs_rw_sem);
794 list_for_each_safe(l, tmp, &super->pccs_datasets) {
795 dataset = list_entry(l, struct pcc_dataset, pccd_linkage);
796 if (strcmp(dataset->pccd_pathname, pathname) == 0) {
797 list_del_init(&dataset->pccd_linkage);
798 pcc_dataset_put(dataset);
803 up_write(&super->pccs_rw_sem);
808 pcc_dataset_dump(struct pcc_dataset *dataset, struct seq_file *m)
810 seq_printf(m, "%s:\n", dataset->pccd_pathname);
811 seq_printf(m, " rwid: %u\n", dataset->pccd_rwid);
812 seq_printf(m, " flags: %x\n", dataset->pccd_flags);
813 seq_printf(m, " autocache: %s\n", dataset->pccd_rule.pmr_conds_str);
817 pcc_super_dump(struct pcc_super *super, struct seq_file *m)
819 struct pcc_dataset *dataset;
821 down_read(&super->pccs_rw_sem);
822 list_for_each_entry(dataset, &super->pccs_datasets, pccd_linkage) {
823 pcc_dataset_dump(dataset, m);
825 up_read(&super->pccs_rw_sem);
829 static void pcc_remove_datasets(struct pcc_super *super)
831 struct pcc_dataset *dataset, *tmp;
833 down_write(&super->pccs_rw_sem);
834 list_for_each_entry_safe(dataset, tmp,
835 &super->pccs_datasets, pccd_linkage) {
836 list_del(&dataset->pccd_linkage);
837 pcc_dataset_put(dataset);
839 up_write(&super->pccs_rw_sem);
842 void pcc_super_fini(struct pcc_super *super)
844 pcc_remove_datasets(super);
845 put_cred(super->pccs_cred);
848 static bool pathname_is_valid(const char *pathname)
850 /* Needs to be absolute path */
851 if (pathname == NULL || strlen(pathname) == 0 ||
852 strlen(pathname) >= PATH_MAX || pathname[0] != '/')
857 static struct pcc_cmd *
858 pcc_cmd_parse(char *buffer, unsigned long count)
860 static struct pcc_cmd *cmd;
867 GOTO(out, rc = -ENOMEM);
869 /* clear all setting */
870 if (strncmp(buffer, "clear", 5) == 0) {
871 cmd->pccc_cmd = PCC_CLEAR_ALL;
876 token = strsep(&val, " ");
877 if (val == NULL || strlen(val) == 0)
878 GOTO(out_free_cmd, rc = -EINVAL);
880 /* Type of the command */
881 if (strcmp(token, "add") == 0)
882 cmd->pccc_cmd = PCC_ADD_DATASET;
883 else if (strcmp(token, "del") == 0)
884 cmd->pccc_cmd = PCC_DEL_DATASET;
886 GOTO(out_free_cmd, rc = -EINVAL);
888 /* Pathname of the dataset */
889 token = strsep(&val, " ");
890 if ((val == NULL && cmd->pccc_cmd != PCC_DEL_DATASET) ||
891 !pathname_is_valid(token))
892 GOTO(out_free_cmd, rc = -EINVAL);
893 cmd->pccc_pathname = token;
895 if (cmd->pccc_cmd == PCC_ADD_DATASET) {
899 val = strrchr(token, '}');
901 GOTO(out_free_cmd, rc = -EINVAL);
907 } else if (*val == ' ') {
911 GOTO(out_free_cmd, rc = -EINVAL);
914 rc = pcc_id_parse(cmd, token);
916 GOTO(out_free_cmd, rc);
918 rc = pcc_parse_value_pairs(cmd, val);
920 GOTO(out_cmd_fini, rc = -EINVAL);
933 int pcc_cmd_handle(char *buffer, unsigned long count,
934 struct pcc_super *super)
939 cmd = pcc_cmd_parse(buffer, count);
943 switch (cmd->pccc_cmd) {
944 case PCC_ADD_DATASET:
945 rc = pcc_dataset_add(super, cmd);
947 case PCC_DEL_DATASET:
948 rc = pcc_dataset_del(super, cmd->pccc_pathname);
951 pcc_remove_datasets(super);
963 static inline void pcc_inode_lock(struct inode *inode)
965 mutex_lock(&ll_i2info(inode)->lli_pcc_lock);
968 static inline void pcc_inode_unlock(struct inode *inode)
970 mutex_unlock(&ll_i2info(inode)->lli_pcc_lock);
973 static void pcc_inode_init(struct pcc_inode *pcci, struct ll_inode_info *lli)
975 pcci->pcci_lli = lli;
976 lli->lli_pcc_inode = pcci;
977 atomic_set(&pcci->pcci_refcount, 0);
978 pcci->pcci_type = LU_PCC_NONE;
979 pcci->pcci_layout_gen = CL_LAYOUT_GEN_NONE;
980 atomic_set(&pcci->pcci_active_ios, 0);
981 init_waitqueue_head(&pcci->pcci_waitq);
984 static void pcc_inode_fini(struct pcc_inode *pcci)
986 struct ll_inode_info *lli = pcci->pcci_lli;
988 path_put(&pcci->pcci_path);
989 pcci->pcci_type = LU_PCC_NONE;
990 OBD_SLAB_FREE_PTR(pcci, pcc_inode_slab);
991 lli->lli_pcc_inode = NULL;
994 static void pcc_inode_get(struct pcc_inode *pcci)
996 atomic_inc(&pcci->pcci_refcount);
999 static void pcc_inode_put(struct pcc_inode *pcci)
1001 if (atomic_dec_and_test(&pcci->pcci_refcount))
1002 pcc_inode_fini(pcci);
1005 void pcc_inode_free(struct inode *inode)
1007 struct pcc_inode *pcci = ll_i2pcci(inode);
1010 WARN_ON(atomic_read(&pcci->pcci_refcount) > 1);
1011 pcc_inode_put(pcci);
1017 * As Andreas suggested, we'd better use new layout to
1019 * (fid->f_oid >> 16 & oxFFFF)/FID
1021 #define MAX_PCC_DATABASE_PATH (6 * 5 + FID_NOBRACE_LEN + 1)
1022 static int pcc_fid2dataset_path(char *buf, int sz, struct lu_fid *fid)
1024 return snprintf(buf, sz, "%04x/%04x/%04x/%04x/%04x/%04x/"
1026 (fid)->f_oid & 0xFFFF,
1027 (fid)->f_oid >> 16 & 0xFFFF,
1028 (unsigned int)((fid)->f_seq & 0xFFFF),
1029 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
1030 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
1031 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
1035 static inline const struct cred *pcc_super_cred(struct super_block *sb)
1037 return ll_s2sbi(sb)->ll_pcc_super.pccs_cred;
1040 void pcc_file_init(struct pcc_file *pccf)
1042 pccf->pccf_file = NULL;
1043 pccf->pccf_type = LU_PCC_NONE;
1046 static inline bool pcc_open_attach_enabled(struct pcc_dataset *dataset)
1048 return dataset->pccd_flags & PCC_DATASET_OPEN_ATTACH;
1051 static const char pcc_xattr_layout[] = XATTR_USER_PREFIX "PCC.layout";
1053 static int pcc_layout_xattr_set(struct pcc_inode *pcci, __u32 gen)
1055 struct dentry *pcc_dentry = pcci->pcci_path.dentry;
1056 struct ll_inode_info *lli = pcci->pcci_lli;
1061 if (!(lli->lli_pcc_state & PCC_STATE_FL_OPEN_ATTACH))
1064 #ifndef HAVE_VFS_SETXATTR
1065 if (!pcc_dentry->d_inode->i_op->setxattr)
1068 rc = pcc_dentry->d_inode->i_op->setxattr(pcc_dentry, pcc_xattr_layout,
1069 &gen, sizeof(gen), 0);
1071 rc = __vfs_setxattr(pcc_dentry, pcc_dentry->d_inode, pcc_xattr_layout,
1072 &gen, sizeof(gen), 0);
1077 static int pcc_get_layout_info(struct inode *inode, struct cl_layout *clt)
1080 struct ll_inode_info *lli = ll_i2info(inode);
1089 env = cl_env_get(&refcheck);
1091 RETURN(PTR_ERR(env));
1093 rc = cl_object_layout_get(env, lli->lli_clob, clt);
1095 CDEBUG(D_INODE, "Cannot get layout for "DFID"\n",
1096 PFID(ll_inode2fid(inode)));
1098 cl_env_put(env, &refcheck);
1102 static int pcc_fid2dataset_fullpath(char *buf, int sz, struct lu_fid *fid,
1103 struct pcc_dataset *dataset)
1105 return snprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
1107 dataset->pccd_pathname,
1108 (fid)->f_oid & 0xFFFF,
1109 (fid)->f_oid >> 16 & 0xFFFF,
1110 (unsigned int)((fid)->f_seq & 0xFFFF),
1111 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
1112 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
1113 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
1117 /* Must be called with pcci->pcci_lock held */
1118 static void pcc_inode_attach_init(struct pcc_dataset *dataset,
1119 struct pcc_inode *pcci,
1120 struct dentry *dentry,
1121 enum lu_pcc_type type)
1123 pcci->pcci_path.mnt = mntget(dataset->pccd_path.mnt);
1124 pcci->pcci_path.dentry = dentry;
1125 LASSERT(atomic_read(&pcci->pcci_refcount) == 0);
1126 atomic_set(&pcci->pcci_refcount, 1);
1127 pcci->pcci_type = type;
1128 pcci->pcci_attr_valid = false;
1130 if (pcc_open_attach_enabled(dataset)) {
1131 struct ll_inode_info *lli = pcci->pcci_lli;
1133 lli->lli_pcc_state |= PCC_STATE_FL_OPEN_ATTACH;
1137 static inline void pcc_layout_gen_set(struct pcc_inode *pcci,
1140 pcci->pcci_layout_gen = gen;
1143 static inline bool pcc_inode_has_layout(struct pcc_inode *pcci)
1145 return pcci->pcci_layout_gen != CL_LAYOUT_GEN_NONE;
1148 static int pcc_try_dataset_attach(struct inode *inode, __u32 gen,
1149 enum lu_pcc_type type,
1150 struct pcc_dataset *dataset,
1153 struct ll_inode_info *lli = ll_i2info(inode);
1154 struct pcc_inode *pcci = lli->lli_pcc_inode;
1155 const struct cred *old_cred;
1156 struct dentry *pcc_dentry;
1164 if (type == LU_PCC_READWRITE &&
1165 !(dataset->pccd_flags & PCC_DATASET_RWPCC))
1168 OBD_ALLOC(pathname, PATH_MAX);
1169 if (pathname == NULL)
1172 pcc_fid2dataset_fullpath(pathname, PATH_MAX, &lli->lli_fid, dataset);
1174 old_cred = override_creds(pcc_super_cred(inode->i_sb));
1175 rc = kern_path(pathname, LOOKUP_FOLLOW, &path);
1177 /* ignore this error */
1180 pcc_dentry = path.dentry;
1181 #ifndef HAVE_VFS_SETXATTR
1182 if (!pcc_dentry->d_inode->i_op->getxattr)
1183 /* ignore this error */
1184 GOTO(out_put_path, rc = 0);
1186 rc = pcc_dentry->d_inode->i_op->getxattr(pcc_dentry, pcc_xattr_layout,
1187 &pcc_gen, sizeof(pcc_gen));
1189 rc = __vfs_getxattr(pcc_dentry, pcc_dentry->d_inode, pcc_xattr_layout,
1190 &pcc_gen, sizeof(pcc_gen));
1194 /* ignore this error */
1195 GOTO(out_put_path, rc = 0);
1198 /* The file is still valid cached in PCC, attach it immediately. */
1199 if (pcc_gen == gen) {
1200 CDEBUG(D_CACHE, DFID" L.Gen (%d) consistent, auto attached.\n",
1201 PFID(&lli->lli_fid), gen);
1203 OBD_SLAB_ALLOC_PTR_GFP(pcci, pcc_inode_slab, GFP_NOFS);
1205 GOTO(out_put_path, rc = -ENOMEM);
1207 pcc_inode_init(pcci, lli);
1209 pcc_inode_attach_init(dataset, pcci, pcc_dentry, type);
1212 * This happened when a file was once attached into
1213 * PCC, and some processes keep this file opened
1214 * (pcci->refcount > 1) and corresponding PCC file
1215 * without any I/O activity, and then this file was
1216 * detached by the manual detach command or the
1217 * revocation of the layout lock (i.e. cached LRU lock
1220 pcc_inode_get(pcci);
1221 pcci->pcci_type = type;
1223 pcc_layout_gen_set(pcci, gen);
1229 revert_creds(old_cred);
1230 OBD_FREE(pathname, PATH_MAX);
1234 static int pcc_try_datasets_attach(struct inode *inode, __u32 gen,
1235 enum lu_pcc_type type, bool *cached)
1237 struct pcc_dataset *dataset, *tmp;
1238 struct pcc_super *super = &ll_i2sbi(inode)->ll_pcc_super;
1243 down_read(&super->pccs_rw_sem);
1244 list_for_each_entry_safe(dataset, tmp,
1245 &super->pccs_datasets, pccd_linkage) {
1246 if (!pcc_open_attach_enabled(dataset))
1248 rc = pcc_try_dataset_attach(inode, gen, type, dataset, cached);
1249 if (rc < 0 || (!rc && *cached))
1252 up_read(&super->pccs_rw_sem);
1257 static int pcc_try_open_attach(struct inode *inode, bool *cached)
1259 struct pcc_super *super = &ll_i2sbi(inode)->ll_pcc_super;
1260 struct cl_layout clt = {
1262 .cl_is_released = false,
1269 * Quick check whether there is PCC device.
1271 if (list_empty(&super->pccs_datasets))
1275 * The file layout lock was cancelled. And this open does not
1276 * obtain valid layout lock from MDT (i.e. the file is being
1279 if (ll_layout_version_get(ll_i2info(inode)) == CL_LAYOUT_GEN_NONE)
1282 rc = pcc_get_layout_info(inode, &clt);
1286 if (clt.cl_is_released)
1287 rc = pcc_try_datasets_attach(inode, clt.cl_layout_gen,
1288 LU_PCC_READWRITE, cached);
1293 int pcc_file_open(struct inode *inode, struct file *file)
1295 struct pcc_inode *pcci;
1296 struct ll_inode_info *lli = ll_i2info(inode);
1297 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1298 struct pcc_file *pccf = &fd->fd_pcc_file;
1299 struct file *pcc_file;
1302 bool cached = false;
1307 if (!S_ISREG(inode->i_mode))
1310 pcc_inode_lock(inode);
1311 pcci = ll_i2pcci(inode);
1313 if (lli->lli_pcc_state & PCC_STATE_FL_ATTACHING)
1314 GOTO(out_unlock, rc = 0);
1316 if (!pcci || !pcc_inode_has_layout(pcci)) {
1317 rc = pcc_try_open_attach(inode, &cached);
1318 if (rc < 0 || !cached)
1319 GOTO(out_unlock, rc);
1322 pcci = ll_i2pcci(inode);
1325 pcc_inode_get(pcci);
1326 WARN_ON(pccf->pccf_file);
1328 path = &pcci->pcci_path;
1329 dname = &path->dentry->d_name;
1330 CDEBUG(D_CACHE, "opening pcc file '%.*s'\n", dname->len,
1333 #ifdef HAVE_DENTRY_OPEN_USE_PATH
1334 pcc_file = dentry_open(path, file->f_flags,
1335 pcc_super_cred(inode->i_sb));
1337 pcc_file = dentry_open(path->dentry, path->mnt, file->f_flags,
1338 pcc_super_cred(inode->i_sb));
1340 if (IS_ERR_OR_NULL(pcc_file)) {
1341 rc = pcc_file == NULL ? -EINVAL : PTR_ERR(pcc_file);
1342 pcc_inode_put(pcci);
1344 pccf->pccf_file = pcc_file;
1345 pccf->pccf_type = pcci->pcci_type;
1349 pcc_inode_unlock(inode);
1353 void pcc_file_release(struct inode *inode, struct file *file)
1355 struct pcc_inode *pcci;
1356 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1357 struct pcc_file *pccf;
1363 if (!S_ISREG(inode->i_mode) || fd == NULL)
1366 pccf = &fd->fd_pcc_file;
1367 pcc_inode_lock(inode);
1368 if (pccf->pccf_file == NULL)
1371 pcci = ll_i2pcci(inode);
1373 path = &pcci->pcci_path;
1374 dname = &path->dentry->d_name;
1375 CDEBUG(D_CACHE, "releasing pcc file \"%.*s\"\n", dname->len,
1377 pcc_inode_put(pcci);
1378 fput(pccf->pccf_file);
1379 pccf->pccf_file = NULL;
1381 pcc_inode_unlock(inode);
1385 static void pcc_io_init(struct inode *inode, bool *cached)
1387 struct pcc_inode *pcci;
1389 pcc_inode_lock(inode);
1390 pcci = ll_i2pcci(inode);
1391 if (pcci && pcc_inode_has_layout(pcci)) {
1392 LASSERT(atomic_read(&pcci->pcci_refcount) > 0);
1393 atomic_inc(&pcci->pcci_active_ios);
1398 pcc_inode_unlock(inode);
1401 static void pcc_io_fini(struct inode *inode)
1403 struct pcc_inode *pcci = ll_i2pcci(inode);
1405 LASSERT(pcci && atomic_read(&pcci->pcci_active_ios) > 0);
1406 if (atomic_dec_and_test(&pcci->pcci_active_ios))
1407 wake_up_all(&pcci->pcci_waitq);
1412 __pcc_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
1414 struct file *file = iocb->ki_filp;
1416 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
1417 return file->f_op->read_iter(iocb, iter);
1423 iov_for_each(iov, i, *iter) {
1426 res = file->f_op->aio_read(iocb, &iov, 1, iocb->ki_pos);
1427 if (-EIOCBQUEUED == res)
1428 res = wait_on_sync_kiocb(iocb);
1436 if (res < iov.iov_len)
1441 iov_iter_advance(iter, bytes);
1446 ssize_t pcc_file_read_iter(struct kiocb *iocb,
1447 struct iov_iter *iter, bool *cached)
1449 struct file *file = iocb->ki_filp;
1450 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1451 struct pcc_file *pccf = &fd->fd_pcc_file;
1452 struct inode *inode = file_inode(file);
1457 if (pccf->pccf_file == NULL) {
1462 pcc_io_init(inode, cached);
1466 iocb->ki_filp = pccf->pccf_file;
1467 /* generic_file_aio_read does not support ext4-dax,
1468 * __pcc_file_read_iter uses ->aio_read hook directly
1469 * to add support for ext4-dax.
1471 result = __pcc_file_read_iter(iocb, iter);
1472 iocb->ki_filp = file;
1479 __pcc_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
1481 struct file *file = iocb->ki_filp;
1483 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
1484 return file->f_op->write_iter(iocb, iter);
1490 iov_for_each(iov, i, *iter) {
1493 res = file->f_op->aio_write(iocb, &iov, 1, iocb->ki_pos);
1494 if (-EIOCBQUEUED == res)
1495 res = wait_on_sync_kiocb(iocb);
1503 if (res < iov.iov_len)
1508 iov_iter_advance(iter, bytes);
1513 ssize_t pcc_file_write_iter(struct kiocb *iocb,
1514 struct iov_iter *iter, bool *cached)
1516 struct file *file = iocb->ki_filp;
1517 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1518 struct pcc_file *pccf = &fd->fd_pcc_file;
1519 struct inode *inode = file_inode(file);
1524 if (pccf->pccf_file == NULL) {
1529 if (pccf->pccf_type != LU_PCC_READWRITE) {
1534 pcc_io_init(inode, cached);
1538 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_PCC_FAKE_ERROR))
1539 GOTO(out, result = -ENOSPC);
1541 iocb->ki_filp = pccf->pccf_file;
1543 /* Since __pcc_file_write_iter makes write calls via
1544 * the normal vfs interface to the local PCC file system,
1545 * the inode lock is not needed.
1547 result = __pcc_file_write_iter(iocb, iter);
1548 iocb->ki_filp = file;
1554 int pcc_inode_setattr(struct inode *inode, struct iattr *attr,
1558 const struct cred *old_cred;
1559 struct iattr attr2 = *attr;
1560 struct dentry *pcc_dentry;
1561 struct pcc_inode *pcci;
1565 if (!S_ISREG(inode->i_mode)) {
1570 pcc_io_init(inode, cached);
1574 attr2.ia_valid = attr->ia_valid & (ATTR_SIZE | ATTR_ATIME |
1575 ATTR_ATIME_SET | ATTR_MTIME | ATTR_MTIME_SET |
1576 ATTR_CTIME | ATTR_UID | ATTR_GID);
1577 pcci = ll_i2pcci(inode);
1578 pcc_dentry = pcci->pcci_path.dentry;
1579 inode_lock(pcc_dentry->d_inode);
1580 old_cred = override_creds(pcc_super_cred(inode->i_sb));
1581 rc = pcc_dentry->d_inode->i_op->setattr(pcc_dentry, &attr2);
1582 revert_creds(old_cred);
1583 inode_unlock(pcc_dentry->d_inode);
1589 int pcc_inode_getattr(struct inode *inode, bool *cached)
1591 struct ll_inode_info *lli = ll_i2info(inode);
1592 const struct cred *old_cred;
1601 if (!S_ISREG(inode->i_mode)) {
1606 pcc_io_init(inode, cached);
1610 old_cred = override_creds(pcc_super_cred(inode->i_sb));
1611 rc = ll_vfs_getattr(&ll_i2pcci(inode)->pcci_path, &stat);
1612 revert_creds(old_cred);
1616 ll_inode_size_lock(inode);
1617 if (inode->i_atime.tv_sec < lli->lli_atime ||
1618 lli->lli_update_atime) {
1619 inode->i_atime.tv_sec = lli->lli_atime;
1620 lli->lli_update_atime = 0;
1622 inode->i_mtime.tv_sec = lli->lli_mtime;
1623 inode->i_ctime.tv_sec = lli->lli_ctime;
1625 atime = inode->i_atime.tv_sec;
1626 mtime = inode->i_mtime.tv_sec;
1627 ctime = inode->i_ctime.tv_sec;
1629 if (atime < stat.atime.tv_sec)
1630 atime = stat.atime.tv_sec;
1632 if (ctime < stat.ctime.tv_sec)
1633 ctime = stat.ctime.tv_sec;
1635 if (mtime < stat.mtime.tv_sec)
1636 mtime = stat.mtime.tv_sec;
1638 i_size_write(inode, stat.size);
1639 inode->i_blocks = stat.blocks;
1641 inode->i_atime.tv_sec = atime;
1642 inode->i_mtime.tv_sec = mtime;
1643 inode->i_ctime.tv_sec = ctime;
1645 ll_inode_size_unlock(inode);
1651 ssize_t pcc_file_splice_read(struct file *in_file, loff_t *ppos,
1652 struct pipe_inode_info *pipe,
1653 size_t count, unsigned int flags,
1656 struct inode *inode = file_inode(in_file);
1657 struct ll_file_data *fd = LUSTRE_FPRIVATE(in_file);
1658 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1667 if (!file_inode(pcc_file)->i_fop->splice_read)
1670 pcc_io_init(inode, cached);
1674 result = file_inode(pcc_file)->i_fop->splice_read(pcc_file,
1682 int pcc_fsync(struct file *file, loff_t start, loff_t end,
1683 int datasync, bool *cached)
1685 struct inode *inode = file_inode(file);
1686 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1687 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1697 pcc_io_init(inode, cached);
1701 #ifdef HAVE_FILE_FSYNC_4ARGS
1702 rc = file_inode(pcc_file)->i_fop->fsync(pcc_file,
1703 start, end, datasync);
1704 #elif defined(HAVE_FILE_FSYNC_2ARGS)
1705 rc = file_inode(pcc_file)->i_fop->fsync(pcc_file, datasync);
1707 rc = file_inode(pcc_file)->i_fop->fsync(pcc_file,
1708 file_dentry(dentry), datasync);
1715 int pcc_file_mmap(struct file *file, struct vm_area_struct *vma,
1718 struct inode *inode = file_inode(file);
1719 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1720 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1721 struct pcc_inode *pcci;
1726 if (!pcc_file || !file_inode(pcc_file)->i_fop->mmap) {
1731 pcc_inode_lock(inode);
1732 pcci = ll_i2pcci(inode);
1733 if (pcci && pcc_inode_has_layout(pcci)) {
1734 LASSERT(atomic_read(&pcci->pcci_refcount) > 1);
1736 vma->vm_file = pcc_file;
1737 rc = file_inode(pcc_file)->i_fop->mmap(pcc_file, vma);
1738 vma->vm_file = file;
1739 /* Save the vm ops of backend PCC */
1740 vma->vm_private_data = (void *)vma->vm_ops;
1744 pcc_inode_unlock(inode);
1749 void pcc_vm_open(struct vm_area_struct *vma)
1751 struct pcc_inode *pcci;
1752 struct file *file = vma->vm_file;
1753 struct inode *inode = file_inode(file);
1754 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1755 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1756 struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
1760 if (!pcc_file || !pcc_vm_ops || !pcc_vm_ops->open)
1763 pcc_inode_lock(inode);
1764 pcci = ll_i2pcci(inode);
1765 if (pcci && pcc_inode_has_layout(pcci)) {
1766 vma->vm_file = pcc_file;
1767 pcc_vm_ops->open(vma);
1768 vma->vm_file = file;
1770 pcc_inode_unlock(inode);
1774 void pcc_vm_close(struct vm_area_struct *vma)
1776 struct file *file = vma->vm_file;
1777 struct inode *inode = file_inode(file);
1778 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1779 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1780 struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
1784 if (!pcc_file || !pcc_vm_ops || !pcc_vm_ops->close)
1787 pcc_inode_lock(inode);
1788 /* Layout lock maybe revoked here */
1789 vma->vm_file = pcc_file;
1790 pcc_vm_ops->close(vma);
1791 vma->vm_file = file;
1792 pcc_inode_unlock(inode);
1796 int pcc_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
1799 struct page *page = vmf->page;
1800 struct mm_struct *mm = vma->vm_mm;
1801 struct file *file = vma->vm_file;
1802 struct inode *inode = file_inode(file);
1803 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1804 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1805 struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
1810 if (!pcc_file || !pcc_vm_ops) {
1815 if (!pcc_vm_ops->page_mkwrite &&
1816 page->mapping == pcc_file->f_mapping) {
1818 "%s: PCC backend fs not support ->page_mkwrite()\n",
1819 ll_i2sbi(inode)->ll_fsname);
1820 pcc_ioctl_detach(inode);
1821 up_read(&mm->mmap_sem);
1822 RETURN(VM_FAULT_RETRY | VM_FAULT_NOPAGE);
1824 /* Pause to allow for a race with concurrent detach */
1825 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_PCC_MKWRITE_PAUSE, cfs_fail_val);
1827 pcc_io_init(inode, cached);
1829 /* This happens when the file is detached from PCC after got
1830 * the fault page via ->fault() on the inode of the PCC copy.
1831 * Here it can not simply fall back to normal Lustre I/O path.
1832 * The reason is that the address space of fault page used by
1833 * ->page_mkwrite() is still the one of PCC inode. In the
1834 * normal Lustre ->page_mkwrite() I/O path, it will be wrongly
1835 * handled as the address space of the fault page is not
1836 * consistent with the one of the Lustre inode (though the
1837 * fault page was truncated).
1838 * As the file is detached from PCC, the fault page must
1839 * be released frist, and retry the mmap write (->fault() and
1841 * We use an ugly and tricky method by returning
1842 * VM_FAULT_NOPAGE | VM_FAULT_RETRY to the caller
1843 * __do_page_fault and retry the memory fault handling.
1845 if (page->mapping == pcc_file->f_mapping) {
1847 up_read(&mm->mmap_sem);
1848 RETURN(VM_FAULT_RETRY | VM_FAULT_NOPAGE);
1855 * This fault injection can also be used to simulate -ENOSPC and
1856 * -EDQUOT failure of underlying PCC backend fs.
1858 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_PCC_DETACH_MKWRITE)) {
1860 pcc_ioctl_detach(inode);
1861 up_read(&mm->mmap_sem);
1862 RETURN(VM_FAULT_RETRY | VM_FAULT_NOPAGE);
1865 vma->vm_file = pcc_file;
1866 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
1867 rc = pcc_vm_ops->page_mkwrite(vmf);
1869 rc = pcc_vm_ops->page_mkwrite(vma, vmf);
1871 vma->vm_file = file;
1877 int pcc_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
1880 struct file *file = vma->vm_file;
1881 struct inode *inode = file_inode(file);
1882 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1883 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1884 struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
1889 if (!pcc_file || !pcc_vm_ops || !pcc_vm_ops->fault) {
1894 pcc_io_init(inode, cached);
1898 vma->vm_file = pcc_file;
1899 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
1900 rc = pcc_vm_ops->fault(vmf);
1902 rc = pcc_vm_ops->fault(vma, vmf);
1904 vma->vm_file = file;
1910 static void pcc_layout_wait(struct pcc_inode *pcci)
1912 struct l_wait_info lwi = { 0 };
1914 while (atomic_read(&pcci->pcci_active_ios) > 0) {
1915 CDEBUG(D_CACHE, "Waiting for IO completion: %d\n",
1916 atomic_read(&pcci->pcci_active_ios));
1917 l_wait_event(pcci->pcci_waitq,
1918 atomic_read(&pcci->pcci_active_ios) == 0, &lwi);
1922 static void __pcc_layout_invalidate(struct pcc_inode *pcci)
1924 pcci->pcci_type = LU_PCC_NONE;
1925 pcc_layout_gen_set(pcci, CL_LAYOUT_GEN_NONE);
1926 pcc_layout_wait(pcci);
1929 void pcc_layout_invalidate(struct inode *inode)
1931 struct pcc_inode *pcci;
1933 pcc_inode_lock(inode);
1934 pcci = ll_i2pcci(inode);
1935 if (pcci && pcc_inode_has_layout(pcci)) {
1936 LASSERT(atomic_read(&pcci->pcci_refcount) > 0);
1937 __pcc_layout_invalidate(pcci);
1939 CDEBUG(D_CACHE, "Invalidate "DFID" layout gen %d\n",
1940 PFID(&ll_i2info(inode)->lli_fid), pcci->pcci_layout_gen);
1942 pcc_inode_put(pcci);
1944 pcc_inode_unlock(inode);
1947 static int pcc_inode_remove(struct inode *inode, struct dentry *pcc_dentry)
1951 rc = ll_vfs_unlink(pcc_dentry->d_parent->d_inode, pcc_dentry);
1953 CWARN("%s: failed to unlink PCC file %.*s, rc = %d\n",
1954 ll_i2sbi(inode)->ll_fsname, pcc_dentry->d_name.len,
1955 pcc_dentry->d_name.name, rc);
1960 /* Create directory under base if directory does not exist */
1961 static struct dentry *
1962 pcc_mkdir(struct dentry *base, const char *name, umode_t mode)
1965 struct dentry *dentry;
1966 struct inode *dir = base->d_inode;
1969 dentry = lookup_one_len(name, base, strlen(name));
1973 if (d_is_positive(dentry))
1976 rc = vfs_mkdir(dir, dentry, mode);
1979 dentry = ERR_PTR(rc);
1987 static struct dentry *
1988 pcc_mkdir_p(struct dentry *root, char *path, umode_t mode)
1990 char *ptr, *entry_name;
1991 struct dentry *parent;
1992 struct dentry *child = ERR_PTR(-EINVAL);
1999 parent = dget(root);
2000 while ((ptr = strchr(ptr, '/')) != NULL) {
2002 child = pcc_mkdir(parent, entry_name, mode);
2016 /* Create file under base. If file already exist, return failure */
2017 static struct dentry *
2018 pcc_create(struct dentry *base, const char *name, umode_t mode)
2021 struct dentry *dentry;
2022 struct inode *dir = base->d_inode;
2025 dentry = lookup_one_len(name, base, strlen(name));
2029 if (d_is_positive(dentry))
2032 rc = vfs_create(dir, dentry, mode, LL_VFS_CREATE_FALSE);
2035 dentry = ERR_PTR(rc);
2043 static int __pcc_inode_create(struct pcc_dataset *dataset,
2045 struct dentry **dentry)
2048 struct dentry *base;
2049 struct dentry *child;
2052 OBD_ALLOC(path, MAX_PCC_DATABASE_PATH);
2056 pcc_fid2dataset_path(path, MAX_PCC_DATABASE_PATH, fid);
2058 base = pcc_mkdir_p(dataset->pccd_path.dentry, path, 0);
2064 snprintf(path, MAX_PCC_DATABASE_PATH, DFID_NOBRACE, PFID(fid));
2065 child = pcc_create(base, path, 0);
2066 if (IS_ERR(child)) {
2067 rc = PTR_ERR(child);
2075 OBD_FREE(path, MAX_PCC_DATABASE_PATH);
2079 /* TODO: Set the project ID for PCC copy */
2080 int pcc_inode_store_ugpid(struct dentry *dentry, kuid_t uid, kgid_t gid)
2082 struct inode *inode = dentry->d_inode;
2088 attr.ia_valid = ATTR_UID | ATTR_GID;
2093 rc = notify_change(dentry, &attr, NULL);
2094 inode_unlock(inode);
2099 int pcc_inode_create(struct super_block *sb, struct pcc_dataset *dataset,
2100 struct lu_fid *fid, struct dentry **pcc_dentry)
2102 const struct cred *old_cred;
2105 old_cred = override_creds(pcc_super_cred(sb));
2106 rc = __pcc_inode_create(dataset, fid, pcc_dentry);
2107 revert_creds(old_cred);
2111 int pcc_inode_create_fini(struct pcc_dataset *dataset, struct inode *inode,
2112 struct dentry *pcc_dentry)
2114 const struct cred *old_cred;
2115 struct pcc_inode *pcci;
2120 old_cred = override_creds(pcc_super_cred(inode->i_sb));
2121 pcc_inode_lock(inode);
2122 LASSERT(ll_i2pcci(inode) == NULL);
2123 OBD_SLAB_ALLOC_PTR_GFP(pcci, pcc_inode_slab, GFP_NOFS);
2125 GOTO(out_put, rc = -ENOMEM);
2127 rc = pcc_inode_store_ugpid(pcc_dentry, old_cred->suid,
2132 pcc_inode_init(pcci, ll_i2info(inode));
2133 pcc_inode_attach_init(dataset, pcci, pcc_dentry, LU_PCC_READWRITE);
2135 rc = pcc_layout_xattr_set(pcci, 0);
2137 (void) pcc_inode_remove(inode, pcci->pcci_path.dentry);
2138 pcc_inode_put(pcci);
2139 GOTO(out_unlock, rc);
2142 /* Set the layout generation of newly created file with 0 */
2143 pcc_layout_gen_set(pcci, 0);
2147 (void) pcc_inode_remove(inode, pcc_dentry);
2151 OBD_SLAB_FREE_PTR(pcci, pcc_inode_slab);
2154 pcc_inode_unlock(inode);
2155 revert_creds(old_cred);
2159 static int pcc_filp_write(struct file *filp, const void *buf, ssize_t count,
2165 size = vfs_write(filp, (const void __user *)buf, count, offset);
2174 static int pcc_copy_data(struct file *src, struct file *dst)
2179 loff_t pos, offset = 0;
2180 size_t buf_len = 1048576;
2185 OBD_ALLOC_LARGE(buf, buf_len);
2193 rc2 = vfs_read(src, (void __user *)buf, buf_len, &pos);
2195 GOTO(out_fs, rc = rc2);
2200 rc = pcc_filp_write(dst, buf, rc2, &pos);
2208 OBD_FREE_LARGE(buf, buf_len);
2212 static int pcc_attach_allowed_check(struct inode *inode)
2214 struct ll_inode_info *lli = ll_i2info(inode);
2215 struct pcc_inode *pcci;
2220 pcc_inode_lock(inode);
2221 if (lli->lli_pcc_state & PCC_STATE_FL_ATTACHING)
2222 GOTO(out_unlock, rc = -EBUSY);
2224 pcci = ll_i2pcci(inode);
2225 if (pcci && pcc_inode_has_layout(pcci))
2226 GOTO(out_unlock, rc = -EEXIST);
2228 lli->lli_pcc_state |= PCC_STATE_FL_ATTACHING;
2230 pcc_inode_unlock(inode);
2234 int pcc_readwrite_attach(struct file *file, struct inode *inode,
2237 struct pcc_dataset *dataset;
2238 struct ll_inode_info *lli = ll_i2info(inode);
2239 struct pcc_inode *pcci;
2240 const struct cred *old_cred;
2241 struct dentry *dentry;
2242 struct file *pcc_filp;
2248 rc = pcc_attach_allowed_check(inode);
2252 dataset = pcc_dataset_get(&ll_i2sbi(inode)->ll_pcc_super,
2253 LU_PCC_READWRITE, archive_id);
2254 if (dataset == NULL)
2257 old_cred = override_creds(pcc_super_cred(inode->i_sb));
2258 rc = __pcc_inode_create(dataset, &lli->lli_fid, &dentry);
2260 revert_creds(old_cred);
2261 GOTO(out_dataset_put, rc);
2264 path.mnt = dataset->pccd_path.mnt;
2265 path.dentry = dentry;
2266 #ifdef HAVE_DENTRY_OPEN_USE_PATH
2267 pcc_filp = dentry_open(&path, O_TRUNC | O_WRONLY | O_LARGEFILE,
2270 pcc_filp = dentry_open(path.dentry, path.mnt,
2271 O_TRUNC | O_WRONLY | O_LARGEFILE,
2274 if (IS_ERR_OR_NULL(pcc_filp)) {
2275 rc = pcc_filp == NULL ? -EINVAL : PTR_ERR(pcc_filp);
2276 revert_creds(old_cred);
2277 GOTO(out_dentry, rc);
2280 rc = pcc_inode_store_ugpid(dentry, old_cred->uid, old_cred->gid);
2281 revert_creds(old_cred);
2285 rc = pcc_copy_data(file, pcc_filp);
2289 /* Pause to allow for a race with concurrent HSM remove */
2290 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_PCC_ATTACH_PAUSE, cfs_fail_val);
2292 pcc_inode_lock(inode);
2293 pcci = ll_i2pcci(inode);
2295 OBD_SLAB_ALLOC_PTR_GFP(pcci, pcc_inode_slab, GFP_NOFS);
2297 GOTO(out_unlock, rc = -ENOMEM);
2299 pcc_inode_init(pcci, lli);
2300 pcc_inode_attach_init(dataset, pcci, dentry, LU_PCC_READWRITE);
2302 pcc_inode_unlock(inode);
2307 old_cred = override_creds(pcc_super_cred(inode->i_sb));
2308 (void) pcc_inode_remove(inode, dentry);
2309 revert_creds(old_cred);
2313 pcc_dataset_put(dataset);
2317 int pcc_readwrite_attach_fini(struct file *file, struct inode *inode,
2318 __u32 gen, bool lease_broken, int rc,
2321 struct ll_inode_info *lli = ll_i2info(inode);
2322 const struct cred *old_cred;
2323 struct pcc_inode *pcci;
2328 old_cred = override_creds(pcc_super_cred(inode->i_sb));
2329 pcc_inode_lock(inode);
2330 pcci = ll_i2pcci(inode);
2331 lli->lli_pcc_state &= ~PCC_STATE_FL_ATTACHING;
2332 if (rc || lease_broken) {
2333 if (attached && pcci)
2334 pcc_inode_put(pcci);
2336 GOTO(out_unlock, rc);
2339 /* PCC inode may be released due to layout lock revocatioin */
2341 GOTO(out_unlock, rc = -ESTALE);
2344 rc = pcc_layout_xattr_set(pcci, gen);
2348 rc = ll_layout_refresh(inode, &gen2);
2351 pcc_layout_gen_set(pcci, gen);
2354 DFID" layout changed from %d to %d.\n",
2355 PFID(ll_inode2fid(inode)), gen, gen2);
2356 GOTO(out_put, rc = -ESTALE);
2362 (void) pcc_inode_remove(inode, pcci->pcci_path.dentry);
2363 pcc_inode_put(pcci);
2366 pcc_inode_unlock(inode);
2367 revert_creds(old_cred);
2371 int pcc_ioctl_detach(struct inode *inode)
2373 struct ll_inode_info *lli = ll_i2info(inode);
2374 struct pcc_inode *pcci;
2379 pcc_inode_lock(inode);
2380 pcci = lli->lli_pcc_inode;
2381 if (!pcci || lli->lli_pcc_state & PCC_STATE_FL_ATTACHING ||
2382 !pcc_inode_has_layout(pcci))
2383 GOTO(out_unlock, rc = 0);
2385 __pcc_layout_invalidate(pcci);
2386 pcc_inode_put(pcci);
2389 pcc_inode_unlock(inode);
2393 int pcc_ioctl_state(struct file *file, struct inode *inode,
2394 struct lu_pcc_state *state)
2400 int buf_len = sizeof(state->pccs_path);
2401 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2402 struct pcc_file *pccf = &fd->fd_pcc_file;
2403 struct pcc_inode *pcci;
2410 OBD_ALLOC(buf, buf_len);
2414 pcc_inode_lock(inode);
2415 pcci = ll_i2pcci(inode);
2417 state->pccs_type = LU_PCC_NONE;
2418 GOTO(out_unlock, rc = 0);
2421 count = atomic_read(&pcci->pcci_refcount);
2423 state->pccs_type = LU_PCC_NONE;
2424 state->pccs_open_count = 0;
2425 GOTO(out_unlock, rc = 0);
2428 if (pcc_inode_has_layout(pcci))
2430 if (pccf->pccf_file != NULL)
2432 state->pccs_type = pcci->pcci_type;
2433 state->pccs_open_count = count;
2434 state->pccs_flags = ll_i2info(inode)->lli_pcc_state;
2435 #ifdef HAVE_DENTRY_PATH_RAW
2436 path = dentry_path_raw(pcci->pcci_path.dentry, buf, buf_len);
2438 GOTO(out_unlock, rc = PTR_ERR(path));
2443 if (strlcpy(state->pccs_path, path, buf_len) >= buf_len)
2444 GOTO(out_unlock, rc = -ENAMETOOLONG);
2447 pcc_inode_unlock(inode);
2448 OBD_FREE(buf, buf_len);