30 #include <linux/nfs_fs.h>
32 #include <linux/module.h>
37 #define NFSDBG_FACILITY NFSDBG_PNFS
38 #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
53 static struct pnfs_layoutdriver_type *
54 find_pnfs_driver_locked(
u32 id)
56 struct pnfs_layoutdriver_type *local;
63 dprintk("%
s: Searching
for id %
u, found %
p\
n", __func__,
id, local);
67 static
struct pnfs_layoutdriver_type *
68 find_pnfs_driver(
u32 id)
70 struct pnfs_layoutdriver_type *local;
72 spin_lock(&pnfs_spinlock);
73 local = find_pnfs_driver_locked(
id);
74 if (local != NULL && !try_module_get(local->owner)) {
75 dprintk(
"%s: Could not grab reference on module\n", __func__);
78 spin_unlock(&pnfs_spinlock);
85 if (nfss->pnfs_curr_ld) {
86 if (nfss->pnfs_curr_ld->clear_layoutdriver)
87 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
91 module_put(nfss->pnfs_curr_ld->owner);
93 nfss->pnfs_curr_ld =
NULL;
106 struct pnfs_layoutdriver_type *ld_type =
NULL;
113 __func__,
id, server->
nfs_client->cl_exchange_flags);
116 ld_type = find_pnfs_driver(
id);
118 request_module(
"%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX,
id);
119 ld_type = find_pnfs_driver(
id);
121 dprintk(
"%s: No pNFS module found for %u.\n",
126 server->pnfs_curr_ld = ld_type;
127 if (ld_type->set_layoutdriver
128 && ld_type->set_layoutdriver(server, mntfh)) {
130 "driver %u.\n", __func__,
id);
131 module_put(ld_type->owner);
137 dprintk(
"%s: pNFS module for %u set\n", __func__,
id);
141 dprintk(
"%s: Using NFSv4 I/O\n", __func__);
142 server->pnfs_curr_ld =
NULL;
149 struct pnfs_layoutdriver_type *
tmp;
151 if (ld_type->id == 0) {
155 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
157 "alloc_lseg and free_lseg.\n", __func__);
161 spin_lock(&pnfs_spinlock);
162 tmp = find_pnfs_driver_locked(ld_type->id);
164 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
166 dprintk(
"%s Registering id:%u name:%s\n", __func__, ld_type->id,
170 __func__, ld_type->id);
172 spin_unlock(&pnfs_spinlock);
181 dprintk(
"%s Deregistering id:%u\n", __func__, ld_type->id);
182 spin_lock(&pnfs_spinlock);
184 spin_unlock(&pnfs_spinlock);
199 static struct pnfs_layout_hdr *
202 struct pnfs_layoutdriver_type *
ld = NFS_SERVER(ino)->pnfs_curr_ld;
203 return ld->alloc_layout_hdr(ino, gfp_flags);
207 pnfs_free_layout_hdr(
struct pnfs_layout_hdr *lo)
209 struct nfs_server *server = NFS_SERVER(lo->plh_inode);
210 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
212 if (!list_empty(&lo->plh_layouts)) {
215 spin_lock(&clp->cl_lock);
216 list_del_init(&lo->plh_layouts);
217 spin_unlock(&clp->cl_lock);
220 return ld->free_layout_hdr(lo);
224 pnfs_detach_layout_hdr(
struct pnfs_layout_hdr *lo)
226 struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
227 dprintk(
"%s: freeing layout cache %p\n", __func__, lo);
240 pnfs_detach_layout_hdr(lo);
241 spin_unlock(&inode->
i_lock);
242 pnfs_free_layout_hdr(lo);
247 pnfs_iomode_to_fail_bit(
u32 iomode)
250 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
254 pnfs_layout_set_fail_bit(
struct pnfs_layout_hdr *lo,
int fail_bit)
256 lo->plh_retry_timestamp =
jiffies;
262 pnfs_layout_clear_fail_bit(
struct pnfs_layout_hdr *lo,
int fail_bit)
269 pnfs_layout_io_set_failed(
struct pnfs_layout_hdr *lo,
u32 iomode)
279 spin_lock(&inode->
i_lock);
280 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
282 spin_unlock(&inode->
i_lock);
284 dprintk(
"%s Setting layout IOMODE_%s fail bit\n", __func__,
289 pnfs_layout_io_test_failed(
struct pnfs_layout_hdr *lo,
u32 iomode)
292 int fail_bit = pnfs_iomode_to_fail_bit(iomode);
294 if (
test_bit(fail_bit, &lo->plh_flags) == 0)
300 pnfs_layout_clear_fail_bit(lo, fail_bit);
319 struct inode *ino = lseg->
pls_layout->plh_inode;
321 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
325 pnfs_layout_remove_lseg(
struct pnfs_layout_hdr *lo,
328 struct inode *inode = lo->plh_inode;
334 if (list_empty(&lo->plh_segs))
335 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
342 struct pnfs_layout_hdr *lo;
348 dprintk(
"%s: lseg %p ref %d valid %d\n", __func__, lseg,
352 inode = lo->plh_inode;
355 pnfs_layout_remove_lseg(lo, lseg);
356 spin_unlock(&inode->
i_lock);
357 pnfs_free_lseg(lseg);
374 last_byte_offset(
u64 start,
u64 len)
399 return (start1 <= start2) && (end1 >= end2);
428 lo_seg_intersecting(lseg_range, recall_range);
442 dprintk(
"%s: lseg %p ref %d\n", __func__, lseg,
445 pnfs_layout_remove_lseg(lseg->
pls_layout, lseg);
446 list_add(&lseg->
pls_list, tmp_list);
464 dprintk(
"%s:Begin lo %p\n", __func__, lo);
466 if (list_empty(&lo->plh_segs))
470 should_free_lseg(&lseg->
pls_range, recall_range)) {
471 dprintk(
"%s: freeing lseg %p iomode %d "
472 "offset %llu length %llu\n", __func__,
476 removed += mark_lseg_invalid(lseg, tmp_list);
478 dprintk(
"%s:Return %i\n", __func__, invalid - removed);
479 return invalid - removed;
488 if (list_empty(free_me))
493 pnfs_free_lseg(lseg);
500 struct pnfs_layout_hdr *lo;
506 lo->plh_block_lgets++;
509 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
510 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
527 struct pnfs_layout_hdr *lo;
533 spin_lock(&clp->cl_lock);
535 list_for_each_entry_rcu(server, &clp->
cl_superblocks, client_link) {
536 if (!list_empty(&server->
layouts))
537 list_splice_init(&server->
layouts, &tmp_list);
540 spin_unlock(&clp->cl_lock);
542 while (!list_empty(&tmp_list)) {
545 dprintk(
"%s freeing layout for inode %lu\n", __func__,
546 lo->plh_inode->i_ino);
547 list_del_init(&lo->plh_layouts);
556 static bool pnfs_seqid_is_newer(
u32 s1,
u32 s2)
558 return (
s32)s1 - (
s32)s2 > 0;
566 u32 oldseq, newseq, new_barrier;
567 int empty = list_empty(&lo->plh_segs);
571 if (empty || pnfs_seqid_is_newer(newseq, oldseq)) {
572 nfs4_stateid_copy(&lo->plh_stateid,
new);
573 if (update_barrier) {
579 new_barrier = newseq -
atomic_read(&lo->plh_outstanding);
581 if (empty || pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
582 lo->plh_barrier = new_barrier;
587 pnfs_layout_stateid_blocked(
const struct pnfs_layout_hdr *lo,
592 return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
597 pnfs_layoutgets_blocked(
const struct pnfs_layout_hdr *lo,
int lget)
599 return lo->plh_block_lgets ||
600 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
601 (list_empty(&lo->plh_segs) &&
607 struct nfs4_state *open_state)
612 spin_lock(&lo->plh_inode->i_lock);
613 if (pnfs_layoutgets_blocked(lo, 1)) {
615 }
else if (list_empty(&lo->plh_segs)) {
619 seq = read_seqbegin(&open_state->seqlock);
620 nfs4_stateid_copy(dst, &open_state->stateid);
621 }
while (read_seqretry(&open_state->seqlock, seq));
623 nfs4_stateid_copy(dst, &lo->plh_stateid);
624 spin_unlock(&lo->plh_inode->i_lock);
636 send_layoutget(
struct pnfs_layout_hdr *lo,
641 struct inode *ino = lo->plh_inode;
649 lgp = kzalloc(
sizeof(*lgp), gfp_flags);
658 lgp->
args.type = server->pnfs_curr_ld->id;
666 lseg = nfs4_proc_layoutget(lgp, gfp_flags);
668 switch (PTR_ERR(lseg)) {
674 pnfs_layout_io_set_failed(lo, range->
iomode);
693 struct pnfs_layout_hdr *lo =
NULL;
700 dprintk(
"NFS: %s for inode %lu\n", __func__, ino->
i_ino);
705 spin_unlock(&ino->
i_lock);
706 dprintk(
"NFS: %s no layout to return\n", __func__);
709 stateid = nfsi->layout->plh_stateid;
712 empty = list_empty(&lo->plh_segs);
716 spin_unlock(&ino->
i_lock);
718 dprintk(
"NFS: %s no layout segments to return\n", __func__);
721 lo->plh_block_lgets++;
722 spin_unlock(&ino->
i_lock);
731 lo->plh_block_lgets--;
732 spin_unlock(&ino->
i_lock);
737 lrp->
args.stateid = stateid;
738 lrp->
args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
740 lrp->
args.layout = lo;
741 lrp->
clp = NFS_SERVER(ino)->nfs_client;
743 status = nfs4_proc_layoutreturn(lrp);
745 dprintk(
"<-- %s status: %d\n", __func__, status);
752 struct pnfs_layout_hdr *lo;
758 lo = NFS_I(ino)->layout;
760 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
764 mark_lseg_invalid(lseg, &tmp_list);
769 lo->plh_block_lgets++;
771 spin_unlock(&ino->
i_lock);
776 spin_unlock(&ino->
i_lock);
782 struct pnfs_layout_hdr *lo;
785 lo = NFS_I(ino)->layout;
786 lo->plh_block_lgets--;
788 pnfs_detach_layout_hdr(lo);
789 spin_unlock(&ino->
i_lock);
790 pnfs_free_layout_hdr(lo);
792 spin_unlock(&ino->
i_lock);
797 struct pnfs_layout_hdr *lo;
800 lo = NFS_I(ino)->layout;
801 if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
803 spin_unlock(&ino->
i_lock);
809 struct pnfs_layout_hdr *lo;
817 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
822 current_seqid =
be32_to_cpu(lo->plh_stateid.seqid);
827 *barrier = current_seqid +
atomic_read(&lo->plh_outstanding);
829 spin_unlock(&ino->
i_lock);
859 pnfs_layout_insert_lseg(
struct pnfs_layout_hdr *lo,
864 dprintk(
"%s:Begin\n", __func__);
870 dprintk(
"%s: inserted lseg %p "
871 "iomode %d offset %llu length %llu before "
872 "lp %p iomode %d offset %llu length %llu\n",
880 dprintk(
"%s: inserted lseg %p "
881 "iomode %d offset %llu length %llu at tail\n",
887 dprintk(
"%s:Return\n", __func__);
890 static struct pnfs_layout_hdr *
891 alloc_init_layout_hdr(
struct inode *ino,
895 struct pnfs_layout_hdr *lo;
897 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
901 INIT_LIST_HEAD(&lo->plh_layouts);
902 INIT_LIST_HEAD(&lo->plh_segs);
903 INIT_LIST_HEAD(&lo->plh_bulk_recall);
905 lo->plh_lc_cred = get_rpccred(ctx->
state->owner->so_cred);
909 static struct pnfs_layout_hdr *
910 pnfs_find_alloc_layout(
struct inode *ino,
915 struct pnfs_layout_hdr *
new =
NULL;
917 dprintk(
"%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
919 if (nfsi->layout != NULL)
921 spin_unlock(&ino->
i_lock);
922 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
925 if (
likely(nfsi->layout == NULL)) {
928 }
else if (
new != NULL)
929 pnfs_free_layout_hdr(
new);
954 !lo_seg_intersecting(ls_range, range))
960 return lo_seg_contained(ls_range, &range1);
967 pnfs_find_lseg(
struct pnfs_layout_hdr *lo,
972 dprintk(
"%s:Begin\n", __func__);
976 is_matching_lseg(&lseg->
pls_range, range)) {
977 ret = pnfs_get_lseg(lseg);
984 dprintk(
"%s:Return lseg %p ref %d\n",
1009 struct inode *ino,
int iomode)
1013 loff_t fsize = i_size_read(ino);
1014 bool size =
false, size_set =
false,
io =
false, io_set =
false, ret =
false;
1019 dprintk(
"%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1025 dprintk(
"%s fsize %llu\n", __func__, fsize);
1027 if (fsize < t->rd_sz)
1031 dprintk(
"%s nfsi->read_io %llu\n", __func__,
1040 dprintk(
"%s fsize %llu\n", __func__, fsize);
1042 if (fsize < t->wr_sz)
1046 dprintk(
"%s nfsi->write_io %llu\n", __func__,
1054 if (size_set && io_set) {
1057 }
else if (size ||
io)
1060 dprintk(
"<-- %s size %d io %d ret %d\n", __func__, size,
io, ret);
1084 struct pnfs_layout_hdr *lo;
1088 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
1091 if (pnfs_within_mdsthreshold(ctx, ino, iomode))
1095 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
1097 spin_unlock(&ino->
i_lock);
1102 if (
test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1103 dprintk(
"%s matches recall, use MDS\n", __func__);
1108 if (pnfs_layout_io_test_failed(lo, iomode))
1112 lseg = pnfs_find_lseg(lo, &arg);
1116 if (pnfs_layoutgets_blocked(lo, 0))
1120 if (list_empty(&lo->plh_segs))
1123 spin_unlock(&ino->
i_lock);
1128 spin_lock(&clp->cl_lock);
1129 BUG_ON(!list_empty(&lo->plh_layouts));
1131 spin_unlock(&clp->cl_lock);
1142 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
1147 dprintk(
"%s: inode %s/%llu pNFS layout segment %s for "
1148 "(%s, offset: %llu, length: %llu)\n",
1149 __func__, ino->
i_sb->s_id,
1150 (
unsigned long long)NFS_FILEID(ino),
1151 lseg == NULL ?
"not found" :
"found",
1152 iomode==
IOMODE_RW ?
"read/write" :
"read-only",
1153 (
unsigned long long)pos,
1154 (
unsigned long long)count);
1157 spin_unlock(&ino->
i_lock);
1158 goto out_put_layout_hdr;
1165 struct pnfs_layout_hdr *lo = NFS_I(lgp->
args.inode)->layout;
1168 struct inode *ino = lo->plh_inode;
1172 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->
gfp_flags);
1173 if (!lseg || IS_ERR(lseg)) {
1177 status = PTR_ERR(lseg);
1178 dprintk(
"%s: Could not allocate layout: error %d\n",
1184 if (
test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1185 dprintk(
"%s forget reply due to recall\n", __func__);
1186 goto out_forget_reply;
1189 if (pnfs_layoutgets_blocked(lo, 1) ||
1190 pnfs_layout_stateid_blocked(lo, &res->
stateid)) {
1191 dprintk(
"%s forget reply due to state\n", __func__);
1192 goto out_forget_reply;
1198 init_lseg(lo, lseg);
1200 pnfs_get_lseg(lseg);
1201 pnfs_layout_insert_lseg(lo, lseg);
1205 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1208 spin_unlock(&ino->
i_lock);
1211 return ERR_PTR(status);
1214 spin_unlock(&ino->
i_lock);
1216 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1233 rd_size = i_size_read(pgio->
pg_inode) - req_offset(req);
1277 struct nfs_server *server = NFS_SERVER(inode);
1278 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1291 struct nfs_server *server = NFS_SERVER(inode);
1292 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1322 pgio->
pg_lseg->pls_range.length);
1335 while (!list_empty(head)) {
1338 nfs_list_remove_request(req);
1340 nfs_list_add_request(req, &failed);
1344 if (!list_empty(&failed)) {
1349 list_move(&failed, head);
1361 if (NFS_SERVER(hdr->
inode)->pnfs_curr_ld->flags &
1362 PNFS_LAYOUTRET_ON_ERROR) {
1364 pnfs_return_layout(hdr->
inode);
1383 pnfs_ld_handle_write_error(data);
1384 hdr->
mds_ops->rpc_release(data);
1409 struct inode *inode = hdr->
inode;
1415 dprintk(
"%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1417 trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
1420 dprintk(
"%s End (trypnfs:%d)\n", __func__, trypnfs);
1432 while (!list_empty(head)) {
1436 list_del_init(&data->
list);
1438 trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how);
1440 pnfs_write_through_mds(desc, data);
1491 while (!list_empty(head)) {
1494 nfs_list_remove_request(req);
1496 nfs_list_add_request(req, &failed);
1500 if (!list_empty(&failed)) {
1501 list_move(&failed, head);
1508 static void pnfs_ld_handle_read_error(
struct nfs_read_data *data)
1513 if (NFS_SERVER(hdr->
inode)->pnfs_curr_ld->flags &
1514 PNFS_LAYOUTRET_ON_ERROR) {
1516 pnfs_return_layout(hdr->
inode);
1535 pnfs_ld_handle_read_error(data);
1536 hdr->
mds_ops->rpc_release(data);
1563 struct inode *inode = hdr->
inode;
1569 dprintk(
"%s: Reading ino:%lu %u@%llu\n",
1570 __func__, inode->
i_ino, rdata->
args.count, rdata->
args.offset);
1572 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
1575 dprintk(
"%s End (trypnfs:%d)\n", __func__, trypnfs);
1587 while (!list_empty(head)) {
1591 list_del_init(&data->
list);
1593 trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
1595 pnfs_read_through_mds(desc, data);
1631 pnfs_do_multiple_reads(desc, &hdr->
rpc_list);
1641 static void pnfs_list_write_lseg(
struct inode *inode,
struct list_head *listp)
1662 struct inode *inode = hdr->
inode;
1665 bool mark_as_dirty =
false;
1667 spin_lock(&inode->
i_lock);
1669 mark_as_dirty =
true;
1670 dprintk(
"%s: Set layoutcommit for inode %lu ",
1671 __func__, inode->
i_ino);
1675 pnfs_get_lseg(hdr->
lseg);
1677 if (end_pos > nfsi->layout->plh_lwb)
1678 nfsi->layout->plh_lwb = end_pos;
1679 spin_unlock(&inode->
i_lock);
1680 dprintk(
"%s: lseg %p end_pos %llu\n",
1681 __func__, hdr->
lseg, nfsi->layout->plh_lwb);
1686 mark_inode_dirty_sync(inode);
1694 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
1695 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
1720 data = kzalloc(
sizeof(*data),
GFP_NOFS);
1741 spin_lock(&inode->
i_lock);
1744 spin_unlock(&inode->
i_lock);
1749 pnfs_list_write_lseg(inode, &data->
lseg_list);
1751 end_pos = nfsi->layout->plh_lwb;
1752 nfsi->layout->plh_lwb = 0;
1754 nfs4_stateid_copy(&data->
args.stateid, &nfsi->layout->plh_stateid);
1755 spin_unlock(&inode->
i_lock);
1758 data->
cred = get_rpccred(nfsi->layout->plh_lc_cred);
1760 data->
args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1762 data->
args.lastbytewritten = end_pos - 1;
1763 data->
res.server = NFS_SERVER(inode);
1765 status = nfs4_proc_layoutcommit(data, sync);
1768 mark_inode_dirty_sync(inode);
1769 dprintk(
"<-- %s status %d\n", __func__, status);
1780 thp = kzalloc(
sizeof(*thp),
GFP_NOFS);
1782 dprintk(
"%s mdsthreshold allocation failed\n", __func__);