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