xfs_filestream.c 20 KB

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