blob: 8fa93e23cb24d95e4774240ba60f2d7c91213714 [file] [log] [blame]
Fred Isaman9e692962011-07-30 20:52:41 -04001/*
2 * linux/fs/nfs/blocklayout/blocklayout.h
3 *
4 * Module for the NFSv4.1 pNFS block layout driver.
5 *
6 * Copyright (c) 2006 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Andy Adamson <andros@citi.umich.edu>
10 * Fred Isaman <iisaman@umich.edu>
11 *
12 * permission is granted to use, copy, create derivative works and
13 * redistribute this software and such derivative works for any purpose,
14 * so long as the name of the university of michigan is not used in
15 * any advertising or publicity pertaining to the use or distribution
16 * of this software without specific, written prior authorization. if
17 * the above copyright notice or any other identification of the
18 * university of michigan is included in any copy of any portion of
19 * this software, then the disclaimer below must also be included.
20 *
21 * this software is provided as is, without representation from the
22 * university of michigan as to its fitness for any purpose, and without
23 * warranty by the university of michigan of any kind, either express
24 * or implied, including without limitation the implied warranties of
25 * merchantability and fitness for a particular purpose. the regents
26 * of the university of michigan shall not be liable for any damages,
27 * including special, indirect, incidental, or consequential damages,
28 * with respect to any claim arising out or in connection with the use
29 * of the software, even if it has been or is hereafter advised of the
30 * possibility of such damages.
31 */
32
33#include "blocklayout.h"
34#define NFSDBG_FACILITY NFSDBG_PNFS_LD
35
36static void print_bl_extent(struct pnfs_block_extent *be)
37{
38 dprintk("PRINT EXTENT extent %p\n", be);
39 if (be) {
40 dprintk(" be_f_offset %llu\n", (u64)be->be_f_offset);
41 dprintk(" be_length %llu\n", (u64)be->be_length);
42 dprintk(" be_v_offset %llu\n", (u64)be->be_v_offset);
43 dprintk(" be_state %d\n", be->be_state);
44 }
45}
46
47static void
48destroy_extent(struct kref *kref)
49{
50 struct pnfs_block_extent *be;
51
52 be = container_of(kref, struct pnfs_block_extent, be_refcnt);
53 dprintk("%s be=%p\n", __func__, be);
54 kfree(be);
55}
56
57void
58bl_put_extent(struct pnfs_block_extent *be)
59{
60 if (be) {
61 dprintk("%s enter %p (%i)\n", __func__, be,
62 atomic_read(&be->be_refcnt.refcount));
63 kref_put(&be->be_refcnt, destroy_extent);
64 }
65}
66
67struct pnfs_block_extent *bl_alloc_extent(void)
68{
69 struct pnfs_block_extent *be;
70
71 be = kmalloc(sizeof(struct pnfs_block_extent), GFP_NOFS);
72 if (!be)
73 return NULL;
74 INIT_LIST_HEAD(&be->be_node);
75 kref_init(&be->be_refcnt);
76 be->be_inval = NULL;
77 return be;
78}
79
80static void print_elist(struct list_head *list)
81{
82 struct pnfs_block_extent *be;
83 dprintk("****************\n");
84 dprintk("Extent list looks like:\n");
85 list_for_each_entry(be, list, be_node) {
86 print_bl_extent(be);
87 }
88 dprintk("****************\n");
89}
Fred Isaman03341d22011-07-30 20:52:45 -040090
91static inline int
92extents_consistent(struct pnfs_block_extent *old, struct pnfs_block_extent *new)
93{
94 /* Note this assumes new->be_f_offset >= old->be_f_offset */
95 return (new->be_state == old->be_state) &&
96 ((new->be_state == PNFS_BLOCK_NONE_DATA) ||
97 ((new->be_v_offset - old->be_v_offset ==
98 new->be_f_offset - old->be_f_offset) &&
99 new->be_mdev == old->be_mdev));
100}
101
102/* Adds new to appropriate list in bl, modifying new and removing existing
103 * extents as appropriate to deal with overlaps.
104 *
105 * See bl_find_get_extent for list constraints.
106 *
107 * Refcount on new is already set. If end up not using it, or error out,
108 * need to put the reference.
109 *
110 * bl->bl_ext_lock is held by caller.
111 */
112int
113bl_add_merge_extent(struct pnfs_block_layout *bl,
114 struct pnfs_block_extent *new)
115{
116 struct pnfs_block_extent *be, *tmp;
117 sector_t end = new->be_f_offset + new->be_length;
118 struct list_head *list;
119
120 dprintk("%s enter with be=%p\n", __func__, new);
121 print_bl_extent(new);
122 list = &bl->bl_extents[bl_choose_list(new->be_state)];
123 print_elist(list);
124
125 /* Scan for proper place to insert, extending new to the left
126 * as much as possible.
127 */
128 list_for_each_entry_safe(be, tmp, list, be_node) {
129 if (new->be_f_offset < be->be_f_offset)
130 break;
131 if (end <= be->be_f_offset + be->be_length) {
132 /* new is a subset of existing be*/
133 if (extents_consistent(be, new)) {
134 dprintk("%s: new is subset, ignoring\n",
135 __func__);
136 bl_put_extent(new);
137 return 0;
138 } else
139 goto out_err;
140 } else if (new->be_f_offset <=
141 be->be_f_offset + be->be_length) {
142 /* new overlaps or abuts existing be */
143 if (extents_consistent(be, new)) {
144 /* extend new to fully replace be */
145 new->be_length += new->be_f_offset -
146 be->be_f_offset;
147 new->be_f_offset = be->be_f_offset;
148 new->be_v_offset = be->be_v_offset;
149 dprintk("%s: removing %p\n", __func__, be);
150 list_del(&be->be_node);
151 bl_put_extent(be);
152 } else if (new->be_f_offset !=
153 be->be_f_offset + be->be_length)
154 goto out_err;
155 }
156 }
157 /* Note that if we never hit the above break, be will not point to a
158 * valid extent. However, in that case &be->be_node==list.
159 */
160 list_add_tail(&new->be_node, &be->be_node);
161 dprintk("%s: inserting new\n", __func__);
162 print_elist(list);
163 /* Scan forward for overlaps. If we find any, extend new and
164 * remove the overlapped extent.
165 */
166 be = list_prepare_entry(new, list, be_node);
167 list_for_each_entry_safe_continue(be, tmp, list, be_node) {
168 if (end < be->be_f_offset)
169 break;
170 /* new overlaps or abuts existing be */
171 if (extents_consistent(be, new)) {
172 if (end < be->be_f_offset + be->be_length) {
173 /* extend new to fully cover be */
174 end = be->be_f_offset + be->be_length;
175 new->be_length = end - new->be_f_offset;
176 }
177 dprintk("%s: removing %p\n", __func__, be);
178 list_del(&be->be_node);
179 bl_put_extent(be);
180 } else if (end != be->be_f_offset) {
181 list_del(&new->be_node);
182 goto out_err;
183 }
184 }
185 dprintk("%s: after merging\n", __func__);
186 print_elist(list);
187 /* FIXME - The per-list consistency checks have all been done,
188 * should now check cross-list consistency.
189 */
190 return 0;
191
192 out_err:
193 bl_put_extent(new);
194 return -EIO;
195}
Fred Isaman6d742ba2011-07-30 20:52:48 -0400196
197/* Returns extent, or NULL. If a second READ extent exists, it is returned
198 * in cow_read, if given.
199 *
200 * The extents are kept in two seperate ordered lists, one for READ and NONE,
201 * one for READWRITE and INVALID. Within each list, we assume:
202 * 1. Extents are ordered by file offset.
203 * 2. For any given isect, there is at most one extents that matches.
204 */
205struct pnfs_block_extent *
206bl_find_get_extent(struct pnfs_block_layout *bl, sector_t isect,
207 struct pnfs_block_extent **cow_read)
208{
209 struct pnfs_block_extent *be, *cow, *ret;
210 int i;
211
212 dprintk("%s enter with isect %llu\n", __func__, (u64)isect);
213 cow = ret = NULL;
214 spin_lock(&bl->bl_ext_lock);
215 for (i = 0; i < EXTENT_LISTS; i++) {
216 list_for_each_entry_reverse(be, &bl->bl_extents[i], be_node) {
217 if (isect >= be->be_f_offset + be->be_length)
218 break;
219 if (isect >= be->be_f_offset) {
220 /* We have found an extent */
221 dprintk("%s Get %p (%i)\n", __func__, be,
222 atomic_read(&be->be_refcnt.refcount));
223 kref_get(&be->be_refcnt);
224 if (!ret)
225 ret = be;
226 else if (be->be_state != PNFS_BLOCK_READ_DATA)
227 bl_put_extent(be);
228 else
229 cow = be;
230 break;
231 }
232 }
233 if (ret &&
234 (!cow_read || ret->be_state != PNFS_BLOCK_INVALID_DATA))
235 break;
236 }
237 spin_unlock(&bl->bl_ext_lock);
238 if (cow_read)
239 *cow_read = cow;
240 print_bl_extent(ret);
241 return ret;
242}