xfs_filestream.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. * Copyright (c) 2006-2007 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_log.h"
  20. #include "xfs_bmap_btree.h"
  21. #include "xfs_inum.h"
  22. #include "xfs_dinode.h"
  23. #include "xfs_inode.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap.h"
  29. #include "xfs_bmap_util.h"
  30. #include "xfs_alloc.h"
  31. #include "xfs_utils.h"
  32. #include "xfs_mru_cache.h"
  33. #include "xfs_filestream.h"
  34. #include "xfs_trace.h"
  35. #ifdef XFS_FILESTREAMS_TRACE
  36. ktrace_t *xfs_filestreams_trace_buf;
  37. STATIC void
  38. xfs_filestreams_trace(
  39. xfs_mount_t *mp, /* mount point */
  40. int type, /* type of trace */
  41. const char *func, /* source function */
  42. int line, /* source line number */
  43. __psunsigned_t arg0,
  44. __psunsigned_t arg1,
  45. __psunsigned_t arg2,
  46. __psunsigned_t arg3,
  47. __psunsigned_t arg4,
  48. __psunsigned_t arg5)
  49. {
  50. ktrace_enter(xfs_filestreams_trace_buf,
  51. (void *)(__psint_t)(type | (line << 16)),
  52. (void *)func,
  53. (void *)(__psunsigned_t)current_pid(),
  54. (void *)mp,
  55. (void *)(__psunsigned_t)arg0,
  56. (void *)(__psunsigned_t)arg1,
  57. (void *)(__psunsigned_t)arg2,
  58. (void *)(__psunsigned_t)arg3,
  59. (void *)(__psunsigned_t)arg4,
  60. (void *)(__psunsigned_t)arg5,
  61. NULL, NULL, NULL, NULL, NULL, NULL);
  62. }
  63. #define TRACE0(mp,t) TRACE6(mp,t,0,0,0,0,0,0)
  64. #define TRACE1(mp,t,a0) TRACE6(mp,t,a0,0,0,0,0,0)
  65. #define TRACE2(mp,t,a0,a1) TRACE6(mp,t,a0,a1,0,0,0,0)
  66. #define TRACE3(mp,t,a0,a1,a2) TRACE6(mp,t,a0,a1,a2,0,0,0)
  67. #define TRACE4(mp,t,a0,a1,a2,a3) TRACE6(mp,t,a0,a1,a2,a3,0,0)
  68. #define TRACE5(mp,t,a0,a1,a2,a3,a4) TRACE6(mp,t,a0,a1,a2,a3,a4,0)
  69. #define TRACE6(mp,t,a0,a1,a2,a3,a4,a5) \
  70. xfs_filestreams_trace(mp, t, __func__, __LINE__, \
  71. (__psunsigned_t)a0, (__psunsigned_t)a1, \
  72. (__psunsigned_t)a2, (__psunsigned_t)a3, \
  73. (__psunsigned_t)a4, (__psunsigned_t)a5)
  74. #define TRACE_AG_SCAN(mp, ag, ag2) \
  75. TRACE2(mp, XFS_FSTRM_KTRACE_AGSCAN, ag, ag2);
  76. #define TRACE_AG_PICK1(mp, max_ag, maxfree) \
  77. TRACE2(mp, XFS_FSTRM_KTRACE_AGPICK1, max_ag, maxfree);
  78. #define TRACE_AG_PICK2(mp, ag, ag2, cnt, free, scan, flag) \
  79. TRACE6(mp, XFS_FSTRM_KTRACE_AGPICK2, ag, ag2, \
  80. cnt, free, scan, flag)
  81. #define TRACE_UPDATE(mp, ip, ag, cnt, ag2, cnt2) \
  82. TRACE5(mp, XFS_FSTRM_KTRACE_UPDATE, ip, ag, cnt, ag2, cnt2)
  83. #define TRACE_FREE(mp, ip, pip, ag, cnt) \
  84. TRACE4(mp, XFS_FSTRM_KTRACE_FREE, ip, pip, ag, cnt)
  85. #define TRACE_LOOKUP(mp, ip, pip, ag, cnt) \
  86. TRACE4(mp, XFS_FSTRM_KTRACE_ITEM_LOOKUP, ip, pip, ag, cnt)
  87. #define TRACE_ASSOCIATE(mp, ip, pip, ag, cnt) \
  88. TRACE4(mp, XFS_FSTRM_KTRACE_ASSOCIATE, ip, pip, ag, cnt)
  89. #define TRACE_MOVEAG(mp, ip, pip, oag, ocnt, nag, ncnt) \
  90. TRACE6(mp, XFS_FSTRM_KTRACE_MOVEAG, ip, pip, oag, ocnt, nag, ncnt)
  91. #define TRACE_ORPHAN(mp, ip, ag) \
  92. TRACE2(mp, XFS_FSTRM_KTRACE_ORPHAN, ip, ag);
  93. #else
  94. #define TRACE_AG_SCAN(mp, ag, ag2)
  95. #define TRACE_AG_PICK1(mp, max_ag, maxfree)
  96. #define TRACE_AG_PICK2(mp, ag, ag2, cnt, free, scan, flag)
  97. #define TRACE_UPDATE(mp, ip, ag, cnt, ag2, cnt2)
  98. #define TRACE_FREE(mp, ip, pip, ag, cnt)
  99. #define TRACE_LOOKUP(mp, ip, pip, ag, cnt)
  100. #define TRACE_ASSOCIATE(mp, ip, pip, ag, cnt)
  101. #define TRACE_MOVEAG(mp, ip, pip, oag, ocnt, nag, ncnt)
  102. #define TRACE_ORPHAN(mp, ip, ag)
  103. #endif
  104. static kmem_zone_t *item_zone;
  105. /*
  106. * Structure for associating a file or a directory with an allocation group.
  107. * The parent directory pointer is only needed for files, but since there will
  108. * generally be vastly more files than directories in the cache, using the same
  109. * data structure simplifies the code with very little memory overhead.
  110. */
  111. typedef struct fstrm_item
  112. {
  113. xfs_agnumber_t ag; /* AG currently in use for the file/directory. */
  114. xfs_inode_t *ip; /* inode self-pointer. */
  115. xfs_inode_t *pip; /* Parent directory inode pointer. */
  116. } fstrm_item_t;
  117. /*
  118. * Allocation group filestream associations are tracked with per-ag atomic
  119. * counters. These counters allow _xfs_filestream_pick_ag() to tell whether a
  120. * particular AG already has active filestreams associated with it. The mount
  121. * point's m_peraglock is used to protect these counters from per-ag array
  122. * re-allocation during a growfs operation. When xfs_growfs_data_private() is
  123. * about to reallocate the array, it calls xfs_filestream_flush() with the
  124. * m_peraglock held in write mode.
  125. *
  126. * Since xfs_mru_cache_flush() guarantees that all the free functions for all
  127. * the cache elements have finished executing before it returns, it's safe for
  128. * the free functions to use the atomic counters without m_peraglock protection.
  129. * This allows the implementation of xfs_fstrm_free_func() to be agnostic about
  130. * whether it was called with the m_peraglock held in read mode, write mode or
  131. * not held at all. The race condition this addresses is the following:
  132. *
  133. * - The work queue scheduler fires and pulls a filestream directory cache
  134. * element off the LRU end of the cache for deletion, then gets pre-empted.
  135. * - A growfs operation grabs the m_peraglock in write mode, flushes all the
  136. * remaining items from the cache and reallocates the mount point's per-ag
  137. * array, resetting all the counters to zero.
  138. * - The work queue thread resumes and calls the free function for the element
  139. * it started cleaning up earlier. In the process it decrements the
  140. * filestreams counter for an AG that now has no references.
  141. *
  142. * With a shrinkfs feature, the above scenario could panic the system.
  143. *
  144. * All other uses of the following macros should be protected by either the
  145. * m_peraglock held in read mode, or the cache's internal locking exposed by the
  146. * interval between a call to xfs_mru_cache_lookup() and a call to
  147. * xfs_mru_cache_done(). In addition, the m_peraglock must be held in read mode
  148. * when new elements are added to the cache.
  149. *
  150. * Combined, these locking rules ensure that no associations will ever exist in
  151. * the cache that reference per-ag array elements that have since been
  152. * reallocated.
  153. */
  154. static int
  155. xfs_filestream_peek_ag(
  156. xfs_mount_t *mp,
  157. xfs_agnumber_t agno)
  158. {
  159. struct xfs_perag *pag;
  160. int ret;
  161. pag = xfs_perag_get(mp, agno);
  162. ret = atomic_read(&pag->pagf_fstrms);
  163. xfs_perag_put(pag);
  164. return ret;
  165. }
  166. static int
  167. xfs_filestream_get_ag(
  168. xfs_mount_t *mp,
  169. xfs_agnumber_t agno)
  170. {
  171. struct xfs_perag *pag;
  172. int ret;
  173. pag = xfs_perag_get(mp, agno);
  174. ret = atomic_inc_return(&pag->pagf_fstrms);
  175. xfs_perag_put(pag);
  176. return ret;
  177. }
  178. static void
  179. xfs_filestream_put_ag(
  180. xfs_mount_t *mp,
  181. xfs_agnumber_t agno)
  182. {
  183. struct xfs_perag *pag;
  184. pag = xfs_perag_get(mp, agno);
  185. atomic_dec(&pag->pagf_fstrms);
  186. xfs_perag_put(pag);
  187. }
  188. /*
  189. * Scan the AGs starting at startag looking for an AG that isn't in use and has
  190. * at least minlen blocks free.
  191. */
  192. static int
  193. _xfs_filestream_pick_ag(
  194. xfs_mount_t *mp,
  195. xfs_agnumber_t startag,
  196. xfs_agnumber_t *agp,
  197. int flags,
  198. xfs_extlen_t minlen)
  199. {
  200. int streams, max_streams;
  201. int err, trylock, nscan;
  202. xfs_extlen_t longest, free, minfree, maxfree = 0;
  203. xfs_agnumber_t ag, max_ag = NULLAGNUMBER;
  204. struct xfs_perag *pag;
  205. /* 2% of an AG's blocks must be free for it to be chosen. */
  206. minfree = mp->m_sb.sb_agblocks / 50;
  207. ag = startag;
  208. *agp = NULLAGNUMBER;
  209. /* For the first pass, don't sleep trying to init the per-AG. */
  210. trylock = XFS_ALLOC_FLAG_TRYLOCK;
  211. for (nscan = 0; 1; nscan++) {
  212. pag = xfs_perag_get(mp, ag);
  213. TRACE_AG_SCAN(mp, ag, atomic_read(&pag->pagf_fstrms));
  214. if (!pag->pagf_init) {
  215. err = xfs_alloc_pagf_init(mp, NULL, ag, trylock);
  216. if (err && !trylock) {
  217. xfs_perag_put(pag);
  218. return err;
  219. }
  220. }
  221. /* Might fail sometimes during the 1st pass with trylock set. */
  222. if (!pag->pagf_init)
  223. goto next_ag;
  224. /* Keep track of the AG with the most free blocks. */
  225. if (pag->pagf_freeblks > maxfree) {
  226. maxfree = pag->pagf_freeblks;
  227. max_streams = atomic_read(&pag->pagf_fstrms);
  228. max_ag = ag;
  229. }
  230. /*
  231. * The AG reference count does two things: it enforces mutual
  232. * exclusion when examining the suitability of an AG in this
  233. * loop, and it guards against two filestreams being established
  234. * in the same AG as each other.
  235. */
  236. if (xfs_filestream_get_ag(mp, ag) > 1) {
  237. xfs_filestream_put_ag(mp, ag);
  238. goto next_ag;
  239. }
  240. longest = xfs_alloc_longest_free_extent(mp, pag);
  241. if (((minlen && longest >= minlen) ||
  242. (!minlen && pag->pagf_freeblks >= minfree)) &&
  243. (!pag->pagf_metadata || !(flags & XFS_PICK_USERDATA) ||
  244. (flags & XFS_PICK_LOWSPACE))) {
  245. /* Break out, retaining the reference on the AG. */
  246. free = pag->pagf_freeblks;
  247. streams = atomic_read(&pag->pagf_fstrms);
  248. xfs_perag_put(pag);
  249. *agp = ag;
  250. break;
  251. }
  252. /* Drop the reference on this AG, it's not usable. */
  253. xfs_filestream_put_ag(mp, ag);
  254. next_ag:
  255. xfs_perag_put(pag);
  256. /* Move to the next AG, wrapping to AG 0 if necessary. */
  257. if (++ag >= mp->m_sb.sb_agcount)
  258. ag = 0;
  259. /* If a full pass of the AGs hasn't been done yet, continue. */
  260. if (ag != startag)
  261. continue;
  262. /* Allow sleeping in xfs_alloc_pagf_init() on the 2nd pass. */
  263. if (trylock != 0) {
  264. trylock = 0;
  265. continue;
  266. }
  267. /* Finally, if lowspace wasn't set, set it for the 3rd pass. */
  268. if (!(flags & XFS_PICK_LOWSPACE)) {
  269. flags |= XFS_PICK_LOWSPACE;
  270. continue;
  271. }
  272. /*
  273. * Take the AG with the most free space, regardless of whether
  274. * it's already in use by another filestream.
  275. */
  276. if (max_ag != NULLAGNUMBER) {
  277. xfs_filestream_get_ag(mp, max_ag);
  278. TRACE_AG_PICK1(mp, max_ag, maxfree);
  279. streams = max_streams;
  280. free = maxfree;
  281. *agp = max_ag;
  282. break;
  283. }
  284. /* take AG 0 if none matched */
  285. TRACE_AG_PICK1(mp, max_ag, maxfree);
  286. *agp = 0;
  287. return 0;
  288. }
  289. TRACE_AG_PICK2(mp, startag, *agp, streams, free, nscan, flags);
  290. return 0;
  291. }
  292. /*
  293. * Set the allocation group number for a file or a directory, updating inode
  294. * references and per-AG references as appropriate.
  295. */
  296. static int
  297. _xfs_filestream_update_ag(
  298. xfs_inode_t *ip,
  299. xfs_inode_t *pip,
  300. xfs_agnumber_t ag)
  301. {
  302. int err = 0;
  303. xfs_mount_t *mp;
  304. xfs_mru_cache_t *cache;
  305. fstrm_item_t *item;
  306. xfs_agnumber_t old_ag;
  307. xfs_inode_t *old_pip;
  308. /*
  309. * Either ip is a regular file and pip is a directory, or ip is a
  310. * directory and pip is NULL.
  311. */
  312. ASSERT(ip && ((S_ISREG(ip->i_d.di_mode) && pip &&
  313. S_ISDIR(pip->i_d.di_mode)) ||
  314. (S_ISDIR(ip->i_d.di_mode) && !pip)));
  315. mp = ip->i_mount;
  316. cache = mp->m_filestream;
  317. item = xfs_mru_cache_lookup(cache, ip->i_ino);
  318. if (item) {
  319. ASSERT(item->ip == ip);
  320. old_ag = item->ag;
  321. item->ag = ag;
  322. old_pip = item->pip;
  323. item->pip = pip;
  324. xfs_mru_cache_done(cache);
  325. /*
  326. * If the AG has changed, drop the old ref and take a new one,
  327. * effectively transferring the reference from old to new AG.
  328. */
  329. if (ag != old_ag) {
  330. xfs_filestream_put_ag(mp, old_ag);
  331. xfs_filestream_get_ag(mp, ag);
  332. }
  333. /*
  334. * If ip is a file and its pip has changed, drop the old ref and
  335. * take a new one.
  336. */
  337. if (pip && pip != old_pip) {
  338. IRELE(old_pip);
  339. IHOLD(pip);
  340. }
  341. TRACE_UPDATE(mp, ip, old_ag, xfs_filestream_peek_ag(mp, old_ag),
  342. ag, xfs_filestream_peek_ag(mp, ag));
  343. return 0;
  344. }
  345. item = kmem_zone_zalloc(item_zone, KM_MAYFAIL);
  346. if (!item)
  347. return ENOMEM;
  348. item->ag = ag;
  349. item->ip = ip;
  350. item->pip = pip;
  351. err = xfs_mru_cache_insert(cache, ip->i_ino, item);
  352. if (err) {
  353. kmem_zone_free(item_zone, item);
  354. return err;
  355. }
  356. /* Take a reference on the AG. */
  357. xfs_filestream_get_ag(mp, ag);
  358. /*
  359. * Take a reference on the inode itself regardless of whether it's a
  360. * regular file or a directory.
  361. */
  362. IHOLD(ip);
  363. /*
  364. * In the case of a regular file, take a reference on the parent inode
  365. * as well to ensure it remains in-core.
  366. */
  367. if (pip)
  368. IHOLD(pip);
  369. TRACE_UPDATE(mp, ip, ag, xfs_filestream_peek_ag(mp, ag),
  370. ag, xfs_filestream_peek_ag(mp, ag));
  371. return 0;
  372. }
  373. /* xfs_fstrm_free_func(): callback for freeing cached stream items. */
  374. STATIC void
  375. xfs_fstrm_free_func(
  376. unsigned long ino,
  377. void *data)
  378. {
  379. fstrm_item_t *item = (fstrm_item_t *)data;
  380. xfs_inode_t *ip = item->ip;
  381. ASSERT(ip->i_ino == ino);
  382. xfs_iflags_clear(ip, XFS_IFILESTREAM);
  383. /* Drop the reference taken on the AG when the item was added. */
  384. xfs_filestream_put_ag(ip->i_mount, item->ag);
  385. TRACE_FREE(ip->i_mount, ip, item->pip, item->ag,
  386. xfs_filestream_peek_ag(ip->i_mount, item->ag));
  387. /*
  388. * _xfs_filestream_update_ag() always takes a reference on the inode
  389. * itself, whether it's a file or a directory. Release it here.
  390. * This can result in the inode being freed and so we must
  391. * not hold any inode locks when freeing filesstreams objects
  392. * otherwise we can deadlock here.
  393. */
  394. IRELE(ip);
  395. /*
  396. * In the case of a regular file, _xfs_filestream_update_ag() also
  397. * takes a ref on the parent inode to keep it in-core. Release that
  398. * too.
  399. */
  400. if (item->pip)
  401. IRELE(item->pip);
  402. /* Finally, free the memory allocated for the item. */
  403. kmem_zone_free(item_zone, item);
  404. }
  405. /*
  406. * xfs_filestream_init() is called at xfs initialisation time to set up the
  407. * memory zone that will be used for filestream data structure allocation.
  408. */
  409. int
  410. xfs_filestream_init(void)
  411. {
  412. item_zone = kmem_zone_init(sizeof(fstrm_item_t), "fstrm_item");
  413. if (!item_zone)
  414. return -ENOMEM;
  415. return 0;
  416. }
  417. /*
  418. * xfs_filestream_uninit() is called at xfs termination time to destroy the
  419. * memory zone that was used for filestream data structure allocation.
  420. */
  421. void
  422. xfs_filestream_uninit(void)
  423. {
  424. kmem_zone_destroy(item_zone);
  425. }
  426. /*
  427. * xfs_filestream_mount() is called when a file system is mounted with the
  428. * filestream option. It is responsible for allocating the data structures
  429. * needed to track the new file system's file streams.
  430. */
  431. int
  432. xfs_filestream_mount(
  433. xfs_mount_t *mp)
  434. {
  435. int err;
  436. unsigned int lifetime, grp_count;
  437. /*
  438. * The filestream timer tunable is currently fixed within the range of
  439. * one second to four minutes, with five seconds being the default. The
  440. * group count is somewhat arbitrary, but it'd be nice to adhere to the
  441. * timer tunable to within about 10 percent. This requires at least 10
  442. * groups.
  443. */
  444. lifetime = xfs_fstrm_centisecs * 10;
  445. grp_count = 10;
  446. err = xfs_mru_cache_create(&mp->m_filestream, lifetime, grp_count,
  447. xfs_fstrm_free_func);
  448. return err;
  449. }
  450. /*
  451. * xfs_filestream_unmount() is called when a file system that was mounted with
  452. * the filestream option is unmounted. It drains the data structures created
  453. * to track the file system's file streams and frees all the memory that was
  454. * allocated.
  455. */
  456. void
  457. xfs_filestream_unmount(
  458. xfs_mount_t *mp)
  459. {
  460. xfs_mru_cache_destroy(mp->m_filestream);
  461. }
  462. /*
  463. * Return the AG of the filestream the file or directory belongs to, or
  464. * NULLAGNUMBER otherwise.
  465. */
  466. xfs_agnumber_t
  467. xfs_filestream_lookup_ag(
  468. xfs_inode_t *ip)
  469. {
  470. xfs_mru_cache_t *cache;
  471. fstrm_item_t *item;
  472. xfs_agnumber_t ag;
  473. int ref;
  474. if (!S_ISREG(ip->i_d.di_mode) && !S_ISDIR(ip->i_d.di_mode)) {
  475. ASSERT(0);
  476. return NULLAGNUMBER;
  477. }
  478. cache = ip->i_mount->m_filestream;
  479. item = xfs_mru_cache_lookup(cache, ip->i_ino);
  480. if (!item) {
  481. TRACE_LOOKUP(ip->i_mount, ip, NULL, NULLAGNUMBER, 0);
  482. return NULLAGNUMBER;
  483. }
  484. ASSERT(ip == item->ip);
  485. ag = item->ag;
  486. ref = xfs_filestream_peek_ag(ip->i_mount, ag);
  487. xfs_mru_cache_done(cache);
  488. TRACE_LOOKUP(ip->i_mount, ip, item->pip, ag, ref);
  489. return ag;
  490. }
  491. /*
  492. * xfs_filestream_associate() should only be called to associate a regular file
  493. * with its parent directory. Calling it with a child directory isn't
  494. * appropriate because filestreams don't apply to entire directory hierarchies.
  495. * Creating a file in a child directory of an existing filestream directory
  496. * starts a new filestream with its own allocation group association.
  497. *
  498. * Returns < 0 on error, 0 if successful association occurred, > 0 if
  499. * we failed to get an association because of locking issues.
  500. */
  501. int
  502. xfs_filestream_associate(
  503. xfs_inode_t *pip,
  504. xfs_inode_t *ip)
  505. {
  506. xfs_mount_t *mp;
  507. xfs_mru_cache_t *cache;
  508. fstrm_item_t *item;
  509. xfs_agnumber_t ag, rotorstep, startag;
  510. int err = 0;
  511. ASSERT(S_ISDIR(pip->i_d.di_mode));
  512. ASSERT(S_ISREG(ip->i_d.di_mode));
  513. if (!S_ISDIR(pip->i_d.di_mode) || !S_ISREG(ip->i_d.di_mode))
  514. return -EINVAL;
  515. mp = pip->i_mount;
  516. cache = mp->m_filestream;
  517. /*
  518. * We have a problem, Houston.
  519. *
  520. * Taking the iolock here violates inode locking order - we already
  521. * hold the ilock. Hence if we block getting this lock we may never
  522. * wake. Unfortunately, that means if we can't get the lock, we're
  523. * screwed in terms of getting a stream association - we can't spin
  524. * waiting for the lock because someone else is waiting on the lock we
  525. * hold and we cannot drop that as we are in a transaction here.
  526. *
  527. * Lucky for us, this inversion is not a problem because it's a
  528. * directory inode that we are trying to lock here.
  529. *
  530. * So, if we can't get the iolock without sleeping then just give up
  531. */
  532. if (!xfs_ilock_nowait(pip, XFS_IOLOCK_EXCL))
  533. return 1;
  534. /* If the parent directory is already in the cache, use its AG. */
  535. item = xfs_mru_cache_lookup(cache, pip->i_ino);
  536. if (item) {
  537. ASSERT(item->ip == pip);
  538. ag = item->ag;
  539. xfs_mru_cache_done(cache);
  540. TRACE_LOOKUP(mp, pip, pip, ag, xfs_filestream_peek_ag(mp, ag));
  541. err = _xfs_filestream_update_ag(ip, pip, ag);
  542. goto exit;
  543. }
  544. /*
  545. * Set the starting AG using the rotor for inode32, otherwise
  546. * use the directory inode's AG.
  547. */
  548. if (mp->m_flags & XFS_MOUNT_32BITINODES) {
  549. rotorstep = xfs_rotorstep;
  550. startag = (mp->m_agfrotor / rotorstep) % mp->m_sb.sb_agcount;
  551. mp->m_agfrotor = (mp->m_agfrotor + 1) %
  552. (mp->m_sb.sb_agcount * rotorstep);
  553. } else
  554. startag = XFS_INO_TO_AGNO(mp, pip->i_ino);
  555. /* Pick a new AG for the parent inode starting at startag. */
  556. err = _xfs_filestream_pick_ag(mp, startag, &ag, 0, 0);
  557. if (err || ag == NULLAGNUMBER)
  558. goto exit_did_pick;
  559. /* Associate the parent inode with the AG. */
  560. err = _xfs_filestream_update_ag(pip, NULL, ag);
  561. if (err)
  562. goto exit_did_pick;
  563. /* Associate the file inode with the AG. */
  564. err = _xfs_filestream_update_ag(ip, pip, ag);
  565. if (err)
  566. goto exit_did_pick;
  567. TRACE_ASSOCIATE(mp, ip, pip, ag, xfs_filestream_peek_ag(mp, ag));
  568. exit_did_pick:
  569. /*
  570. * If _xfs_filestream_pick_ag() returned a valid AG, remove the
  571. * reference it took on it, since the file and directory will have taken
  572. * their own now if they were successfully cached.
  573. */
  574. if (ag != NULLAGNUMBER)
  575. xfs_filestream_put_ag(mp, ag);
  576. exit:
  577. xfs_iunlock(pip, XFS_IOLOCK_EXCL);
  578. return -err;
  579. }
  580. /*
  581. * Pick a new allocation group for the current file and its file stream. This
  582. * function is called by xfs_bmap_filestreams() with the mount point's per-ag
  583. * lock held.
  584. */
  585. int
  586. xfs_filestream_new_ag(
  587. struct xfs_bmalloca *ap,
  588. xfs_agnumber_t *agp)
  589. {
  590. int flags, err;
  591. xfs_inode_t *ip, *pip = NULL;
  592. xfs_mount_t *mp;
  593. xfs_mru_cache_t *cache;
  594. xfs_extlen_t minlen;
  595. fstrm_item_t *dir, *file;
  596. xfs_agnumber_t ag = NULLAGNUMBER;
  597. ip = ap->ip;
  598. mp = ip->i_mount;
  599. cache = mp->m_filestream;
  600. minlen = ap->length;
  601. *agp = NULLAGNUMBER;
  602. /*
  603. * Look for the file in the cache, removing it if it's found. Doing
  604. * this allows it to be held across the dir lookup that follows.
  605. */
  606. file = xfs_mru_cache_remove(cache, ip->i_ino);
  607. if (file) {
  608. ASSERT(ip == file->ip);
  609. /* Save the file's parent inode and old AG number for later. */
  610. pip = file->pip;
  611. ag = file->ag;
  612. /* Look for the file's directory in the cache. */
  613. dir = xfs_mru_cache_lookup(cache, pip->i_ino);
  614. if (dir) {
  615. ASSERT(pip == dir->ip);
  616. /*
  617. * If the directory has already moved on to a new AG,
  618. * use that AG as the new AG for the file. Don't
  619. * forget to twiddle the AG refcounts to match the
  620. * movement.
  621. */
  622. if (dir->ag != file->ag) {
  623. xfs_filestream_put_ag(mp, file->ag);
  624. xfs_filestream_get_ag(mp, dir->ag);
  625. *agp = file->ag = dir->ag;
  626. }
  627. xfs_mru_cache_done(cache);
  628. }
  629. /*
  630. * Put the file back in the cache. If this fails, the free
  631. * function needs to be called to tidy up in the same way as if
  632. * the item had simply expired from the cache.
  633. */
  634. err = xfs_mru_cache_insert(cache, ip->i_ino, file);
  635. if (err) {
  636. xfs_fstrm_free_func(ip->i_ino, file);
  637. return err;
  638. }
  639. /*
  640. * If the file's AG was moved to the directory's new AG, there's
  641. * nothing more to be done.
  642. */
  643. if (*agp != NULLAGNUMBER) {
  644. TRACE_MOVEAG(mp, ip, pip,
  645. ag, xfs_filestream_peek_ag(mp, ag),
  646. *agp, xfs_filestream_peek_ag(mp, *agp));
  647. return 0;
  648. }
  649. }
  650. /*
  651. * If the file's parent directory is known, take its iolock in exclusive
  652. * mode to prevent two sibling files from racing each other to migrate
  653. * themselves and their parent to different AGs.
  654. *
  655. * Note that we lock the parent directory iolock inside the child
  656. * iolock here. That's fine as we never hold both parent and child
  657. * iolock in any other place. This is different from the ilock,
  658. * which requires locking of the child after the parent for namespace
  659. * operations.
  660. */
  661. if (pip)
  662. xfs_ilock(pip, XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
  663. /*
  664. * A new AG needs to be found for the file. If the file's parent
  665. * directory is also known, it will be moved to the new AG as well to
  666. * ensure that files created inside it in future use the new AG.
  667. */
  668. ag = (ag == NULLAGNUMBER) ? 0 : (ag + 1) % mp->m_sb.sb_agcount;
  669. flags = (ap->userdata ? XFS_PICK_USERDATA : 0) |
  670. (ap->flist->xbf_low ? XFS_PICK_LOWSPACE : 0);
  671. err = _xfs_filestream_pick_ag(mp, ag, agp, flags, minlen);
  672. if (err || *agp == NULLAGNUMBER)
  673. goto exit;
  674. /*
  675. * If the file wasn't found in the file cache, then its parent directory
  676. * inode isn't known. For this to have happened, the file must either
  677. * be pre-existing, or it was created long enough ago that its cache
  678. * entry has expired. This isn't the sort of usage that the filestreams
  679. * allocator is trying to optimise, so there's no point trying to track
  680. * its new AG somehow in the filestream data structures.
  681. */
  682. if (!pip) {
  683. TRACE_ORPHAN(mp, ip, *agp);
  684. goto exit;
  685. }
  686. /* Associate the parent inode with the AG. */
  687. err = _xfs_filestream_update_ag(pip, NULL, *agp);
  688. if (err)
  689. goto exit;
  690. /* Associate the file inode with the AG. */
  691. err = _xfs_filestream_update_ag(ip, pip, *agp);
  692. if (err)
  693. goto exit;
  694. TRACE_MOVEAG(mp, ip, pip, NULLAGNUMBER, 0,
  695. *agp, xfs_filestream_peek_ag(mp, *agp));
  696. exit:
  697. /*
  698. * If _xfs_filestream_pick_ag() returned a valid AG, remove the
  699. * reference it took on it, since the file and directory will have taken
  700. * their own now if they were successfully cached.
  701. */
  702. if (*agp != NULLAGNUMBER)
  703. xfs_filestream_put_ag(mp, *agp);
  704. else
  705. *agp = 0;
  706. if (pip)
  707. xfs_iunlock(pip, XFS_IOLOCK_EXCL);
  708. return err;
  709. }
  710. /*
  711. * Remove an association between an inode and a filestream object.
  712. * Typically this is done on last close of an unlinked file.
  713. */
  714. void
  715. xfs_filestream_deassociate(
  716. xfs_inode_t *ip)
  717. {
  718. xfs_mru_cache_t *cache = ip->i_mount->m_filestream;
  719. xfs_mru_cache_delete(cache, ip->i_ino);
  720. }