xfs: rewrite the filestream allocator using the dentry cache
[deliverable/linux.git] / fs / xfs / xfs_filestream.c
1 /*
2 * Copyright (c) 2006-2007 Silicon Graphics, Inc.
3 * Copyright (c) 2014 Christoph Hellwig.
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"
20 #include "xfs_format.h"
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"
26 #include "xfs_inum.h"
27 #include "xfs_inode.h"
28 #include "xfs_bmap.h"
29 #include "xfs_bmap_util.h"
30 #include "xfs_alloc.h"
31 #include "xfs_mru_cache.h"
32 #include "xfs_dinode.h"
33 #include "xfs_filestream.h"
34 #include "xfs_trace.h"
35
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)
39 #define TRACE_FREE(mp, ip, pip, ag, cnt)
40 #define TRACE_LOOKUP(mp, ip, pip, ag, cnt)
41
42 static kmem_zone_t *item_zone;
43
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 };
54
55 /*
56 * Allocation group filestream associations are tracked with per-ag atomic
57 * counters. These counters allow xfs_filestream_pick_ag() to tell whether a
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 }
131
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
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
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)
158 {
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));
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++) {
179 pag = xfs_perag_get(mp, ag);
180 TRACE_AG_SCAN(mp, ag, atomic_read(&pag->pagf_fstrms));
181
182 if (!pag->pagf_init) {
183 err = xfs_alloc_pagf_init(mp, NULL, ag, trylock);
184 if (err && !trylock) {
185 xfs_perag_put(pag);
186 return err;
187 }
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;
197 max_streams = atomic_read(&pag->pagf_fstrms);
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
212 longest = xfs_alloc_longest_free_extent(mp, pag);
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;
220 streams = atomic_read(&pag->pagf_fstrms);
221 xfs_perag_put(pag);
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:
229 xfs_perag_put(pag);
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);
257 streams = max_streams;
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
269 TRACE_AG_PICK2(mp, startag, *agp, streams, free, nscan, flags);
270
271 if (*agp == NULLAGNUMBER)
272 return 0;
273
274 err = ENOMEM;
275 item = kmem_zone_zalloc(item_zone, KM_MAYFAIL);
276 if (!item)
277 goto out_put_ag;
278
279 item->ag = *agp;
280 item->ip = ip;
281
282 err = xfs_mru_cache_insert(mp->m_filestream, ip->i_ino, &item->mru);
283 if (err) {
284 if (err == EEXIST)
285 err = 0;
286 goto out_free_item;
287 }
288
289 return 0;
290
291 out_free_item:
292 kmem_zone_free(item_zone, item);
293 out_put_ag:
294 xfs_filestream_put_ag(mp, *agp);
295 return err;
296 }
297
298 static struct xfs_inode *
299 xfs_filestream_get_parent(
300 struct xfs_inode *ip)
301 {
302 struct inode *inode = VFS_I(ip), *dir = NULL;
303 struct dentry *dentry, *parent;
304
305 dentry = d_find_alias(inode);
306 if (!dentry)
307 goto out;
308
309 parent = dget_parent(dentry);
310 if (!parent)
311 goto out_dput;
312
313 dir = igrab(parent->d_inode);
314 dput(parent);
315
316 out_dput:
317 dput(dentry);
318 out:
319 return dir ? XFS_I(dir) : NULL;
320 }
321
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(
328 struct xfs_inode *ip)
329 {
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;
335 struct xfs_mru_cache_elem *mru;
336
337 ASSERT(S_ISREG(ip->i_d.di_mode));
338
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);
348
349 ag = item->ag;
350 xfs_mru_cache_done(mp->m_filestream);
351
352 ref = xfs_filestream_peek_ag(ip->i_mount, ag);
353 out:
354 TRACE_LOOKUP(mp, ip, pip, ag, ref);
355 IRELE(pip);
356 return ag;
357 }
358
359 /*
360 * Make sure a directory has a filestream associated with it.
361 *
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.
365 */
366 int
367 xfs_filestream_associate(
368 struct xfs_inode *pip)
369 {
370 struct xfs_mount *mp = pip->i_mount;
371 struct xfs_mru_cache_elem *mru;
372 xfs_agnumber_t startag, ag;
373
374 ASSERT(S_ISDIR(pip->i_d.di_mode));
375
376 /*
377 * If the directory already has a file stream associated we're done.
378 */
379 mru = xfs_mru_cache_lookup(mp->m_filestream, pip->i_ino);
380 if (mru) {
381 xfs_mru_cache_done(mp->m_filestream);
382 return 0;
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) {
390 xfs_agnumber_t rotorstep = xfs_rotorstep;
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
397 return xfs_filestream_pick_ag(pip, startag, &ag, 0, 0);
398 }
399
400 /*
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.
405 */
406 int
407 xfs_filestream_new_ag(
408 struct xfs_bmalloca *ap,
409 xfs_agnumber_t *agp)
410 {
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;
417
418 *agp = NULLAGNUMBER;
419
420 pip = xfs_filestream_get_parent(ip);
421 if (!pip)
422 goto exit;
423
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;
429 }
430
431 flags = (ap->userdata ? XFS_PICK_USERDATA : 0) |
432 (ap->flist->xbf_low ? XFS_PICK_LOWSPACE : 0);
433
434 err = xfs_filestream_pick_ag(pip, startag, agp, flags, minlen);
435
436 /*
437 * Only free the item here so we skip over the old AG earlier.
438 */
439 if (mru)
440 xfs_fstrm_free_func(mru);
441
442 IRELE(pip);
443 exit:
444 if (*agp == NULLAGNUMBER)
445 *agp = 0;
446 return err;
447 }
448
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 }
455
456 int
457 xfs_filestream_mount(
458 xfs_mount_t *mp)
459 {
460 /*
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.
466 */
467 return xfs_mru_cache_create(&mp->m_filestream, xfs_fstrm_centisecs * 10,
468 10, xfs_fstrm_free_func);
469 }
470
471 void
472 xfs_filestream_unmount(
473 xfs_mount_t *mp)
474 {
475 xfs_mru_cache_destroy(mp->m_filestream);
476 }
477
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;
487 }
488
489 void
490 xfs_filestream_uninit(void)
491 {
492 kmem_zone_destroy(item_zone);
493 }
This page took 0.043567 seconds and 6 git commands to generate.