Commit | Line | Data |
---|---|---|
2a82b8be DC |
1 | /* |
2 | * Copyright (c) 2006-2007 Silicon Graphics, Inc. | |
2cd2ef6a | 3 | * Copyright (c) 2014 Christoph Hellwig. |
2a82b8be DC |
4 | * All Rights Reserved. |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it would be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write the Free Software Foundation, | |
17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | #include "xfs.h" | |
a4fbe6ab | 20 | #include "xfs_format.h" |
239880ef DC |
21 | #include "xfs_log_format.h" |
22 | #include "xfs_trans_resv.h" | |
23 | #include "xfs_ag.h" | |
24 | #include "xfs_sb.h" | |
25 | #include "xfs_mount.h" | |
2a82b8be | 26 | #include "xfs_inum.h" |
2a82b8be | 27 | #include "xfs_inode.h" |
2a82b8be | 28 | #include "xfs_bmap.h" |
68988114 | 29 | #include "xfs_bmap_util.h" |
2a82b8be | 30 | #include "xfs_alloc.h" |
2a82b8be | 31 | #include "xfs_mru_cache.h" |
a4fbe6ab | 32 | #include "xfs_dinode.h" |
2a82b8be | 33 | #include "xfs_filestream.h" |
0b1b213f | 34 | #include "xfs_trace.h" |
2a82b8be | 35 | |
2a82b8be DC |
36 | #define TRACE_AG_SCAN(mp, ag, ag2) |
37 | #define TRACE_AG_PICK1(mp, max_ag, maxfree) | |
38 | #define TRACE_AG_PICK2(mp, ag, ag2, cnt, free, scan, flag) | |
2a82b8be DC |
39 | #define TRACE_FREE(mp, ip, pip, ag, cnt) |
40 | #define TRACE_LOOKUP(mp, ip, pip, ag, cnt) | |
2a82b8be DC |
41 | |
42 | static kmem_zone_t *item_zone; | |
43 | ||
2cd2ef6a CH |
44 | struct xfs_fstrm_item { |
45 | struct xfs_mru_cache_elem mru; | |
46 | struct xfs_inode *ip; | |
47 | xfs_agnumber_t ag; /* AG in use for this directory */ | |
48 | }; | |
49 | ||
50 | enum xfs_fstrm_alloc { | |
51 | XFS_PICK_USERDATA = 1, | |
52 | XFS_PICK_LOWSPACE = 2, | |
53 | }; | |
2a82b8be | 54 | |
0664ce8d CH |
55 | /* |
56 | * Allocation group filestream associations are tracked with per-ag atomic | |
2cd2ef6a | 57 | * counters. These counters allow xfs_filestream_pick_ag() to tell whether a |
0664ce8d CH |
58 | * particular AG already has active filestreams associated with it. The mount |
59 | * point's m_peraglock is used to protect these counters from per-ag array | |
60 | * re-allocation during a growfs operation. When xfs_growfs_data_private() is | |
61 | * about to reallocate the array, it calls xfs_filestream_flush() with the | |
62 | * m_peraglock held in write mode. | |
63 | * | |
64 | * Since xfs_mru_cache_flush() guarantees that all the free functions for all | |
65 | * the cache elements have finished executing before it returns, it's safe for | |
66 | * the free functions to use the atomic counters without m_peraglock protection. | |
67 | * This allows the implementation of xfs_fstrm_free_func() to be agnostic about | |
68 | * whether it was called with the m_peraglock held in read mode, write mode or | |
69 | * not held at all. The race condition this addresses is the following: | |
70 | * | |
71 | * - The work queue scheduler fires and pulls a filestream directory cache | |
72 | * element off the LRU end of the cache for deletion, then gets pre-empted. | |
73 | * - A growfs operation grabs the m_peraglock in write mode, flushes all the | |
74 | * remaining items from the cache and reallocates the mount point's per-ag | |
75 | * array, resetting all the counters to zero. | |
76 | * - The work queue thread resumes and calls the free function for the element | |
77 | * it started cleaning up earlier. In the process it decrements the | |
78 | * filestreams counter for an AG that now has no references. | |
79 | * | |
80 | * With a shrinkfs feature, the above scenario could panic the system. | |
81 | * | |
82 | * All other uses of the following macros should be protected by either the | |
83 | * m_peraglock held in read mode, or the cache's internal locking exposed by the | |
84 | * interval between a call to xfs_mru_cache_lookup() and a call to | |
85 | * xfs_mru_cache_done(). In addition, the m_peraglock must be held in read mode | |
86 | * when new elements are added to the cache. | |
87 | * | |
88 | * Combined, these locking rules ensure that no associations will ever exist in | |
89 | * the cache that reference per-ag array elements that have since been | |
90 | * reallocated. | |
91 | */ | |
92 | static int | |
93 | xfs_filestream_peek_ag( | |
94 | xfs_mount_t *mp, | |
95 | xfs_agnumber_t agno) | |
96 | { | |
97 | struct xfs_perag *pag; | |
98 | int ret; | |
99 | ||
100 | pag = xfs_perag_get(mp, agno); | |
101 | ret = atomic_read(&pag->pagf_fstrms); | |
102 | xfs_perag_put(pag); | |
103 | return ret; | |
104 | } | |
105 | ||
106 | static int | |
107 | xfs_filestream_get_ag( | |
108 | xfs_mount_t *mp, | |
109 | xfs_agnumber_t agno) | |
110 | { | |
111 | struct xfs_perag *pag; | |
112 | int ret; | |
113 | ||
114 | pag = xfs_perag_get(mp, agno); | |
115 | ret = atomic_inc_return(&pag->pagf_fstrms); | |
116 | xfs_perag_put(pag); | |
117 | return ret; | |
118 | } | |
119 | ||
120 | static void | |
121 | xfs_filestream_put_ag( | |
122 | xfs_mount_t *mp, | |
123 | xfs_agnumber_t agno) | |
124 | { | |
125 | struct xfs_perag *pag; | |
126 | ||
127 | pag = xfs_perag_get(mp, agno); | |
128 | atomic_dec(&pag->pagf_fstrms); | |
129 | xfs_perag_put(pag); | |
130 | } | |
2a82b8be | 131 | |
2cd2ef6a CH |
132 | static void |
133 | xfs_fstrm_free_func( | |
134 | struct xfs_mru_cache_elem *mru) | |
135 | { | |
136 | struct xfs_fstrm_item *item = | |
137 | container_of(mru, struct xfs_fstrm_item, mru); | |
138 | ||
139 | xfs_filestream_put_ag(item->ip->i_mount, item->ag); | |
140 | ||
141 | TRACE_FREE(mp, ip, NULL, item->ag, | |
142 | xfs_filestream_peek_ag(mp, item->ag)); | |
143 | ||
144 | kmem_zone_free(item_zone, item); | |
145 | } | |
146 | ||
2a82b8be DC |
147 | /* |
148 | * Scan the AGs starting at startag looking for an AG that isn't in use and has | |
149 | * at least minlen blocks free. | |
150 | */ | |
151 | static int | |
2cd2ef6a CH |
152 | xfs_filestream_pick_ag( |
153 | struct xfs_inode *ip, | |
154 | xfs_agnumber_t startag, | |
155 | xfs_agnumber_t *agp, | |
156 | int flags, | |
157 | xfs_extlen_t minlen) | |
2a82b8be | 158 | { |
2cd2ef6a CH |
159 | struct xfs_mount *mp = ip->i_mount; |
160 | struct xfs_fstrm_item *item; | |
161 | struct xfs_perag *pag; | |
162 | xfs_extlen_t longest, free, minfree, maxfree = 0; | |
163 | xfs_agnumber_t ag, max_ag = NULLAGNUMBER; | |
164 | int streams, max_streams; | |
165 | int err, trylock, nscan; | |
166 | ||
167 | ASSERT(S_ISDIR(ip->i_d.di_mode)); | |
2a82b8be DC |
168 | |
169 | /* 2% of an AG's blocks must be free for it to be chosen. */ | |
170 | minfree = mp->m_sb.sb_agblocks / 50; | |
171 | ||
172 | ag = startag; | |
173 | *agp = NULLAGNUMBER; | |
174 | ||
175 | /* For the first pass, don't sleep trying to init the per-AG. */ | |
176 | trylock = XFS_ALLOC_FLAG_TRYLOCK; | |
177 | ||
178 | for (nscan = 0; 1; nscan++) { | |
4196ac08 DC |
179 | pag = xfs_perag_get(mp, ag); |
180 | TRACE_AG_SCAN(mp, ag, atomic_read(&pag->pagf_fstrms)); | |
2a82b8be DC |
181 | |
182 | if (!pag->pagf_init) { | |
183 | err = xfs_alloc_pagf_init(mp, NULL, ag, trylock); | |
4196ac08 DC |
184 | if (err && !trylock) { |
185 | xfs_perag_put(pag); | |
2a82b8be | 186 | return err; |
4196ac08 | 187 | } |
2a82b8be DC |
188 | } |
189 | ||
190 | /* Might fail sometimes during the 1st pass with trylock set. */ | |
191 | if (!pag->pagf_init) | |
192 | goto next_ag; | |
193 | ||
194 | /* Keep track of the AG with the most free blocks. */ | |
195 | if (pag->pagf_freeblks > maxfree) { | |
196 | maxfree = pag->pagf_freeblks; | |
4196ac08 | 197 | max_streams = atomic_read(&pag->pagf_fstrms); |
2a82b8be DC |
198 | max_ag = ag; |
199 | } | |
200 | ||
201 | /* | |
202 | * The AG reference count does two things: it enforces mutual | |
203 | * exclusion when examining the suitability of an AG in this | |
204 | * loop, and it guards against two filestreams being established | |
205 | * in the same AG as each other. | |
206 | */ | |
207 | if (xfs_filestream_get_ag(mp, ag) > 1) { | |
208 | xfs_filestream_put_ag(mp, ag); | |
209 | goto next_ag; | |
210 | } | |
211 | ||
6cc87645 | 212 | longest = xfs_alloc_longest_free_extent(mp, pag); |
2a82b8be DC |
213 | if (((minlen && longest >= minlen) || |
214 | (!minlen && pag->pagf_freeblks >= minfree)) && | |
215 | (!pag->pagf_metadata || !(flags & XFS_PICK_USERDATA) || | |
216 | (flags & XFS_PICK_LOWSPACE))) { | |
217 | ||
218 | /* Break out, retaining the reference on the AG. */ | |
219 | free = pag->pagf_freeblks; | |
4196ac08 DC |
220 | streams = atomic_read(&pag->pagf_fstrms); |
221 | xfs_perag_put(pag); | |
2a82b8be DC |
222 | *agp = ag; |
223 | break; | |
224 | } | |
225 | ||
226 | /* Drop the reference on this AG, it's not usable. */ | |
227 | xfs_filestream_put_ag(mp, ag); | |
228 | next_ag: | |
4196ac08 | 229 | xfs_perag_put(pag); |
2a82b8be DC |
230 | /* Move to the next AG, wrapping to AG 0 if necessary. */ |
231 | if (++ag >= mp->m_sb.sb_agcount) | |
232 | ag = 0; | |
233 | ||
234 | /* If a full pass of the AGs hasn't been done yet, continue. */ | |
235 | if (ag != startag) | |
236 | continue; | |
237 | ||
238 | /* Allow sleeping in xfs_alloc_pagf_init() on the 2nd pass. */ | |
239 | if (trylock != 0) { | |
240 | trylock = 0; | |
241 | continue; | |
242 | } | |
243 | ||
244 | /* Finally, if lowspace wasn't set, set it for the 3rd pass. */ | |
245 | if (!(flags & XFS_PICK_LOWSPACE)) { | |
246 | flags |= XFS_PICK_LOWSPACE; | |
247 | continue; | |
248 | } | |
249 | ||
250 | /* | |
251 | * Take the AG with the most free space, regardless of whether | |
252 | * it's already in use by another filestream. | |
253 | */ | |
254 | if (max_ag != NULLAGNUMBER) { | |
255 | xfs_filestream_get_ag(mp, max_ag); | |
256 | TRACE_AG_PICK1(mp, max_ag, maxfree); | |
4196ac08 | 257 | streams = max_streams; |
2a82b8be DC |
258 | free = maxfree; |
259 | *agp = max_ag; | |
260 | break; | |
261 | } | |
262 | ||
263 | /* take AG 0 if none matched */ | |
264 | TRACE_AG_PICK1(mp, max_ag, maxfree); | |
265 | *agp = 0; | |
266 | return 0; | |
267 | } | |
268 | ||
4196ac08 | 269 | TRACE_AG_PICK2(mp, startag, *agp, streams, free, nscan, flags); |
2a82b8be | 270 | |
2cd2ef6a | 271 | if (*agp == NULLAGNUMBER) |
2a82b8be | 272 | return 0; |
2a82b8be | 273 | |
2cd2ef6a | 274 | err = ENOMEM; |
2a82b8be DC |
275 | item = kmem_zone_zalloc(item_zone, KM_MAYFAIL); |
276 | if (!item) | |
2cd2ef6a | 277 | goto out_put_ag; |
2a82b8be | 278 | |
2cd2ef6a | 279 | item->ag = *agp; |
2a82b8be | 280 | item->ip = ip; |
2a82b8be | 281 | |
22328d71 | 282 | err = xfs_mru_cache_insert(mp->m_filestream, ip->i_ino, &item->mru); |
2a82b8be | 283 | if (err) { |
2cd2ef6a CH |
284 | if (err == EEXIST) |
285 | err = 0; | |
286 | goto out_free_item; | |
2a82b8be DC |
287 | } |
288 | ||
2a82b8be | 289 | return 0; |
2a82b8be | 290 | |
2cd2ef6a | 291 | out_free_item: |
2a82b8be | 292 | kmem_zone_free(item_zone, item); |
2cd2ef6a CH |
293 | out_put_ag: |
294 | xfs_filestream_put_ag(mp, *agp); | |
295 | return err; | |
2a82b8be DC |
296 | } |
297 | ||
2cd2ef6a CH |
298 | static struct xfs_inode * |
299 | xfs_filestream_get_parent( | |
300 | struct xfs_inode *ip) | |
2a82b8be | 301 | { |
2cd2ef6a CH |
302 | struct inode *inode = VFS_I(ip), *dir = NULL; |
303 | struct dentry *dentry, *parent; | |
2a82b8be | 304 | |
2cd2ef6a CH |
305 | dentry = d_find_alias(inode); |
306 | if (!dentry) | |
307 | goto out; | |
2a82b8be | 308 | |
2cd2ef6a CH |
309 | parent = dget_parent(dentry); |
310 | if (!parent) | |
311 | goto out_dput; | |
2a82b8be | 312 | |
2cd2ef6a CH |
313 | dir = igrab(parent->d_inode); |
314 | dput(parent); | |
2a82b8be | 315 | |
2cd2ef6a CH |
316 | out_dput: |
317 | dput(dentry); | |
318 | out: | |
319 | return dir ? XFS_I(dir) : NULL; | |
2a82b8be DC |
320 | } |
321 | ||
2a82b8be DC |
322 | /* |
323 | * Return the AG of the filestream the file or directory belongs to, or | |
324 | * NULLAGNUMBER otherwise. | |
325 | */ | |
326 | xfs_agnumber_t | |
327 | xfs_filestream_lookup_ag( | |
2cd2ef6a | 328 | struct xfs_inode *ip) |
2a82b8be | 329 | { |
2cd2ef6a CH |
330 | struct xfs_mount *mp = ip->i_mount; |
331 | struct xfs_fstrm_item *item; | |
332 | struct xfs_inode *pip = NULL; | |
333 | xfs_agnumber_t ag = NULLAGNUMBER; | |
334 | int ref = 0; | |
22328d71 | 335 | struct xfs_mru_cache_elem *mru; |
2a82b8be | 336 | |
2cd2ef6a | 337 | ASSERT(S_ISREG(ip->i_d.di_mode)); |
2a82b8be | 338 | |
2cd2ef6a CH |
339 | pip = xfs_filestream_get_parent(ip); |
340 | if (!pip) | |
341 | goto out; | |
342 | ||
343 | mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino); | |
344 | if (!mru) | |
345 | goto out; | |
346 | ||
347 | item = container_of(mru, struct xfs_fstrm_item, mru); | |
2a82b8be | 348 | |
2a82b8be | 349 | ag = item->ag; |
22328d71 | 350 | xfs_mru_cache_done(mp->m_filestream); |
2a82b8be | 351 | |
2cd2ef6a CH |
352 | ref = xfs_filestream_peek_ag(ip->i_mount, ag); |
353 | out: | |
354 | TRACE_LOOKUP(mp, ip, pip, ag, ref); | |
355 | IRELE(pip); | |
2a82b8be DC |
356 | return ag; |
357 | } | |
358 | ||
359 | /* | |
2cd2ef6a | 360 | * Make sure a directory has a filestream associated with it. |
2a82b8be | 361 | * |
2cd2ef6a CH |
362 | * This is called when creating regular files in an directory that has |
363 | * filestreams enabled, so that a stream is ready by the time we need it | |
364 | * in the allocator for the files inside the directory. | |
2a82b8be DC |
365 | */ |
366 | int | |
367 | xfs_filestream_associate( | |
2cd2ef6a | 368 | struct xfs_inode *pip) |
2a82b8be | 369 | { |
2cd2ef6a | 370 | struct xfs_mount *mp = pip->i_mount; |
22328d71 | 371 | struct xfs_mru_cache_elem *mru; |
2cd2ef6a | 372 | xfs_agnumber_t startag, ag; |
2a82b8be | 373 | |
03209378 | 374 | ASSERT(S_ISDIR(pip->i_d.di_mode)); |
2a82b8be DC |
375 | |
376 | /* | |
2cd2ef6a | 377 | * If the directory already has a file stream associated we're done. |
2a82b8be | 378 | */ |
22328d71 CH |
379 | mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino); |
380 | if (mru) { | |
22328d71 | 381 | xfs_mru_cache_done(mp->m_filestream); |
2cd2ef6a | 382 | return 0; |
2a82b8be DC |
383 | } |
384 | ||
385 | /* | |
386 | * Set the starting AG using the rotor for inode32, otherwise | |
387 | * use the directory inode's AG. | |
388 | */ | |
389 | if (mp->m_flags & XFS_MOUNT_32BITINODES) { | |
2cd2ef6a | 390 | xfs_agnumber_t rotorstep = xfs_rotorstep; |
2a82b8be DC |
391 | startag = (mp->m_agfrotor / rotorstep) % mp->m_sb.sb_agcount; |
392 | mp->m_agfrotor = (mp->m_agfrotor + 1) % | |
393 | (mp->m_sb.sb_agcount * rotorstep); | |
394 | } else | |
395 | startag = XFS_INO_TO_AGNO(mp, pip->i_ino); | |
396 | ||
2cd2ef6a | 397 | return xfs_filestream_pick_ag(pip, startag, &ag, 0, 0); |
2a82b8be DC |
398 | } |
399 | ||
400 | /* | |
2cd2ef6a CH |
401 | * Pick a new allocation group for the current file and its file stream. |
402 | * | |
403 | * This is called when the allocator can't find a suitable extent in the | |
404 | * current AG, and we have to move the stream into a new AG with more space. | |
2a82b8be DC |
405 | */ |
406 | int | |
407 | xfs_filestream_new_ag( | |
68988114 DC |
408 | struct xfs_bmalloca *ap, |
409 | xfs_agnumber_t *agp) | |
2a82b8be | 410 | { |
2cd2ef6a CH |
411 | struct xfs_inode *ip = ap->ip, *pip; |
412 | struct xfs_mount *mp = ip->i_mount; | |
413 | xfs_extlen_t minlen = ap->length; | |
414 | xfs_agnumber_t startag = 0; | |
415 | int flags, err = 0; | |
416 | struct xfs_mru_cache_elem *mru; | |
2a82b8be | 417 | |
2cd2ef6a | 418 | *agp = NULLAGNUMBER; |
2a82b8be | 419 | |
2cd2ef6a CH |
420 | pip = xfs_filestream_get_parent(ip); |
421 | if (!pip) | |
422 | goto exit; | |
2a82b8be | 423 | |
2cd2ef6a CH |
424 | mru = xfs_mru_cache_remove(mp->m_filestream, pip->i_ino); |
425 | if (mru) { | |
426 | struct xfs_fstrm_item *item = | |
427 | container_of(mru, struct xfs_fstrm_item, mru); | |
428 | startag = (item->ag + 1) % mp->m_sb.sb_agcount; | |
2a82b8be DC |
429 | } |
430 | ||
2a82b8be | 431 | flags = (ap->userdata ? XFS_PICK_USERDATA : 0) | |
0937e0fd | 432 | (ap->flist->xbf_low ? XFS_PICK_LOWSPACE : 0); |
2a82b8be | 433 | |
2cd2ef6a | 434 | err = xfs_filestream_pick_ag(pip, startag, agp, flags, minlen); |
2a82b8be DC |
435 | |
436 | /* | |
2cd2ef6a | 437 | * Only free the item here so we skip over the old AG earlier. |
2a82b8be | 438 | */ |
2cd2ef6a CH |
439 | if (mru) |
440 | xfs_fstrm_free_func(mru); | |
2a82b8be | 441 | |
2cd2ef6a CH |
442 | IRELE(pip); |
443 | exit: | |
444 | if (*agp == NULLAGNUMBER) | |
445 | *agp = 0; | |
446 | return err; | |
447 | } | |
2a82b8be | 448 | |
2cd2ef6a CH |
449 | void |
450 | xfs_filestream_deassociate( | |
451 | struct xfs_inode *ip) | |
452 | { | |
453 | xfs_mru_cache_delete(ip->i_mount->m_filestream, ip->i_ino); | |
454 | } | |
2a82b8be | 455 | |
2cd2ef6a CH |
456 | int |
457 | xfs_filestream_mount( | |
458 | xfs_mount_t *mp) | |
459 | { | |
2a82b8be | 460 | /* |
2cd2ef6a CH |
461 | * The filestream timer tunable is currently fixed within the range of |
462 | * one second to four minutes, with five seconds being the default. The | |
463 | * group count is somewhat arbitrary, but it'd be nice to adhere to the | |
464 | * timer tunable to within about 10 percent. This requires at least 10 | |
465 | * groups. | |
2a82b8be | 466 | */ |
2cd2ef6a CH |
467 | return xfs_mru_cache_create(&mp->m_filestream, xfs_fstrm_centisecs * 10, |
468 | 10, xfs_fstrm_free_func); | |
469 | } | |
2a82b8be | 470 | |
2cd2ef6a CH |
471 | void |
472 | xfs_filestream_unmount( | |
473 | xfs_mount_t *mp) | |
474 | { | |
475 | xfs_mru_cache_destroy(mp->m_filestream); | |
476 | } | |
2a82b8be | 477 | |
2cd2ef6a CH |
478 | |
479 | /* needs to return a positive errno for the init path */ | |
480 | int | |
481 | xfs_filestream_init(void) | |
482 | { | |
483 | item_zone = kmem_zone_init(sizeof(struct xfs_fstrm_item), "fstrm_item"); | |
484 | if (!item_zone) | |
485 | return -ENOMEM; | |
486 | return 0; | |
2a82b8be DC |
487 | } |
488 | ||
2a82b8be | 489 | void |
2cd2ef6a | 490 | xfs_filestream_uninit(void) |
2a82b8be | 491 | { |
2cd2ef6a | 492 | kmem_zone_destroy(item_zone); |
2a82b8be | 493 | } |