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 spin_lock_init(&super->pccs_lock);
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;
470 pcc_parse_value_pairs(struct pcc_cmd *cmd, char *buffer)
477 while (val != NULL && strlen(val) != 0) {
478 token = strsep(&val, " ");
479 rc = pcc_parse_value_pair(cmd, token);
488 pcc_dataset_rule_fini(struct pcc_match_rule *rule)
490 if (!list_empty(&rule->pmr_conds))
491 pcc_rule_conds_free(&rule->pmr_conds);
492 LASSERT(rule->pmr_conds_str != NULL);
493 OBD_FREE(rule->pmr_conds_str, strlen(rule->pmr_conds_str) + 1);
497 pcc_dataset_rule_init(struct pcc_match_rule *rule, struct pcc_cmd *cmd)
501 LASSERT(cmd->u.pccc_add.pccc_conds_str);
502 OBD_ALLOC(rule->pmr_conds_str,
503 strlen(cmd->u.pccc_add.pccc_conds_str) + 1);
504 if (rule->pmr_conds_str == NULL)
507 memcpy(rule->pmr_conds_str,
508 cmd->u.pccc_add.pccc_conds_str,
509 strlen(cmd->u.pccc_add.pccc_conds_str));
511 INIT_LIST_HEAD(&rule->pmr_conds);
512 if (!list_empty(&cmd->u.pccc_add.pccc_conds))
513 rc = pcc_conds_parse(rule->pmr_conds_str,
514 strlen(rule->pmr_conds_str),
518 pcc_dataset_rule_fini(rule);
525 pcc_id_list_match(struct list_head *id_list, __u32 id_val)
527 struct pcc_match_id *id;
529 list_for_each_entry(id, id_list, pmi_linkage) {
530 if (id->pmi_id == id_val)
537 cfs_match_wildcard(const char *pattern, const char *content)
539 if (*pattern == '\0' && *content == '\0')
542 if (*pattern == '*' && *(pattern + 1) != '\0' && *content == '\0')
545 while (*pattern == *content) {
548 if (*pattern == '\0' && *content == '\0')
551 if (*pattern == '*' && *(pattern + 1) != '\0' &&
557 return (cfs_match_wildcard(pattern + 1, content) ||
558 cfs_match_wildcard(pattern, content + 1));
564 pcc_fname_list_match(struct list_head *fname_list, const char *name)
566 struct pcc_match_fname *fname;
568 list_for_each_entry(fname, fname_list, pmf_linkage) {
569 if (cfs_match_wildcard(fname->pmf_name, name))
576 pcc_expression_match(struct pcc_expression *expr, struct pcc_matcher *matcher)
578 switch (expr->pe_field) {
580 return pcc_id_list_match(&expr->pe_cond, matcher->pm_uid);
582 return pcc_id_list_match(&expr->pe_cond, matcher->pm_gid);
583 case PCC_FIELD_PROJID:
584 return pcc_id_list_match(&expr->pe_cond, matcher->pm_projid);
585 case PCC_FIELD_FNAME:
586 return pcc_fname_list_match(&expr->pe_cond,
587 matcher->pm_name->name);
594 pcc_conjunction_match(struct pcc_conjunction *conjunction,
595 struct pcc_matcher *matcher)
597 struct pcc_expression *expr;
600 list_for_each_entry(expr, &conjunction->pc_expressions, pe_linkage) {
601 matched = pcc_expression_match(expr, matcher);
610 pcc_cond_match(struct pcc_match_rule *rule, struct pcc_matcher *matcher)
612 struct pcc_conjunction *conjunction;
615 list_for_each_entry(conjunction, &rule->pmr_conds, pc_linkage) {
616 matched = pcc_conjunction_match(conjunction, matcher);
625 pcc_dataset_match_get(struct pcc_super *super, struct pcc_matcher *matcher)
627 struct pcc_dataset *dataset;
628 struct pcc_dataset *selected = NULL;
630 spin_lock(&super->pccs_lock);
631 list_for_each_entry(dataset, &super->pccs_datasets, pccd_linkage) {
632 if (pcc_cond_match(&dataset->pccd_rule, matcher)) {
633 atomic_inc(&dataset->pccd_refcount);
638 spin_unlock(&super->pccs_lock);
640 CDEBUG(D_CACHE, "PCC create, matched %s - %d:%d:%d:%s\n",
641 dataset->pccd_rule.pmr_conds_str,
642 matcher->pm_uid, matcher->pm_gid,
643 matcher->pm_projid, matcher->pm_name->name);
649 * pcc_dataset_add - Add a Cache policy to control which files need be
650 * cached and where it will be cached.
652 * @super: superblock of pcc
656 pcc_dataset_add(struct pcc_super *super, struct pcc_cmd *cmd)
658 char *pathname = cmd->pccc_pathname;
659 struct pcc_dataset *dataset;
660 struct pcc_dataset *tmp;
664 OBD_ALLOC_PTR(dataset);
668 rc = kern_path(pathname, LOOKUP_DIRECTORY, &dataset->pccd_path);
670 OBD_FREE_PTR(dataset);
673 strncpy(dataset->pccd_pathname, pathname, PATH_MAX);
674 dataset->pccd_rwid = cmd->u.pccc_add.pccc_rwid;
675 dataset->pccd_roid = cmd->u.pccc_add.pccc_roid;
676 atomic_set(&dataset->pccd_refcount, 1);
678 rc = pcc_dataset_rule_init(&dataset->pccd_rule, cmd);
680 pcc_dataset_put(dataset);
684 spin_lock(&super->pccs_lock);
685 list_for_each_entry(tmp, &super->pccs_datasets, pccd_linkage) {
686 if (strcmp(tmp->pccd_pathname, pathname) == 0 ||
687 (dataset->pccd_rwid != 0 &&
688 dataset->pccd_rwid == tmp->pccd_rwid) ||
689 (dataset->pccd_roid != 0 &&
690 dataset->pccd_roid == tmp->pccd_roid)) {
696 list_add(&dataset->pccd_linkage, &super->pccs_datasets);
697 spin_unlock(&super->pccs_lock);
700 pcc_dataset_put(dataset);
708 pcc_dataset_get(struct pcc_super *super, enum lu_pcc_type type, __u32 id)
710 struct pcc_dataset *dataset;
711 struct pcc_dataset *selected = NULL;
717 * archive ID (read-write ID) or read-only ID is unique in the list,
718 * we just return last added one as first priority.
720 spin_lock(&super->pccs_lock);
721 list_for_each_entry(dataset, &super->pccs_datasets, pccd_linkage) {
722 if (type == LU_PCC_READWRITE && dataset->pccd_rwid != id)
724 atomic_inc(&dataset->pccd_refcount);
728 spin_unlock(&super->pccs_lock);
730 CDEBUG(D_CACHE, "matched id %u, PCC mode %d\n", id, type);
736 pcc_dataset_put(struct pcc_dataset *dataset)
738 if (atomic_dec_and_test(&dataset->pccd_refcount)) {
739 pcc_dataset_rule_fini(&dataset->pccd_rule);
740 path_put(&dataset->pccd_path);
741 OBD_FREE_PTR(dataset);
746 pcc_dataset_del(struct pcc_super *super, char *pathname)
748 struct list_head *l, *tmp;
749 struct pcc_dataset *dataset;
752 spin_lock(&super->pccs_lock);
753 list_for_each_safe(l, tmp, &super->pccs_datasets) {
754 dataset = list_entry(l, struct pcc_dataset, pccd_linkage);
755 if (strcmp(dataset->pccd_pathname, pathname) == 0) {
756 list_del(&dataset->pccd_linkage);
757 pcc_dataset_put(dataset);
762 spin_unlock(&super->pccs_lock);
767 pcc_dataset_dump(struct pcc_dataset *dataset, struct seq_file *m)
769 seq_printf(m, "%s:\n", dataset->pccd_pathname);
770 seq_printf(m, " rwid: %u\n", dataset->pccd_rwid);
771 seq_printf(m, " autocache: %s\n", dataset->pccd_rule.pmr_conds_str);
775 pcc_super_dump(struct pcc_super *super, struct seq_file *m)
777 struct pcc_dataset *dataset;
779 spin_lock(&super->pccs_lock);
780 list_for_each_entry(dataset, &super->pccs_datasets, pccd_linkage) {
781 pcc_dataset_dump(dataset, m);
783 spin_unlock(&super->pccs_lock);
787 static void pcc_remove_datasets(struct pcc_super *super)
789 struct pcc_dataset *dataset, *tmp;
791 list_for_each_entry_safe(dataset, tmp,
792 &super->pccs_datasets, pccd_linkage) {
793 list_del(&dataset->pccd_linkage);
794 pcc_dataset_put(dataset);
798 void pcc_super_fini(struct pcc_super *super)
800 pcc_remove_datasets(super);
801 put_cred(super->pccs_cred);
804 static bool pathname_is_valid(const char *pathname)
806 /* Needs to be absolute path */
807 if (pathname == NULL || strlen(pathname) == 0 ||
808 strlen(pathname) >= PATH_MAX || pathname[0] != '/')
813 static struct pcc_cmd *
814 pcc_cmd_parse(char *buffer, unsigned long count)
816 static struct pcc_cmd *cmd;
823 GOTO(out, rc = -ENOMEM);
825 /* clear all setting */
826 if (strncmp(buffer, "clear", 5) == 0) {
827 cmd->pccc_cmd = PCC_CLEAR_ALL;
832 token = strsep(&val, " ");
833 if (val == NULL || strlen(val) == 0)
834 GOTO(out_free_cmd, rc = -EINVAL);
836 /* Type of the command */
837 if (strcmp(token, "add") == 0)
838 cmd->pccc_cmd = PCC_ADD_DATASET;
839 else if (strcmp(token, "del") == 0)
840 cmd->pccc_cmd = PCC_DEL_DATASET;
842 GOTO(out_free_cmd, rc = -EINVAL);
844 /* Pathname of the dataset */
845 token = strsep(&val, " ");
846 if ((val == NULL && cmd->pccc_cmd != PCC_DEL_DATASET) ||
847 !pathname_is_valid(token))
848 GOTO(out_free_cmd, rc = -EINVAL);
849 cmd->pccc_pathname = token;
851 if (cmd->pccc_cmd == PCC_ADD_DATASET) {
855 val = strrchr(token, '}');
857 GOTO(out_free_cmd, rc = -EINVAL);
863 } else if (*val == ' ') {
867 GOTO(out_free_cmd, rc = -EINVAL);
870 rc = pcc_id_parse(cmd, token);
872 GOTO(out_free_cmd, rc);
874 rc = pcc_parse_value_pairs(cmd, val);
876 GOTO(out_cmd_fini, rc = -EINVAL);
889 int pcc_cmd_handle(char *buffer, unsigned long count,
890 struct pcc_super *super)
895 cmd = pcc_cmd_parse(buffer, count);
899 switch (cmd->pccc_cmd) {
900 case PCC_ADD_DATASET:
901 rc = pcc_dataset_add(super, cmd);
903 case PCC_DEL_DATASET:
904 rc = pcc_dataset_del(super, cmd->pccc_pathname);
907 pcc_remove_datasets(super);
919 static inline void pcc_inode_lock(struct inode *inode)
921 mutex_lock(&ll_i2info(inode)->lli_pcc_lock);
924 static inline void pcc_inode_unlock(struct inode *inode)
926 mutex_unlock(&ll_i2info(inode)->lli_pcc_lock);
929 static void pcc_inode_init(struct pcc_inode *pcci, struct ll_inode_info *lli)
931 pcci->pcci_lli = lli;
932 lli->lli_pcc_inode = pcci;
933 atomic_set(&pcci->pcci_refcount, 0);
934 pcci->pcci_type = LU_PCC_NONE;
935 pcci->pcci_layout_gen = CL_LAYOUT_GEN_NONE;
936 atomic_set(&pcci->pcci_active_ios, 0);
937 init_waitqueue_head(&pcci->pcci_waitq);
940 static void pcc_inode_fini(struct pcc_inode *pcci)
942 struct ll_inode_info *lli = pcci->pcci_lli;
944 path_put(&pcci->pcci_path);
945 pcci->pcci_type = LU_PCC_NONE;
946 OBD_SLAB_FREE_PTR(pcci, pcc_inode_slab);
947 lli->lli_pcc_inode = NULL;
950 static void pcc_inode_get(struct pcc_inode *pcci)
952 atomic_inc(&pcci->pcci_refcount);
955 static void pcc_inode_put(struct pcc_inode *pcci)
957 if (atomic_dec_and_test(&pcci->pcci_refcount))
958 pcc_inode_fini(pcci);
961 void pcc_inode_free(struct inode *inode)
963 struct pcc_inode *pcci = ll_i2pcci(inode);
966 WARN_ON(atomic_read(&pcci->pcci_refcount) > 1);
973 * As Andreas suggested, we'd better use new layout to
975 * (fid->f_oid >> 16 & oxFFFF)/FID
977 #define MAX_PCC_DATABASE_PATH (6 * 5 + FID_NOBRACE_LEN + 1)
978 static int pcc_fid2dataset_path(char *buf, int sz, struct lu_fid *fid)
980 return snprintf(buf, sz, "%04x/%04x/%04x/%04x/%04x/%04x/"
982 (fid)->f_oid & 0xFFFF,
983 (fid)->f_oid >> 16 & 0xFFFF,
984 (unsigned int)((fid)->f_seq & 0xFFFF),
985 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
986 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
987 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
991 static inline const struct cred *pcc_super_cred(struct super_block *sb)
993 return ll_s2sbi(sb)->ll_pcc_super.pccs_cred;
996 void pcc_file_init(struct pcc_file *pccf)
998 pccf->pccf_file = NULL;
999 pccf->pccf_type = LU_PCC_NONE;
1002 static inline bool pcc_inode_has_layout(struct pcc_inode *pcci)
1004 return pcci->pcci_layout_gen != CL_LAYOUT_GEN_NONE;
1007 int pcc_file_open(struct inode *inode, struct file *file)
1009 struct pcc_inode *pcci;
1010 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1011 struct pcc_file *pccf = &fd->fd_pcc_file;
1012 struct file *pcc_file;
1019 if (!S_ISREG(inode->i_mode))
1022 pcc_inode_lock(inode);
1023 pcci = ll_i2pcci(inode);
1025 GOTO(out_unlock, rc = 0);
1027 if (atomic_read(&pcci->pcci_refcount) == 0 ||
1028 !pcc_inode_has_layout(pcci))
1029 GOTO(out_unlock, rc = 0);
1031 pcc_inode_get(pcci);
1032 WARN_ON(pccf->pccf_file);
1034 path = &pcci->pcci_path;
1035 dname = &path->dentry->d_name;
1036 CDEBUG(D_CACHE, "opening pcc file '%.*s'\n", dname->len,
1039 #ifdef HAVE_DENTRY_OPEN_USE_PATH
1040 pcc_file = dentry_open(path, file->f_flags,
1041 pcc_super_cred(inode->i_sb));
1043 pcc_file = dentry_open(path->dentry, path->mnt, file->f_flags,
1044 pcc_super_cred(inode->i_sb));
1046 if (IS_ERR_OR_NULL(pcc_file)) {
1047 rc = pcc_file == NULL ? -EINVAL : PTR_ERR(pcc_file);
1048 pcc_inode_put(pcci);
1050 pccf->pccf_file = pcc_file;
1051 pccf->pccf_type = pcci->pcci_type;
1055 pcc_inode_unlock(inode);
1059 void pcc_file_release(struct inode *inode, struct file *file)
1061 struct pcc_inode *pcci;
1062 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1063 struct pcc_file *pccf;
1069 if (!S_ISREG(inode->i_mode) || fd == NULL)
1072 pccf = &fd->fd_pcc_file;
1073 pcc_inode_lock(inode);
1074 if (pccf->pccf_file == NULL)
1077 pcci = ll_i2pcci(inode);
1079 path = &pcci->pcci_path;
1080 dname = &path->dentry->d_name;
1081 CDEBUG(D_CACHE, "releasing pcc file \"%.*s\"\n", dname->len,
1083 pcc_inode_put(pcci);
1084 fput(pccf->pccf_file);
1085 pccf->pccf_file = NULL;
1087 pcc_inode_unlock(inode);
1091 static inline void pcc_layout_gen_set(struct pcc_inode *pcci,
1094 pcci->pcci_layout_gen = gen;
1097 static void pcc_io_init(struct inode *inode, bool *cached)
1099 struct pcc_inode *pcci;
1101 pcc_inode_lock(inode);
1102 pcci = ll_i2pcci(inode);
1103 if (pcci && pcc_inode_has_layout(pcci)) {
1104 LASSERT(atomic_read(&pcci->pcci_refcount) > 0);
1105 atomic_inc(&pcci->pcci_active_ios);
1110 pcc_inode_unlock(inode);
1113 static void pcc_io_fini(struct inode *inode)
1115 struct pcc_inode *pcci = ll_i2pcci(inode);
1117 LASSERT(pcci && atomic_read(&pcci->pcci_active_ios) > 0);
1118 if (atomic_dec_and_test(&pcci->pcci_active_ios))
1119 wake_up_all(&pcci->pcci_waitq);
1124 __pcc_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
1126 struct file *file = iocb->ki_filp;
1128 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
1129 return file->f_op->read_iter(iocb, iter);
1135 iov_for_each(iov, i, *iter) {
1138 res = file->f_op->aio_read(iocb, &iov, 1, iocb->ki_pos);
1139 if (-EIOCBQUEUED == res)
1140 res = wait_on_sync_kiocb(iocb);
1148 if (res < iov.iov_len)
1153 iov_iter_advance(iter, bytes);
1158 ssize_t pcc_file_read_iter(struct kiocb *iocb,
1159 struct iov_iter *iter, bool *cached)
1161 struct file *file = iocb->ki_filp;
1162 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1163 struct pcc_file *pccf = &fd->fd_pcc_file;
1164 struct inode *inode = file_inode(file);
1169 if (pccf->pccf_file == NULL) {
1174 pcc_io_init(inode, cached);
1178 iocb->ki_filp = pccf->pccf_file;
1179 /* generic_file_aio_read does not support ext4-dax,
1180 * __pcc_file_read_iter uses ->aio_read hook directly
1181 * to add support for ext4-dax.
1183 result = __pcc_file_read_iter(iocb, iter);
1184 iocb->ki_filp = file;
1191 __pcc_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
1193 struct file *file = iocb->ki_filp;
1195 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
1196 return file->f_op->write_iter(iocb, iter);
1202 iov_for_each(iov, i, *iter) {
1205 res = file->f_op->aio_write(iocb, &iov, 1, iocb->ki_pos);
1206 if (-EIOCBQUEUED == res)
1207 res = wait_on_sync_kiocb(iocb);
1215 if (res < iov.iov_len)
1220 iov_iter_advance(iter, bytes);
1225 ssize_t pcc_file_write_iter(struct kiocb *iocb,
1226 struct iov_iter *iter, bool *cached)
1228 struct file *file = iocb->ki_filp;
1229 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1230 struct pcc_file *pccf = &fd->fd_pcc_file;
1231 struct inode *inode = file_inode(file);
1236 if (pccf->pccf_file == NULL) {
1241 if (pccf->pccf_type != LU_PCC_READWRITE) {
1246 pcc_io_init(inode, cached);
1250 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_PCC_FAKE_ERROR))
1251 GOTO(out, result = -ENOSPC);
1253 iocb->ki_filp = pccf->pccf_file;
1255 /* Since __pcc_file_write_iter makes write calls via
1256 * the normal vfs interface to the local PCC file system,
1257 * the inode lock is not needed.
1259 result = __pcc_file_write_iter(iocb, iter);
1260 iocb->ki_filp = file;
1266 int pcc_inode_setattr(struct inode *inode, struct iattr *attr,
1270 const struct cred *old_cred;
1271 struct iattr attr2 = *attr;
1272 struct dentry *pcc_dentry;
1273 struct pcc_inode *pcci;
1277 if (!S_ISREG(inode->i_mode)) {
1282 pcc_io_init(inode, cached);
1286 attr2.ia_valid = attr->ia_valid & (ATTR_SIZE | ATTR_ATIME |
1287 ATTR_ATIME_SET | ATTR_MTIME | ATTR_MTIME_SET |
1288 ATTR_CTIME | ATTR_UID | ATTR_GID);
1289 pcci = ll_i2pcci(inode);
1290 pcc_dentry = pcci->pcci_path.dentry;
1291 inode_lock(pcc_dentry->d_inode);
1292 old_cred = override_creds(pcc_super_cred(inode->i_sb));
1293 rc = pcc_dentry->d_inode->i_op->setattr(pcc_dentry, &attr2);
1294 revert_creds(old_cred);
1295 inode_unlock(pcc_dentry->d_inode);
1301 int pcc_inode_getattr(struct inode *inode, bool *cached)
1303 struct ll_inode_info *lli = ll_i2info(inode);
1304 const struct cred *old_cred;
1313 if (!S_ISREG(inode->i_mode)) {
1318 pcc_io_init(inode, cached);
1322 old_cred = override_creds(pcc_super_cred(inode->i_sb));
1323 rc = ll_vfs_getattr(&ll_i2pcci(inode)->pcci_path, &stat);
1324 revert_creds(old_cred);
1328 ll_inode_size_lock(inode);
1329 if (inode->i_atime.tv_sec < lli->lli_atime ||
1330 lli->lli_update_atime) {
1331 inode->i_atime.tv_sec = lli->lli_atime;
1332 lli->lli_update_atime = 0;
1334 inode->i_mtime.tv_sec = lli->lli_mtime;
1335 inode->i_ctime.tv_sec = lli->lli_ctime;
1337 atime = inode->i_atime.tv_sec;
1338 mtime = inode->i_mtime.tv_sec;
1339 ctime = inode->i_ctime.tv_sec;
1341 if (atime < stat.atime.tv_sec)
1342 atime = stat.atime.tv_sec;
1344 if (ctime < stat.ctime.tv_sec)
1345 ctime = stat.ctime.tv_sec;
1347 if (mtime < stat.mtime.tv_sec)
1348 mtime = stat.mtime.tv_sec;
1350 i_size_write(inode, stat.size);
1351 inode->i_blocks = stat.blocks;
1353 inode->i_atime.tv_sec = atime;
1354 inode->i_mtime.tv_sec = mtime;
1355 inode->i_ctime.tv_sec = ctime;
1357 ll_inode_size_unlock(inode);
1363 ssize_t pcc_file_splice_read(struct file *in_file, loff_t *ppos,
1364 struct pipe_inode_info *pipe,
1365 size_t count, unsigned int flags,
1368 struct inode *inode = file_inode(in_file);
1369 struct ll_file_data *fd = LUSTRE_FPRIVATE(in_file);
1370 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1379 if (!file_inode(pcc_file)->i_fop->splice_read)
1382 pcc_io_init(inode, cached);
1386 result = file_inode(pcc_file)->i_fop->splice_read(pcc_file,
1394 int pcc_fsync(struct file *file, loff_t start, loff_t end,
1395 int datasync, bool *cached)
1397 struct inode *inode = file_inode(file);
1398 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1399 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1409 pcc_io_init(inode, cached);
1413 #ifdef HAVE_FILE_FSYNC_4ARGS
1414 rc = file_inode(pcc_file)->i_fop->fsync(pcc_file,
1415 start, end, datasync);
1416 #elif defined(HAVE_FILE_FSYNC_2ARGS)
1417 rc = file_inode(pcc_file)->i_fop->fsync(pcc_file, datasync);
1419 rc = file_inode(pcc_file)->i_fop->fsync(pcc_file,
1420 file_dentry(dentry), datasync);
1427 int pcc_file_mmap(struct file *file, struct vm_area_struct *vma,
1430 struct inode *inode = file_inode(file);
1431 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1432 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1433 struct pcc_inode *pcci;
1438 if (!pcc_file || !file_inode(pcc_file)->i_fop->mmap) {
1443 pcc_inode_lock(inode);
1444 pcci = ll_i2pcci(inode);
1445 if (pcci && pcc_inode_has_layout(pcci)) {
1446 LASSERT(atomic_read(&pcci->pcci_refcount) > 1);
1448 vma->vm_file = pcc_file;
1449 rc = file_inode(pcc_file)->i_fop->mmap(pcc_file, vma);
1450 vma->vm_file = file;
1451 /* Save the vm ops of backend PCC */
1452 vma->vm_private_data = (void *)vma->vm_ops;
1456 pcc_inode_unlock(inode);
1461 void pcc_vm_open(struct vm_area_struct *vma)
1463 struct pcc_inode *pcci;
1464 struct file *file = vma->vm_file;
1465 struct inode *inode = file_inode(file);
1466 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1467 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1468 struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
1472 if (!pcc_file || !pcc_vm_ops || !pcc_vm_ops->open)
1475 pcc_inode_lock(inode);
1476 pcci = ll_i2pcci(inode);
1477 if (pcci && pcc_inode_has_layout(pcci)) {
1478 vma->vm_file = pcc_file;
1479 pcc_vm_ops->open(vma);
1480 vma->vm_file = file;
1482 pcc_inode_unlock(inode);
1486 void pcc_vm_close(struct vm_area_struct *vma)
1488 struct file *file = vma->vm_file;
1489 struct inode *inode = file_inode(file);
1490 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1491 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1492 struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
1496 if (!pcc_file || !pcc_vm_ops || !pcc_vm_ops->close)
1499 pcc_inode_lock(inode);
1500 /* Layout lock maybe revoked here */
1501 vma->vm_file = pcc_file;
1502 pcc_vm_ops->close(vma);
1503 vma->vm_file = file;
1504 pcc_inode_unlock(inode);
1508 int pcc_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
1511 struct page *page = vmf->page;
1512 struct mm_struct *mm = vma->vm_mm;
1513 struct file *file = vma->vm_file;
1514 struct inode *inode = file_inode(file);
1515 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1516 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1517 struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
1522 if (!pcc_file || !pcc_vm_ops || !pcc_vm_ops->page_mkwrite) {
1527 /* Pause to allow for a race with concurrent detach */
1528 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_PCC_MKWRITE_PAUSE, cfs_fail_val);
1530 pcc_io_init(inode, cached);
1532 /* This happens when the file is detached from PCC after got
1533 * the fault page via ->fault() on the inode of the PCC copy.
1534 * Here it can not simply fall back to normal Lustre I/O path.
1535 * The reason is that the address space of fault page used by
1536 * ->page_mkwrite() is still the one of PCC inode. In the
1537 * normal Lustre ->page_mkwrite() I/O path, it will be wrongly
1538 * handled as the address space of the fault page is not
1539 * consistent with the one of the Lustre inode (though the
1540 * fault page was truncated).
1541 * As the file is detached from PCC, the fault page must
1542 * be released frist, and retry the mmap write (->fault() and
1544 * We use an ugly and tricky method by returning
1545 * VM_FAULT_NOPAGE | VM_FAULT_RETRY to the caller
1546 * __do_page_fault and retry the memory fault handling.
1548 if (page->mapping == file_inode(pcc_file)->i_mapping) {
1550 up_read(&mm->mmap_sem);
1551 RETURN(VM_FAULT_RETRY | VM_FAULT_NOPAGE);
1558 * This fault injection can also be used to simulate -ENOSPC and
1559 * -EDQUOT failure of underlying PCC backend fs.
1561 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_PCC_DETACH_MKWRITE)) {
1563 pcc_ioctl_detach(inode);
1564 up_read(&mm->mmap_sem);
1565 RETURN(VM_FAULT_RETRY | VM_FAULT_NOPAGE);
1568 vma->vm_file = pcc_file;
1569 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
1570 rc = pcc_vm_ops->page_mkwrite(vmf);
1572 rc = pcc_vm_ops->page_mkwrite(vma, vmf);
1574 vma->vm_file = file;
1580 int pcc_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
1583 struct file *file = vma->vm_file;
1584 struct inode *inode = file_inode(file);
1585 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1586 struct file *pcc_file = fd->fd_pcc_file.pccf_file;
1587 struct vm_operations_struct *pcc_vm_ops = vma->vm_private_data;
1592 if (!pcc_file || !pcc_vm_ops || !pcc_vm_ops->fault) {
1597 pcc_io_init(inode, cached);
1601 vma->vm_file = pcc_file;
1602 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
1603 rc = pcc_vm_ops->fault(vmf);
1605 rc = pcc_vm_ops->fault(vma, vmf);
1607 vma->vm_file = file;
1613 static void pcc_layout_wait(struct pcc_inode *pcci)
1615 struct l_wait_info lwi = { 0 };
1617 while (atomic_read(&pcci->pcci_active_ios) > 0) {
1618 CDEBUG(D_CACHE, "Waiting for IO completion: %d\n",
1619 atomic_read(&pcci->pcci_active_ios));
1620 l_wait_event(pcci->pcci_waitq,
1621 atomic_read(&pcci->pcci_active_ios) == 0, &lwi);
1625 static void __pcc_layout_invalidate(struct pcc_inode *pcci)
1627 pcci->pcci_type = LU_PCC_NONE;
1628 pcc_layout_gen_set(pcci, CL_LAYOUT_GEN_NONE);
1629 pcc_layout_wait(pcci);
1632 void pcc_layout_invalidate(struct inode *inode)
1634 struct pcc_inode *pcci;
1636 pcc_inode_lock(inode);
1637 pcci = ll_i2pcci(inode);
1638 if (pcci && pcc_inode_has_layout(pcci)) {
1639 LASSERT(atomic_read(&pcci->pcci_refcount) > 0);
1640 __pcc_layout_invalidate(pcci);
1642 CDEBUG(D_CACHE, "Invalidate "DFID" layout gen %d\n",
1643 PFID(&ll_i2info(inode)->lli_fid), pcci->pcci_layout_gen);
1645 pcc_inode_put(pcci);
1647 pcc_inode_unlock(inode);
1650 static int pcc_inode_remove(struct pcc_inode *pcci)
1652 struct dentry *dentry;
1655 dentry = pcci->pcci_path.dentry;
1656 rc = ll_vfs_unlink(dentry->d_parent->d_inode, dentry);
1658 CWARN("failed to unlink PCC file %.*s, rc = %d\n",
1659 dentry->d_name.len, dentry->d_name.name, rc);
1664 /* Create directory under base if directory does not exist */
1665 static struct dentry *
1666 pcc_mkdir(struct dentry *base, const char *name, umode_t mode)
1669 struct dentry *dentry;
1670 struct inode *dir = base->d_inode;
1673 dentry = lookup_one_len(name, base, strlen(name));
1677 if (d_is_positive(dentry))
1680 rc = vfs_mkdir(dir, dentry, mode);
1683 dentry = ERR_PTR(rc);
1691 static struct dentry *
1692 pcc_mkdir_p(struct dentry *root, char *path, umode_t mode)
1694 char *ptr, *entry_name;
1695 struct dentry *parent;
1696 struct dentry *child = ERR_PTR(-EINVAL);
1703 parent = dget(root);
1704 while ((ptr = strchr(ptr, '/')) != NULL) {
1706 child = pcc_mkdir(parent, entry_name, mode);
1720 /* Create file under base. If file already exist, return failure */
1721 static struct dentry *
1722 pcc_create(struct dentry *base, const char *name, umode_t mode)
1725 struct dentry *dentry;
1726 struct inode *dir = base->d_inode;
1729 dentry = lookup_one_len(name, base, strlen(name));
1733 if (d_is_positive(dentry))
1736 rc = vfs_create(dir, dentry, mode, LL_VFS_CREATE_FALSE);
1739 dentry = ERR_PTR(rc);
1747 /* Must be called with pcci->pcci_lock held */
1748 static void pcc_inode_attach_init(struct pcc_dataset *dataset,
1749 struct pcc_inode *pcci,
1750 struct dentry *dentry,
1751 enum lu_pcc_type type)
1753 pcci->pcci_path.mnt = mntget(dataset->pccd_path.mnt);
1754 pcci->pcci_path.dentry = dentry;
1755 LASSERT(atomic_read(&pcci->pcci_refcount) == 0);
1756 atomic_set(&pcci->pcci_refcount, 1);
1757 pcci->pcci_type = type;
1758 pcci->pcci_attr_valid = false;
1761 static int __pcc_inode_create(struct pcc_dataset *dataset,
1763 struct dentry **dentry)
1766 struct dentry *base;
1767 struct dentry *child;
1770 OBD_ALLOC(path, MAX_PCC_DATABASE_PATH);
1774 pcc_fid2dataset_path(path, MAX_PCC_DATABASE_PATH, fid);
1776 base = pcc_mkdir_p(dataset->pccd_path.dentry, path, 0);
1782 snprintf(path, MAX_PCC_DATABASE_PATH, DFID_NOBRACE, PFID(fid));
1783 child = pcc_create(base, path, 0);
1784 if (IS_ERR(child)) {
1785 rc = PTR_ERR(child);
1793 OBD_FREE(path, MAX_PCC_DATABASE_PATH);
1797 /* TODO: Set the project ID for PCC copy */
1798 int pcc_inode_store_ugpid(struct dentry *dentry, kuid_t uid, kgid_t gid)
1800 struct inode *inode = dentry->d_inode;
1806 attr.ia_valid = ATTR_UID | ATTR_GID;
1811 rc = notify_change(dentry, &attr, NULL);
1812 inode_unlock(inode);
1817 int pcc_inode_create(struct super_block *sb, struct pcc_dataset *dataset,
1818 struct lu_fid *fid, struct dentry **pcc_dentry)
1820 const struct cred *old_cred;
1823 old_cred = override_creds(pcc_super_cred(sb));
1824 rc = __pcc_inode_create(dataset, fid, pcc_dentry);
1825 revert_creds(old_cred);
1829 int pcc_inode_create_fini(struct pcc_dataset *dataset, struct inode *inode,
1830 struct dentry *pcc_dentry)
1832 const struct cred *old_cred;
1833 struct pcc_inode *pcci;
1838 old_cred = override_creds(pcc_super_cred(inode->i_sb));
1839 pcc_inode_lock(inode);
1840 LASSERT(ll_i2pcci(inode) == NULL);
1841 OBD_SLAB_ALLOC_PTR_GFP(pcci, pcc_inode_slab, GFP_NOFS);
1843 GOTO(out_unlock, rc = -ENOMEM);
1845 rc = pcc_inode_store_ugpid(pcc_dentry, old_cred->suid,
1848 GOTO(out_unlock, rc);
1850 pcc_inode_init(pcci, ll_i2info(inode));
1851 pcc_inode_attach_init(dataset, pcci, pcc_dentry, LU_PCC_READWRITE);
1852 /* Set the layout generation of newly created file with 0 */
1853 pcc_layout_gen_set(pcci, 0);
1859 rc2 = ll_vfs_unlink(pcc_dentry->d_parent->d_inode, pcc_dentry);
1861 CWARN("%s: failed to unlink PCC file %.*s, rc = %d\n",
1862 ll_i2sbi(inode)->ll_fsname,
1863 pcc_dentry->d_name.len, pcc_dentry->d_name.name,
1869 pcc_inode_unlock(inode);
1870 revert_creds(old_cred);
1872 OBD_SLAB_FREE_PTR(pcci, pcc_inode_slab);
1877 static int pcc_filp_write(struct file *filp, const void *buf, ssize_t count,
1883 size = vfs_write(filp, (const void __user *)buf, count, offset);
1892 static int pcc_copy_data(struct file *src, struct file *dst)
1897 loff_t pos, offset = 0;
1898 size_t buf_len = 1048576;
1903 OBD_ALLOC_LARGE(buf, buf_len);
1911 rc2 = vfs_read(src, (void __user *)buf, buf_len, &pos);
1913 GOTO(out_fs, rc = rc2);
1918 rc = pcc_filp_write(dst, buf, rc2, &pos);
1926 OBD_FREE_LARGE(buf, buf_len);
1930 static int pcc_attach_allowed_check(struct inode *inode)
1932 struct ll_inode_info *lli = ll_i2info(inode);
1933 struct pcc_inode *pcci;
1938 pcc_inode_lock(inode);
1939 if (lli->lli_pcc_state & PCC_STATE_FL_ATTACHING)
1940 GOTO(out_unlock, rc = -EBUSY);
1942 pcci = ll_i2pcci(inode);
1943 if (pcci && pcc_inode_has_layout(pcci))
1944 GOTO(out_unlock, rc = -EEXIST);
1946 lli->lli_pcc_state |= PCC_STATE_FL_ATTACHING;
1948 pcc_inode_unlock(inode);
1952 int pcc_readwrite_attach(struct file *file, struct inode *inode,
1955 struct pcc_dataset *dataset;
1956 struct ll_inode_info *lli = ll_i2info(inode);
1957 struct pcc_inode *pcci;
1958 const struct cred *old_cred;
1959 struct dentry *dentry;
1960 struct file *pcc_filp;
1966 rc = pcc_attach_allowed_check(inode);
1970 dataset = pcc_dataset_get(&ll_i2sbi(inode)->ll_pcc_super,
1971 LU_PCC_READWRITE, archive_id);
1972 if (dataset == NULL)
1975 old_cred = override_creds(pcc_super_cred(inode->i_sb));
1976 rc = __pcc_inode_create(dataset, &lli->lli_fid, &dentry);
1978 revert_creds(old_cred);
1979 GOTO(out_dataset_put, rc);
1982 path.mnt = dataset->pccd_path.mnt;
1983 path.dentry = dentry;
1984 #ifdef HAVE_DENTRY_OPEN_USE_PATH
1985 pcc_filp = dentry_open(&path, O_TRUNC | O_WRONLY | O_LARGEFILE,
1988 pcc_filp = dentry_open(path.dentry, path.mnt,
1989 O_TRUNC | O_WRONLY | O_LARGEFILE,
1992 if (IS_ERR_OR_NULL(pcc_filp)) {
1993 rc = pcc_filp == NULL ? -EINVAL : PTR_ERR(pcc_filp);
1994 revert_creds(old_cred);
1995 GOTO(out_dentry, rc);
1998 rc = pcc_inode_store_ugpid(dentry, old_cred->uid, old_cred->gid);
1999 revert_creds(old_cred);
2003 rc = pcc_copy_data(file, pcc_filp);
2007 /* Pause to allow for a race with concurrent HSM remove */
2008 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_PCC_ATTACH_PAUSE, cfs_fail_val);
2010 pcc_inode_lock(inode);
2011 pcci = ll_i2pcci(inode);
2013 OBD_SLAB_ALLOC_PTR_GFP(pcci, pcc_inode_slab, GFP_NOFS);
2015 GOTO(out_unlock, rc = -ENOMEM);
2017 pcc_inode_init(pcci, lli);
2018 pcc_inode_attach_init(dataset, pcci, dentry, LU_PCC_READWRITE);
2020 pcc_inode_unlock(inode);
2027 old_cred = override_creds(pcc_super_cred(inode->i_sb));
2028 rc2 = ll_vfs_unlink(dentry->d_parent->d_inode, dentry);
2029 revert_creds(old_cred);
2031 CWARN("%s: failed to unlink PCC file %.*s, rc = %d\n",
2032 ll_i2sbi(inode)->ll_fsname, dentry->d_name.len,
2033 dentry->d_name.name, rc2);
2038 pcc_dataset_put(dataset);
2042 int pcc_readwrite_attach_fini(struct file *file, struct inode *inode,
2043 __u32 gen, bool lease_broken, int rc,
2046 struct ll_inode_info *lli = ll_i2info(inode);
2047 const struct cred *old_cred;
2048 struct pcc_inode *pcci;
2053 pcc_inode_lock(inode);
2054 pcci = ll_i2pcci(inode);
2055 lli->lli_pcc_state &= ~PCC_STATE_FL_ATTACHING;
2056 if (rc || lease_broken) {
2057 if (attached && pcci)
2058 pcc_inode_put(pcci);
2060 GOTO(out_unlock, rc);
2063 /* PCC inode may be released due to layout lock revocatioin */
2065 GOTO(out_unlock, rc = -ESTALE);
2068 rc = ll_layout_refresh(inode, &gen2);
2071 pcc_layout_gen_set(pcci, gen);
2074 DFID" layout changed from %d to %d.\n",
2075 PFID(ll_inode2fid(inode)), gen, gen2);
2076 GOTO(out_put, rc = -ESTALE);
2082 old_cred = override_creds(pcc_super_cred(inode->i_sb));
2083 pcc_inode_remove(pcci);
2084 revert_creds(old_cred);
2085 pcc_inode_put(pcci);
2088 pcc_inode_unlock(inode);
2092 int pcc_ioctl_detach(struct inode *inode)
2094 struct ll_inode_info *lli = ll_i2info(inode);
2095 struct pcc_inode *pcci;
2100 pcc_inode_lock(inode);
2101 pcci = lli->lli_pcc_inode;
2102 if (!pcci || lli->lli_pcc_state & PCC_STATE_FL_ATTACHING ||
2103 !pcc_inode_has_layout(pcci))
2104 GOTO(out_unlock, rc = 0);
2106 __pcc_layout_invalidate(pcci);
2107 pcc_inode_put(pcci);
2110 pcc_inode_unlock(inode);
2114 int pcc_ioctl_state(struct file *file, struct inode *inode,
2115 struct lu_pcc_state *state)
2121 int buf_len = sizeof(state->pccs_path);
2122 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
2123 struct pcc_file *pccf = &fd->fd_pcc_file;
2124 struct pcc_inode *pcci;
2131 OBD_ALLOC(buf, buf_len);
2135 pcc_inode_lock(inode);
2136 pcci = ll_i2pcci(inode);
2138 state->pccs_type = LU_PCC_NONE;
2139 GOTO(out_unlock, rc = 0);
2142 count = atomic_read(&pcci->pcci_refcount);
2144 state->pccs_type = LU_PCC_NONE;
2145 state->pccs_open_count = 0;
2146 GOTO(out_unlock, rc = 0);
2149 if (pcc_inode_has_layout(pcci))
2151 if (pccf->pccf_file != NULL)
2153 state->pccs_type = pcci->pcci_type;
2154 state->pccs_open_count = count;
2155 state->pccs_flags = ll_i2info(inode)->lli_pcc_state;
2156 #ifdef HAVE_DENTRY_PATH_RAW
2157 path = dentry_path_raw(pcci->pcci_path.dentry, buf, buf_len);
2159 GOTO(out_unlock, rc = PTR_ERR(path));
2164 if (strlcpy(state->pccs_path, path, buf_len) >= buf_len)
2165 GOTO(out_unlock, rc = -ENAMETOOLONG);
2168 pcc_inode_unlock(inode);
2169 OBD_FREE(buf, buf_len);