xfs_filestream.c 20 KB

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