ffs_balloc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /* $OpenBSD: ffs_balloc.c,v 1.43 2015/03/14 03:38:52 jsg Exp $ */
  2. /* $NetBSD: ffs_balloc.c,v 1.3 1996/02/09 22:22:21 christos Exp $ */
  3. /*
  4. * Copyright (c) 2002 Networks Associates Technology, Inc.
  5. * All rights reserved.
  6. *
  7. * This software was developed for the FreeBSD Project by Marshall
  8. * Kirk McKusick and Network Associates Laboratories, the Security
  9. * Research Division of Network Associates, Inc. under DARPA/SPAWAR
  10. * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
  11. * research program.
  12. *
  13. * Copyright (c) 1982, 1986, 1989, 1993
  14. * The Regents of the University of California. All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. * 1. Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. * 3. Neither the name of the University nor the names of its contributors
  25. * may be used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  29. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. * SUCH DAMAGE.
  39. *
  40. * @(#)ffs_balloc.c 8.4 (Berkeley) 9/23/93
  41. */
  42. #include <sys/param.h>
  43. #include <sys/systm.h>
  44. #include <sys/buf.h>
  45. #include <sys/proc.h>
  46. #include <sys/mount.h>
  47. #include <sys/vnode.h>
  48. #include <ufs/ufs/quota.h>
  49. #include <ufs/ufs/inode.h>
  50. #include <ufs/ufs/ufsmount.h>
  51. #include <ufs/ufs/ufs_extern.h>
  52. #include <ufs/ffs/fs.h>
  53. #include <ufs/ffs/ffs_extern.h>
  54. int ffs1_balloc(struct inode *, off_t, int, struct ucred *, int, struct buf **);
  55. #ifdef FFS2
  56. int ffs2_balloc(struct inode *, off_t, int, struct ucred *, int, struct buf **);
  57. #endif
  58. /*
  59. * Balloc defines the structure of file system storage
  60. * by allocating the physical blocks on a device given
  61. * the inode and the logical block number in a file.
  62. */
  63. int
  64. ffs1_balloc(struct inode *ip, off_t startoffset, int size, struct ucred *cred,
  65. int flags, struct buf **bpp)
  66. {
  67. daddr_t lbn, nb, newb, pref;
  68. struct fs *fs;
  69. struct buf *bp, *nbp;
  70. struct vnode *vp;
  71. struct proc *p;
  72. struct indir indirs[NIADDR + 2];
  73. int32_t *bap;
  74. int deallocated, osize, nsize, num, i, error;
  75. int32_t *allocib, *blkp, *allocblk, allociblk[NIADDR+1];
  76. int unwindidx = -1;
  77. vp = ITOV(ip);
  78. fs = ip->i_fs;
  79. p = curproc;
  80. lbn = lblkno(fs, startoffset);
  81. size = blkoff(fs, startoffset) + size;
  82. if (size > fs->fs_bsize)
  83. panic("ffs1_balloc: blk too big");
  84. if (bpp != NULL)
  85. *bpp = NULL;
  86. if (lbn < 0)
  87. return (EFBIG);
  88. /*
  89. * If the next write will extend the file into a new block,
  90. * and the file is currently composed of a fragment
  91. * this fragment has to be extended to be a full block.
  92. */
  93. nb = lblkno(fs, ip->i_ffs1_size);
  94. if (nb < NDADDR && nb < lbn) {
  95. osize = blksize(fs, ip, nb);
  96. if (osize < fs->fs_bsize && osize > 0) {
  97. error = ffs_realloccg(ip, nb,
  98. ffs1_blkpref(ip, nb, (int)nb, &ip->i_ffs1_db[0]),
  99. osize, (int)fs->fs_bsize, cred, bpp, &newb);
  100. if (error)
  101. return (error);
  102. if (DOINGSOFTDEP(vp))
  103. softdep_setup_allocdirect(ip, nb, newb,
  104. ip->i_ffs1_db[nb], fs->fs_bsize, osize,
  105. bpp ? *bpp : NULL);
  106. ip->i_ffs1_size = lblktosize(fs, nb + 1);
  107. uvm_vnp_setsize(vp, ip->i_ffs1_size);
  108. ip->i_ffs1_db[nb] = newb;
  109. ip->i_flag |= IN_CHANGE | IN_UPDATE;
  110. if (bpp != NULL) {
  111. if (flags & B_SYNC)
  112. bwrite(*bpp);
  113. else
  114. bawrite(*bpp);
  115. }
  116. }
  117. }
  118. /*
  119. * The first NDADDR blocks are direct blocks
  120. */
  121. if (lbn < NDADDR) {
  122. nb = ip->i_ffs1_db[lbn];
  123. if (nb != 0 && ip->i_ffs1_size >= lblktosize(fs, lbn + 1)) {
  124. /*
  125. * The block is an already-allocated direct block
  126. * and the file already extends past this block,
  127. * thus this must be a whole block.
  128. * Just read the block (if requested).
  129. */
  130. if (bpp != NULL) {
  131. error = bread(vp, lbn, fs->fs_bsize, bpp);
  132. if (error) {
  133. brelse(*bpp);
  134. return (error);
  135. }
  136. }
  137. return (0);
  138. }
  139. if (nb != 0) {
  140. /*
  141. * Consider need to reallocate a fragment.
  142. */
  143. osize = fragroundup(fs, blkoff(fs, ip->i_ffs1_size));
  144. nsize = fragroundup(fs, size);
  145. if (nsize <= osize) {
  146. /*
  147. * The existing block is already
  148. * at least as big as we want.
  149. * Just read the block (if requested).
  150. */
  151. if (bpp != NULL) {
  152. error = bread(vp, lbn, fs->fs_bsize,
  153. bpp);
  154. if (error) {
  155. brelse(*bpp);
  156. return (error);
  157. }
  158. (*bpp)->b_bcount = osize;
  159. }
  160. return (0);
  161. } else {
  162. /*
  163. * The existing block is smaller than we
  164. * want, grow it.
  165. */
  166. error = ffs_realloccg(ip, lbn,
  167. ffs1_blkpref(ip, lbn, (int)lbn,
  168. &ip->i_ffs1_db[0]),
  169. osize, nsize, cred, bpp, &newb);
  170. if (error)
  171. return (error);
  172. if (DOINGSOFTDEP(vp))
  173. softdep_setup_allocdirect(ip, lbn,
  174. newb, nb, nsize, osize,
  175. bpp ? *bpp : NULL);
  176. }
  177. } else {
  178. /*
  179. * The block was not previously allocated,
  180. * allocate a new block or fragment.
  181. */
  182. if (ip->i_ffs1_size < lblktosize(fs, lbn + 1))
  183. nsize = fragroundup(fs, size);
  184. else
  185. nsize = fs->fs_bsize;
  186. error = ffs_alloc(ip, lbn,
  187. ffs1_blkpref(ip, lbn, (int)lbn, &ip->i_ffs1_db[0]),
  188. nsize, cred, &newb);
  189. if (error)
  190. return (error);
  191. if (bpp != NULL) {
  192. *bpp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
  193. if (nsize < fs->fs_bsize)
  194. (*bpp)->b_bcount = nsize;
  195. (*bpp)->b_blkno = fsbtodb(fs, newb);
  196. if (flags & B_CLRBUF)
  197. clrbuf(*bpp);
  198. }
  199. if (DOINGSOFTDEP(vp))
  200. softdep_setup_allocdirect(ip, lbn, newb, 0,
  201. nsize, 0, bpp ? *bpp : NULL);
  202. }
  203. ip->i_ffs1_db[lbn] = newb;
  204. ip->i_flag |= IN_CHANGE | IN_UPDATE;
  205. return (0);
  206. }
  207. /*
  208. * Determine the number of levels of indirection.
  209. */
  210. pref = 0;
  211. if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
  212. return(error);
  213. #ifdef DIAGNOSTIC
  214. if (num < 1)
  215. panic ("ffs1_balloc: ufs_bmaparray returned indirect block");
  216. #endif
  217. /*
  218. * Fetch the first indirect block allocating if necessary.
  219. */
  220. --num;
  221. nb = ip->i_ffs1_ib[indirs[0].in_off];
  222. allocib = NULL;
  223. allocblk = allociblk;
  224. if (nb == 0) {
  225. pref = ffs1_blkpref(ip, lbn, -indirs[0].in_off - 1, NULL);
  226. error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
  227. cred, &newb);
  228. if (error)
  229. goto fail;
  230. nb = newb;
  231. *allocblk++ = nb;
  232. bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
  233. bp->b_blkno = fsbtodb(fs, nb);
  234. clrbuf(bp);
  235. if (DOINGSOFTDEP(vp)) {
  236. softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
  237. newb, 0, fs->fs_bsize, 0, bp);
  238. bdwrite(bp);
  239. } else {
  240. /*
  241. * Write synchronously so that indirect blocks
  242. * never point at garbage.
  243. */
  244. if ((error = bwrite(bp)) != 0)
  245. goto fail;
  246. }
  247. allocib = &ip->i_ffs1_ib[indirs[0].in_off];
  248. *allocib = nb;
  249. ip->i_flag |= IN_CHANGE | IN_UPDATE;
  250. }
  251. /*
  252. * Fetch through the indirect blocks, allocating as necessary.
  253. */
  254. for (i = 1;;) {
  255. error = bread(vp, indirs[i].in_lbn, (int)fs->fs_bsize, &bp);
  256. if (error) {
  257. brelse(bp);
  258. goto fail;
  259. }
  260. bap = (int32_t *)bp->b_data;
  261. nb = bap[indirs[i].in_off];
  262. if (i == num)
  263. break;
  264. i++;
  265. if (nb != 0) {
  266. brelse(bp);
  267. continue;
  268. }
  269. if (pref == 0)
  270. pref = ffs1_blkpref(ip, lbn, i - num - 1, NULL);
  271. error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
  272. &newb);
  273. if (error) {
  274. brelse(bp);
  275. goto fail;
  276. }
  277. nb = newb;
  278. *allocblk++ = nb;
  279. nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
  280. nbp->b_blkno = fsbtodb(fs, nb);
  281. clrbuf(nbp);
  282. if (DOINGSOFTDEP(vp)) {
  283. softdep_setup_allocindir_meta(nbp, ip, bp,
  284. indirs[i - 1].in_off, nb);
  285. bdwrite(nbp);
  286. } else {
  287. /*
  288. * Write synchronously so that indirect blocks
  289. * never point at garbage.
  290. */
  291. if ((error = bwrite(nbp)) != 0) {
  292. brelse(bp);
  293. goto fail;
  294. }
  295. }
  296. bap[indirs[i - 1].in_off] = nb;
  297. if (allocib == NULL && unwindidx < 0)
  298. unwindidx = i - 1;
  299. /*
  300. * If required, write synchronously, otherwise use
  301. * delayed write.
  302. */
  303. if (flags & B_SYNC) {
  304. bwrite(bp);
  305. } else {
  306. bdwrite(bp);
  307. }
  308. }
  309. /*
  310. * Get the data block, allocating if necessary.
  311. */
  312. if (nb == 0) {
  313. pref = ffs1_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
  314. error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
  315. &newb);
  316. if (error) {
  317. brelse(bp);
  318. goto fail;
  319. }
  320. nb = newb;
  321. *allocblk++ = nb;
  322. if (bpp != NULL) {
  323. nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
  324. nbp->b_blkno = fsbtodb(fs, nb);
  325. if (flags & B_CLRBUF)
  326. clrbuf(nbp);
  327. *bpp = nbp;
  328. }
  329. if (DOINGSOFTDEP(vp))
  330. softdep_setup_allocindir_page(ip, lbn, bp,
  331. indirs[i].in_off, nb, 0, bpp ? *bpp : NULL);
  332. bap[indirs[i].in_off] = nb;
  333. /*
  334. * If required, write synchronously, otherwise use
  335. * delayed write.
  336. */
  337. if (flags & B_SYNC) {
  338. bwrite(bp);
  339. } else {
  340. bdwrite(bp);
  341. }
  342. return (0);
  343. }
  344. brelse(bp);
  345. if (bpp != NULL) {
  346. if (flags & B_CLRBUF) {
  347. error = bread(vp, lbn, (int)fs->fs_bsize, &nbp);
  348. if (error) {
  349. brelse(nbp);
  350. goto fail;
  351. }
  352. } else {
  353. nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
  354. nbp->b_blkno = fsbtodb(fs, nb);
  355. }
  356. *bpp = nbp;
  357. }
  358. return (0);
  359. fail:
  360. /*
  361. * If we have failed to allocate any blocks, simply return the error.
  362. * This is the usual case and avoids the need to fsync the file.
  363. */
  364. if (allocblk == allociblk && allocib == NULL && unwindidx == -1)
  365. return (error);
  366. /*
  367. * If we have failed part way through block allocation, we have to
  368. * deallocate any indirect blocks that we have allocated. We have to
  369. * fsync the file before we start to get rid of all of its
  370. * dependencies so that we do not leave them dangling. We have to sync
  371. * it at the end so that the softdep code does not find any untracked
  372. * changes. Although this is really slow, running out of disk space is
  373. * not expected to be a common occurrence. The error return from fsync
  374. * is ignored as we already have an error to return to the user.
  375. */
  376. VOP_FSYNC(vp, p->p_ucred, MNT_WAIT, p);
  377. for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
  378. ffs_blkfree(ip, *blkp, fs->fs_bsize);
  379. deallocated += fs->fs_bsize;
  380. }
  381. if (allocib != NULL) {
  382. *allocib = 0;
  383. } else if (unwindidx >= 0) {
  384. int r;
  385. r = bread(vp, indirs[unwindidx].in_lbn, (int)fs->fs_bsize, &bp);
  386. if (r)
  387. panic("Could not unwind indirect block, error %d", r);
  388. bap = (int32_t *)bp->b_data;
  389. bap[indirs[unwindidx].in_off] = 0;
  390. if (flags & B_SYNC) {
  391. bwrite(bp);
  392. } else {
  393. bdwrite(bp);
  394. }
  395. }
  396. if (deallocated) {
  397. /*
  398. * Restore user's disk quota because allocation failed.
  399. */
  400. (void)ufs_quota_free_blocks(ip, btodb(deallocated), cred);
  401. ip->i_ffs1_blocks -= btodb(deallocated);
  402. ip->i_flag |= IN_CHANGE | IN_UPDATE;
  403. }
  404. VOP_FSYNC(vp, p->p_ucred, MNT_WAIT, p);
  405. return (error);
  406. }
  407. #ifdef FFS2
  408. int
  409. ffs2_balloc(struct inode *ip, off_t off, int size, struct ucred *cred,
  410. int flags, struct buf **bpp)
  411. {
  412. daddr_t lbn, lastlbn, nb, newb, *blkp;
  413. daddr_t pref, *allocblk, allociblk[NIADDR + 1];
  414. daddr_t *bap, *allocib;
  415. int deallocated, osize, nsize, num, i, error, unwindidx, r;
  416. struct buf *bp, *nbp;
  417. struct indir indirs[NIADDR + 2];
  418. struct fs *fs;
  419. struct vnode *vp;
  420. struct proc *p;
  421. vp = ITOV(ip);
  422. fs = ip->i_fs;
  423. p = curproc;
  424. unwindidx = -1;
  425. lbn = lblkno(fs, off);
  426. size = blkoff(fs, off) + size;
  427. if (size > fs->fs_bsize)
  428. panic("ffs2_balloc: block too big");
  429. if (bpp != NULL)
  430. *bpp = NULL;
  431. if (lbn < 0)
  432. return (EFBIG);
  433. /*
  434. * If the next write will extend the file into a new block, and the
  435. * file is currently composed of a fragment, this fragment has to be
  436. * extended to be a full block.
  437. */
  438. lastlbn = lblkno(fs, ip->i_ffs2_size);
  439. if (lastlbn < NDADDR && lastlbn < lbn) {
  440. nb = lastlbn;
  441. osize = blksize(fs, ip, nb);
  442. if (osize < fs->fs_bsize && osize > 0) {
  443. error = ffs_realloccg(ip, nb, ffs2_blkpref(ip,
  444. lastlbn, nb, &ip->i_ffs2_db[0]), osize,
  445. (int) fs->fs_bsize, cred, bpp, &newb);
  446. if (error)
  447. return (error);
  448. if (DOINGSOFTDEP(vp))
  449. softdep_setup_allocdirect(ip, nb, newb,
  450. ip->i_ffs2_db[nb], fs->fs_bsize, osize,
  451. bpp ? *bpp : NULL);
  452. ip->i_ffs2_size = lblktosize(fs, nb + 1);
  453. uvm_vnp_setsize(vp, ip->i_ffs2_size);
  454. ip->i_ffs2_db[nb] = newb;
  455. ip->i_flag |= IN_CHANGE | IN_UPDATE;
  456. if (bpp) {
  457. if (flags & B_SYNC)
  458. bwrite(*bpp);
  459. else
  460. bawrite(*bpp);
  461. }
  462. }
  463. }
  464. /*
  465. * The first NDADDR blocks are direct.
  466. */
  467. if (lbn < NDADDR) {
  468. nb = ip->i_ffs2_db[lbn];
  469. if (nb != 0 && ip->i_ffs2_size >= lblktosize(fs, lbn + 1)) {
  470. /*
  471. * The direct block is already allocated and the file
  472. * extends past this block, thus this must be a whole
  473. * block. Just read it, if requested.
  474. */
  475. if (bpp != NULL) {
  476. error = bread(vp, lbn, fs->fs_bsize, bpp);
  477. if (error) {
  478. brelse(*bpp);
  479. return (error);
  480. }
  481. }
  482. return (0);
  483. }
  484. if (nb != 0) {
  485. /*
  486. * Consider the need to allocate a fragment.
  487. */
  488. osize = fragroundup(fs, blkoff(fs, ip->i_ffs2_size));
  489. nsize = fragroundup(fs, size);
  490. if (nsize <= osize) {
  491. /*
  492. * The existing block is already at least as
  493. * big as we want. Just read it, if requested.
  494. */
  495. if (bpp != NULL) {
  496. error = bread(vp, lbn, fs->fs_bsize,
  497. bpp);
  498. if (error) {
  499. brelse(*bpp);
  500. return (error);
  501. }
  502. (*bpp)->b_bcount = osize;
  503. }
  504. return (0);
  505. } else {
  506. /*
  507. * The existing block is smaller than we want,
  508. * grow it.
  509. */
  510. error = ffs_realloccg(ip, lbn,
  511. ffs2_blkpref(ip, lbn, (int) lbn,
  512. &ip->i_ffs2_db[0]), osize, nsize, cred,
  513. bpp, &newb);
  514. if (error)
  515. return (error);
  516. if (DOINGSOFTDEP(vp))
  517. softdep_setup_allocdirect(ip, lbn,
  518. newb, nb, nsize, osize,
  519. bpp ? *bpp : NULL);
  520. }
  521. } else {
  522. /*
  523. * The block was not previously allocated, allocate a
  524. * new block or fragment.
  525. */
  526. if (ip->i_ffs2_size < lblktosize(fs, lbn + 1))
  527. nsize = fragroundup(fs, size);
  528. else
  529. nsize = fs->fs_bsize;
  530. error = ffs_alloc(ip, lbn, ffs2_blkpref(ip, lbn,
  531. (int) lbn, &ip->i_ffs2_db[0]), nsize, cred, &newb);
  532. if (error)
  533. return (error);
  534. if (bpp != NULL) {
  535. bp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
  536. if (nsize < fs->fs_bsize)
  537. bp->b_bcount = nsize;
  538. bp->b_blkno = fsbtodb(fs, newb);
  539. if (flags & B_CLRBUF)
  540. clrbuf(bp);
  541. *bpp = bp;
  542. }
  543. if (DOINGSOFTDEP(vp))
  544. softdep_setup_allocdirect(ip, lbn, newb, 0,
  545. nsize, 0, bpp ? *bpp : NULL);
  546. }
  547. ip->i_ffs2_db[lbn] = newb;
  548. ip->i_flag |= IN_CHANGE | IN_UPDATE;
  549. return (0);
  550. }
  551. /*
  552. * Determine the number of levels of indirection.
  553. */
  554. pref = 0;
  555. error = ufs_getlbns(vp, lbn, indirs, &num);
  556. if (error)
  557. return (error);
  558. #ifdef DIAGNOSTIC
  559. if (num < 1)
  560. panic("ffs2_balloc: ufs_bmaparray returned indirect block");
  561. #endif
  562. /*
  563. * Fetch the first indirect block allocating it necessary.
  564. */
  565. --num;
  566. nb = ip->i_ffs2_ib[indirs[0].in_off];
  567. allocib = NULL;
  568. allocblk = allociblk;
  569. if (nb == 0) {
  570. pref = ffs2_blkpref(ip, lbn, -indirs[0].in_off - 1, NULL);
  571. error = ffs_alloc(ip, lbn, pref, (int) fs->fs_bsize, cred,
  572. &newb);
  573. if (error)
  574. goto fail;
  575. nb = newb;
  576. *allocblk++ = nb;
  577. bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
  578. bp->b_blkno = fsbtodb(fs, nb);
  579. clrbuf(bp);
  580. if (DOINGSOFTDEP(vp)) {
  581. softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
  582. newb, 0, fs->fs_bsize, 0, bp);
  583. bdwrite(bp);
  584. } else {
  585. /*
  586. * Write synchronously so that indirect blocks never
  587. * point at garbage.
  588. */
  589. error = bwrite(bp);
  590. if (error)
  591. goto fail;
  592. }
  593. unwindidx = 0;
  594. allocib = &ip->i_ffs2_ib[indirs[0].in_off];
  595. *allocib = nb;
  596. ip->i_flag |= IN_CHANGE | IN_UPDATE;
  597. }
  598. /*
  599. * Fetch through the indirect blocks, allocating as necessary.
  600. */
  601. for (i = 1;;) {
  602. error = bread(vp, indirs[i].in_lbn, (int)fs->fs_bsize, &bp);
  603. if (error) {
  604. brelse(bp);
  605. goto fail;
  606. }
  607. bap = (int64_t *) bp->b_data;
  608. nb = bap[indirs[i].in_off];
  609. if (i == num)
  610. break;
  611. i++;
  612. if (nb != 0) {
  613. brelse(bp);
  614. continue;
  615. }
  616. if (pref == 0)
  617. pref = ffs2_blkpref(ip, lbn, i - num - 1, NULL);
  618. error = ffs_alloc(ip, lbn, pref, (int) fs->fs_bsize, cred,
  619. &newb);
  620. if (error) {
  621. brelse(bp);
  622. goto fail;
  623. }
  624. nb = newb;
  625. *allocblk++ = nb;
  626. nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
  627. nbp->b_blkno = fsbtodb(fs, nb);
  628. clrbuf(nbp);
  629. if (DOINGSOFTDEP(vp)) {
  630. softdep_setup_allocindir_meta(nbp, ip, bp,
  631. indirs[i - 1].in_off, nb);
  632. bdwrite(nbp);
  633. } else {
  634. /*
  635. * Write synchronously so that indirect blocks never
  636. * point at garbage.
  637. */
  638. error = bwrite(nbp);
  639. if (error) {
  640. brelse(bp);
  641. goto fail;
  642. }
  643. }
  644. if (unwindidx < 0)
  645. unwindidx = i - 1;
  646. bap[indirs[i - 1].in_off] = nb;
  647. /*
  648. * If required, write synchronously, otherwise use delayed
  649. * write.
  650. */
  651. if (flags & B_SYNC)
  652. bwrite(bp);
  653. else
  654. bdwrite(bp);
  655. }
  656. /*
  657. * Get the data block, allocating if necessary.
  658. */
  659. if (nb == 0) {
  660. pref = ffs2_blkpref(ip, lbn, indirs[num].in_off, &bap[0]);
  661. error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
  662. &newb);
  663. if (error) {
  664. brelse(bp);
  665. goto fail;
  666. }
  667. nb = newb;
  668. *allocblk++ = nb;
  669. if (bpp != NULL) {
  670. nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
  671. nbp->b_blkno = fsbtodb(fs, nb);
  672. if (flags & B_CLRBUF)
  673. clrbuf(nbp);
  674. *bpp = nbp;
  675. }
  676. if (DOINGSOFTDEP(vp))
  677. softdep_setup_allocindir_page(ip, lbn, bp,
  678. indirs[num].in_off, nb, 0, bpp ? *bpp : NULL);
  679. bap[indirs[num].in_off] = nb;
  680. if (allocib == NULL && unwindidx < 0)
  681. unwindidx = i - 1;
  682. /*
  683. * If required, write synchronously, otherwise use delayed
  684. * write.
  685. */
  686. if (flags & B_SYNC)
  687. bwrite(bp);
  688. else
  689. bdwrite(bp);
  690. return (0);
  691. }
  692. brelse(bp);
  693. if (bpp != NULL) {
  694. if (flags & B_CLRBUF) {
  695. error = bread(vp, lbn, (int)fs->fs_bsize, &nbp);
  696. if (error) {
  697. brelse(nbp);
  698. goto fail;
  699. }
  700. } else {
  701. nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
  702. nbp->b_blkno = fsbtodb(fs, nb);
  703. clrbuf(nbp);
  704. }
  705. *bpp = nbp;
  706. }
  707. return (0);
  708. fail:
  709. /*
  710. * If we have failed to allocate any blocks, simply return the error.
  711. * This is the usual case and avoids the need to fsync the file.
  712. */
  713. if (allocblk == allociblk && allocib == NULL && unwindidx == -1)
  714. return (error);
  715. /*
  716. * If we have failed part way through block allocation, we have to
  717. * deallocate any indirect blocks that we have allocated. We have to
  718. * fsync the file before we start to get rid of all of its
  719. * dependencies so that we do not leave them dangling. We have to sync
  720. * it at the end so that the softdep code does not find any untracked
  721. * changes. Although this is really slow, running out of disk space is
  722. * not expected to be a common occurrence. The error return from fsync
  723. * is ignored as we already have an error to return to the user.
  724. */
  725. VOP_FSYNC(vp, p->p_ucred, MNT_WAIT, p);
  726. if (unwindidx >= 0) {
  727. /*
  728. * First write out any buffers we've created to resolve their
  729. * softdeps. This must be done in reverse order of creation so
  730. * that we resolve the dependencies in one pass.
  731. * Write the cylinder group buffers for these buffers too.
  732. */
  733. for (i = num; i >= unwindidx; i--) {
  734. if (i == 0)
  735. break;
  736. bp = getblk(vp, indirs[i].in_lbn, (int) fs->fs_bsize,
  737. 0, 0);
  738. if (bp->b_flags & B_DELWRI) {
  739. nb = fsbtodb(fs, cgtod(fs, dtog(fs,
  740. dbtofsb(fs, bp->b_blkno))));
  741. bwrite(bp);
  742. bp = getblk(ip->i_devvp, nb,
  743. (int) fs->fs_cgsize, 0, 0);
  744. if (bp->b_flags & B_DELWRI)
  745. bwrite(bp);
  746. else {
  747. bp->b_flags |= B_INVAL;
  748. brelse(bp);
  749. }
  750. } else {
  751. bp->b_flags |= B_INVAL;
  752. brelse(bp);
  753. }
  754. }
  755. if (DOINGSOFTDEP(vp) && unwindidx == 0) {
  756. ip->i_flag |= IN_CHANGE | IN_UPDATE;
  757. ffs_update(ip, 1);
  758. }
  759. /*
  760. * Now that any dependencies that we created have been
  761. * resolved, we can undo the partial allocation.
  762. */
  763. if (unwindidx == 0) {
  764. *allocib = 0;
  765. ip->i_flag |= IN_CHANGE | IN_UPDATE;
  766. if (DOINGSOFTDEP(vp))
  767. ffs_update(ip, 1);
  768. } else {
  769. r = bread(vp, indirs[unwindidx].in_lbn,
  770. (int)fs->fs_bsize, &bp);
  771. if (r)
  772. panic("ffs2_balloc: unwind failed");
  773. bap = (int64_t *) bp->b_data;
  774. bap[indirs[unwindidx].in_off] = 0;
  775. bwrite(bp);
  776. }
  777. for (i = unwindidx + 1; i <= num; i++) {
  778. bp = getblk(vp, indirs[i].in_lbn, (int)fs->fs_bsize, 0,
  779. 0);
  780. bp->b_flags |= B_INVAL;
  781. brelse(bp);
  782. }
  783. }
  784. for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
  785. ffs_blkfree(ip, *blkp, fs->fs_bsize);
  786. deallocated += fs->fs_bsize;
  787. }
  788. if (deallocated) {
  789. /*
  790. * Restore user's disk quota because allocation failed.
  791. */
  792. (void) ufs_quota_free_blocks(ip, btodb(deallocated), cred);
  793. ip->i_ffs2_blocks -= btodb(deallocated);
  794. ip->i_flag |= IN_CHANGE | IN_UPDATE;
  795. }
  796. VOP_FSYNC(vp, p->p_ucred, MNT_WAIT, p);
  797. return (error);
  798. }
  799. #endif /* FFS2 */
  800. /*
  801. * Balloc defines the structure of file system storage by allocating the
  802. * physical blocks given the inode and the logical block number in a file.
  803. */
  804. int
  805. ffs_balloc(struct inode *ip, off_t off, int size, struct ucred *cred,
  806. int flags, struct buf **bpp)
  807. {
  808. #ifdef FFS2
  809. if (ip->i_fs->fs_magic == FS_UFS2_MAGIC)
  810. return (ffs2_balloc(ip, off, size, cred, flags, bpp));
  811. else
  812. #endif
  813. return (ffs1_balloc(ip, off, size, cred, flags, bpp));
  814. }