blob: 67522f2bcee803c7210c139fe215efecdc82a1ef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33#include "xfs.h"
34#include "xfs_macros.h"
35#include "xfs_types.h"
36#include "xfs_inum.h"
37#include "xfs_log.h"
38#include "xfs_trans.h"
39#include "xfs_sb.h"
40#include "xfs_dir.h"
41#include "xfs_dmapi.h"
42#include "xfs_mount.h"
43#include "xfs_ag.h"
44#include "xfs_alloc_btree.h"
45#include "xfs_bmap_btree.h"
46#include "xfs_ialloc_btree.h"
47#include "xfs_btree.h"
48#include "xfs_error.h"
49#include "xfs_alloc.h"
50#include "xfs_ialloc.h"
51#include "xfs_fsops.h"
52#include "xfs_itable.h"
53#include "xfs_rw.h"
54#include "xfs_refcache.h"
55#include "xfs_trans_space.h"
56#include "xfs_rtalloc.h"
57#include "xfs_dir2.h"
58#include "xfs_attr_sf.h"
59#include "xfs_dir_sf.h"
60#include "xfs_dir2_sf.h"
61#include "xfs_dinode.h"
62#include "xfs_inode.h"
63#include "xfs_inode_item.h"
64
65/*
66 * File system operations
67 */
68
69int
70xfs_fs_geometry(
71 xfs_mount_t *mp,
72 xfs_fsop_geom_t *geo,
73 int new_version)
74{
75 geo->blocksize = mp->m_sb.sb_blocksize;
76 geo->rtextsize = mp->m_sb.sb_rextsize;
77 geo->agblocks = mp->m_sb.sb_agblocks;
78 geo->agcount = mp->m_sb.sb_agcount;
79 geo->logblocks = mp->m_sb.sb_logblocks;
80 geo->sectsize = mp->m_sb.sb_sectsize;
81 geo->inodesize = mp->m_sb.sb_inodesize;
82 geo->imaxpct = mp->m_sb.sb_imax_pct;
83 geo->datablocks = mp->m_sb.sb_dblocks;
84 geo->rtblocks = mp->m_sb.sb_rblocks;
85 geo->rtextents = mp->m_sb.sb_rextents;
86 geo->logstart = mp->m_sb.sb_logstart;
87 ASSERT(sizeof(geo->uuid)==sizeof(mp->m_sb.sb_uuid));
88 memcpy(geo->uuid, &mp->m_sb.sb_uuid, sizeof(mp->m_sb.sb_uuid));
89 if (new_version >= 2) {
90 geo->sunit = mp->m_sb.sb_unit;
91 geo->swidth = mp->m_sb.sb_width;
92 }
93 if (new_version >= 3) {
94 geo->version = XFS_FSOP_GEOM_VERSION;
95 geo->flags =
96 (XFS_SB_VERSION_HASATTR(&mp->m_sb) ?
97 XFS_FSOP_GEOM_FLAGS_ATTR : 0) |
98 (XFS_SB_VERSION_HASNLINK(&mp->m_sb) ?
99 XFS_FSOP_GEOM_FLAGS_NLINK : 0) |
100 (XFS_SB_VERSION_HASQUOTA(&mp->m_sb) ?
101 XFS_FSOP_GEOM_FLAGS_QUOTA : 0) |
102 (XFS_SB_VERSION_HASALIGN(&mp->m_sb) ?
103 XFS_FSOP_GEOM_FLAGS_IALIGN : 0) |
104 (XFS_SB_VERSION_HASDALIGN(&mp->m_sb) ?
105 XFS_FSOP_GEOM_FLAGS_DALIGN : 0) |
106 (XFS_SB_VERSION_HASSHARED(&mp->m_sb) ?
107 XFS_FSOP_GEOM_FLAGS_SHARED : 0) |
108 (XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb) ?
109 XFS_FSOP_GEOM_FLAGS_EXTFLG : 0) |
110 (XFS_SB_VERSION_HASDIRV2(&mp->m_sb) ?
111 XFS_FSOP_GEOM_FLAGS_DIRV2 : 0) |
112 (XFS_SB_VERSION_HASSECTOR(&mp->m_sb) ?
Nathan Scottd8cc8902005-11-02 10:34:53 +1100113 XFS_FSOP_GEOM_FLAGS_SECTOR : 0) |
114 (XFS_SB_VERSION_HASATTR2(&mp->m_sb) ?
115 XFS_FSOP_GEOM_FLAGS_ATTR2 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 geo->logsectsize = XFS_SB_VERSION_HASSECTOR(&mp->m_sb) ?
117 mp->m_sb.sb_logsectsize : BBSIZE;
118 geo->rtsectsize = mp->m_sb.sb_blocksize;
119 geo->dirblocksize = mp->m_dirblksize;
120 }
121 if (new_version >= 4) {
122 geo->flags |=
123 (XFS_SB_VERSION_HASLOGV2(&mp->m_sb) ?
124 XFS_FSOP_GEOM_FLAGS_LOGV2 : 0);
125 geo->logsunit = mp->m_sb.sb_logsunit;
126 }
127 return 0;
128}
129
130static int
131xfs_growfs_data_private(
132 xfs_mount_t *mp, /* mount point for filesystem */
133 xfs_growfs_data_t *in) /* growfs data input struct */
134{
135 xfs_agf_t *agf;
136 xfs_agi_t *agi;
137 xfs_agnumber_t agno;
138 xfs_extlen_t agsize;
139 xfs_extlen_t tmpsize;
140 xfs_alloc_rec_t *arec;
141 xfs_btree_sblock_t *block;
142 xfs_buf_t *bp;
143 int bucket;
144 int dpct;
145 int error;
146 xfs_agnumber_t nagcount;
147 xfs_agnumber_t nagimax = 0;
148 xfs_rfsblock_t nb, nb_mod;
149 xfs_rfsblock_t new;
150 xfs_rfsblock_t nfree;
151 xfs_agnumber_t oagcount;
152 int pct;
153 xfs_sb_t *sbp;
154 xfs_trans_t *tp;
155
156 nb = in->newblocks;
157 pct = in->imaxpct;
158 if (nb < mp->m_sb.sb_dblocks || pct < 0 || pct > 100)
159 return XFS_ERROR(EINVAL);
160 dpct = pct - mp->m_sb.sb_imax_pct;
161 error = xfs_read_buf(mp, mp->m_ddev_targp,
162 XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
163 XFS_FSS_TO_BB(mp, 1), 0, &bp);
164 if (error)
165 return error;
166 ASSERT(bp);
167 xfs_buf_relse(bp);
168
169 new = nb; /* use new as a temporary here */
170 nb_mod = do_div(new, mp->m_sb.sb_agblocks);
171 nagcount = new + (nb_mod != 0);
172 if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) {
173 nagcount--;
174 nb = nagcount * mp->m_sb.sb_agblocks;
175 if (nb < mp->m_sb.sb_dblocks)
176 return XFS_ERROR(EINVAL);
177 }
178 new = nb - mp->m_sb.sb_dblocks;
179 oagcount = mp->m_sb.sb_agcount;
180 if (nagcount > oagcount) {
181 down_write(&mp->m_peraglock);
182 mp->m_perag = kmem_realloc(mp->m_perag,
183 sizeof(xfs_perag_t) * nagcount,
184 sizeof(xfs_perag_t) * oagcount,
185 KM_SLEEP);
186 memset(&mp->m_perag[oagcount], 0,
187 (nagcount - oagcount) * sizeof(xfs_perag_t));
188 mp->m_flags |= XFS_MOUNT_32BITINODES;
189 nagimax = xfs_initialize_perag(mp, nagcount);
190 up_write(&mp->m_peraglock);
191 }
192 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFS);
193 if ((error = xfs_trans_reserve(tp, XFS_GROWFS_SPACE_RES(mp),
194 XFS_GROWDATA_LOG_RES(mp), 0, 0, 0))) {
195 xfs_trans_cancel(tp, 0);
196 return error;
197 }
198
199 nfree = 0;
200 for (agno = nagcount - 1; agno >= oagcount; agno--, new -= agsize) {
201 /*
202 * AG freelist header block
203 */
204 bp = xfs_buf_get(mp->m_ddev_targp,
205 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
206 XFS_FSS_TO_BB(mp, 1), 0);
207 agf = XFS_BUF_TO_AGF(bp);
208 memset(agf, 0, mp->m_sb.sb_sectsize);
209 INT_SET(agf->agf_magicnum, ARCH_CONVERT, XFS_AGF_MAGIC);
210 INT_SET(agf->agf_versionnum, ARCH_CONVERT, XFS_AGF_VERSION);
211 INT_SET(agf->agf_seqno, ARCH_CONVERT, agno);
212 if (agno == nagcount - 1)
213 agsize =
214 nb -
215 (agno * (xfs_rfsblock_t)mp->m_sb.sb_agblocks);
216 else
217 agsize = mp->m_sb.sb_agblocks;
218 INT_SET(agf->agf_length, ARCH_CONVERT, agsize);
219 INT_SET(agf->agf_roots[XFS_BTNUM_BNOi], ARCH_CONVERT,
220 XFS_BNO_BLOCK(mp));
221 INT_SET(agf->agf_roots[XFS_BTNUM_CNTi], ARCH_CONVERT,
222 XFS_CNT_BLOCK(mp));
223 INT_SET(agf->agf_levels[XFS_BTNUM_BNOi], ARCH_CONVERT, 1);
224 INT_SET(agf->agf_levels[XFS_BTNUM_CNTi], ARCH_CONVERT, 1);
225 agf->agf_flfirst = 0;
226 INT_SET(agf->agf_fllast, ARCH_CONVERT, XFS_AGFL_SIZE(mp) - 1);
227 agf->agf_flcount = 0;
228 tmpsize = agsize - XFS_PREALLOC_BLOCKS(mp);
229 INT_SET(agf->agf_freeblks, ARCH_CONVERT, tmpsize);
230 INT_SET(agf->agf_longest, ARCH_CONVERT, tmpsize);
231 error = xfs_bwrite(mp, bp);
232 if (error) {
233 goto error0;
234 }
235 /*
236 * AG inode header block
237 */
238 bp = xfs_buf_get(mp->m_ddev_targp,
239 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
240 XFS_FSS_TO_BB(mp, 1), 0);
241 agi = XFS_BUF_TO_AGI(bp);
242 memset(agi, 0, mp->m_sb.sb_sectsize);
243 INT_SET(agi->agi_magicnum, ARCH_CONVERT, XFS_AGI_MAGIC);
244 INT_SET(agi->agi_versionnum, ARCH_CONVERT, XFS_AGI_VERSION);
245 INT_SET(agi->agi_seqno, ARCH_CONVERT, agno);
246 INT_SET(agi->agi_length, ARCH_CONVERT, agsize);
247 agi->agi_count = 0;
248 INT_SET(agi->agi_root, ARCH_CONVERT, XFS_IBT_BLOCK(mp));
249 INT_SET(agi->agi_level, ARCH_CONVERT, 1);
250 agi->agi_freecount = 0;
251 INT_SET(agi->agi_newino, ARCH_CONVERT, NULLAGINO);
252 INT_SET(agi->agi_dirino, ARCH_CONVERT, NULLAGINO);
253 for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
254 INT_SET(agi->agi_unlinked[bucket], ARCH_CONVERT,
255 NULLAGINO);
256 error = xfs_bwrite(mp, bp);
257 if (error) {
258 goto error0;
259 }
260 /*
261 * BNO btree root block
262 */
263 bp = xfs_buf_get(mp->m_ddev_targp,
264 XFS_AGB_TO_DADDR(mp, agno, XFS_BNO_BLOCK(mp)),
265 BTOBB(mp->m_sb.sb_blocksize), 0);
266 block = XFS_BUF_TO_SBLOCK(bp);
267 memset(block, 0, mp->m_sb.sb_blocksize);
268 INT_SET(block->bb_magic, ARCH_CONVERT, XFS_ABTB_MAGIC);
269 block->bb_level = 0;
270 INT_SET(block->bb_numrecs, ARCH_CONVERT, 1);
271 INT_SET(block->bb_leftsib, ARCH_CONVERT, NULLAGBLOCK);
272 INT_SET(block->bb_rightsib, ARCH_CONVERT, NULLAGBLOCK);
273 arec = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_alloc,
274 block, 1, mp->m_alloc_mxr[0]);
275 INT_SET(arec->ar_startblock, ARCH_CONVERT,
276 XFS_PREALLOC_BLOCKS(mp));
277 INT_SET(arec->ar_blockcount, ARCH_CONVERT,
278 agsize - INT_GET(arec->ar_startblock, ARCH_CONVERT));
279 error = xfs_bwrite(mp, bp);
280 if (error) {
281 goto error0;
282 }
283 /*
284 * CNT btree root block
285 */
286 bp = xfs_buf_get(mp->m_ddev_targp,
287 XFS_AGB_TO_DADDR(mp, agno, XFS_CNT_BLOCK(mp)),
288 BTOBB(mp->m_sb.sb_blocksize), 0);
289 block = XFS_BUF_TO_SBLOCK(bp);
290 memset(block, 0, mp->m_sb.sb_blocksize);
291 INT_SET(block->bb_magic, ARCH_CONVERT, XFS_ABTC_MAGIC);
292 block->bb_level = 0;
293 INT_SET(block->bb_numrecs, ARCH_CONVERT, 1);
294 INT_SET(block->bb_leftsib, ARCH_CONVERT, NULLAGBLOCK);
295 INT_SET(block->bb_rightsib, ARCH_CONVERT, NULLAGBLOCK);
296 arec = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_alloc,
297 block, 1, mp->m_alloc_mxr[0]);
298 INT_SET(arec->ar_startblock, ARCH_CONVERT,
299 XFS_PREALLOC_BLOCKS(mp));
300 INT_SET(arec->ar_blockcount, ARCH_CONVERT,
301 agsize - INT_GET(arec->ar_startblock, ARCH_CONVERT));
302 nfree += INT_GET(arec->ar_blockcount, ARCH_CONVERT);
303 error = xfs_bwrite(mp, bp);
304 if (error) {
305 goto error0;
306 }
307 /*
308 * INO btree root block
309 */
310 bp = xfs_buf_get(mp->m_ddev_targp,
311 XFS_AGB_TO_DADDR(mp, agno, XFS_IBT_BLOCK(mp)),
312 BTOBB(mp->m_sb.sb_blocksize), 0);
313 block = XFS_BUF_TO_SBLOCK(bp);
314 memset(block, 0, mp->m_sb.sb_blocksize);
315 INT_SET(block->bb_magic, ARCH_CONVERT, XFS_IBT_MAGIC);
316 block->bb_level = 0;
317 block->bb_numrecs = 0;
318 INT_SET(block->bb_leftsib, ARCH_CONVERT, NULLAGBLOCK);
319 INT_SET(block->bb_rightsib, ARCH_CONVERT, NULLAGBLOCK);
320 error = xfs_bwrite(mp, bp);
321 if (error) {
322 goto error0;
323 }
324 }
325 xfs_trans_agblocks_delta(tp, nfree);
326 /*
327 * There are new blocks in the old last a.g.
328 */
329 if (new) {
330 /*
331 * Change the agi length.
332 */
333 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
334 if (error) {
335 goto error0;
336 }
337 ASSERT(bp);
338 agi = XFS_BUF_TO_AGI(bp);
339 INT_MOD(agi->agi_length, ARCH_CONVERT, new);
340 ASSERT(nagcount == oagcount ||
341 INT_GET(agi->agi_length, ARCH_CONVERT) ==
342 mp->m_sb.sb_agblocks);
343 xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
344 /*
345 * Change agf length.
346 */
347 error = xfs_alloc_read_agf(mp, tp, agno, 0, &bp);
348 if (error) {
349 goto error0;
350 }
351 ASSERT(bp);
352 agf = XFS_BUF_TO_AGF(bp);
353 INT_MOD(agf->agf_length, ARCH_CONVERT, new);
354 ASSERT(INT_GET(agf->agf_length, ARCH_CONVERT) ==
355 INT_GET(agi->agi_length, ARCH_CONVERT));
356 /*
357 * Free the new space.
358 */
359 error = xfs_free_extent(tp, XFS_AGB_TO_FSB(mp, agno,
360 INT_GET(agf->agf_length, ARCH_CONVERT) - new), new);
361 if (error) {
362 goto error0;
363 }
364 }
365 if (nagcount > oagcount)
366 xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
367 if (nb > mp->m_sb.sb_dblocks)
368 xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS,
369 nb - mp->m_sb.sb_dblocks);
370 if (nfree)
371 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, nfree);
372 if (dpct)
373 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
374 error = xfs_trans_commit(tp, 0, NULL);
375 if (error) {
376 return error;
377 }
378 /* New allocation groups fully initialized, so update mount struct */
379 if (nagimax)
380 mp->m_maxagi = nagimax;
381 if (mp->m_sb.sb_imax_pct) {
382 __uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
383 do_div(icount, 100);
384 mp->m_maxicount = icount << mp->m_sb.sb_inopblog;
385 } else
386 mp->m_maxicount = 0;
387 for (agno = 1; agno < nagcount; agno++) {
388 error = xfs_read_buf(mp, mp->m_ddev_targp,
389 XFS_AGB_TO_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
390 XFS_FSS_TO_BB(mp, 1), 0, &bp);
391 if (error) {
392 xfs_fs_cmn_err(CE_WARN, mp,
393 "error %d reading secondary superblock for ag %d",
394 error, agno);
395 break;
396 }
397 sbp = XFS_BUF_TO_SBP(bp);
398 xfs_xlatesb(sbp, &mp->m_sb, -1, XFS_SB_ALL_BITS);
399 /*
400 * If we get an error writing out the alternate superblocks,
401 * just issue a warning and continue. The real work is
402 * already done and committed.
403 */
404 if (!(error = xfs_bwrite(mp, bp))) {
405 continue;
406 } else {
407 xfs_fs_cmn_err(CE_WARN, mp,
408 "write error %d updating secondary superblock for ag %d",
409 error, agno);
410 break; /* no point in continuing */
411 }
412 }
413 return 0;
414
415 error0:
416 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
417 return error;
418}
419
420static int
421xfs_growfs_log_private(
422 xfs_mount_t *mp, /* mount point for filesystem */
423 xfs_growfs_log_t *in) /* growfs log input struct */
424{
425 xfs_extlen_t nb;
426
427 nb = in->newblocks;
428 if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
429 return XFS_ERROR(EINVAL);
430 if (nb == mp->m_sb.sb_logblocks &&
431 in->isint == (mp->m_sb.sb_logstart != 0))
432 return XFS_ERROR(EINVAL);
433 /*
434 * Moving the log is hard, need new interfaces to sync
435 * the log first, hold off all activity while moving it.
436 * Can have shorter or longer log in the same space,
437 * or transform internal to external log or vice versa.
438 */
439 return XFS_ERROR(ENOSYS);
440}
441
442/*
443 * protected versions of growfs function acquire and release locks on the mount
444 * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
445 * XFS_IOC_FSGROWFSRT
446 */
447
448
449int
450xfs_growfs_data(
451 xfs_mount_t *mp,
452 xfs_growfs_data_t *in)
453{
454 int error;
455 if (!cpsema(&mp->m_growlock))
456 return XFS_ERROR(EWOULDBLOCK);
457 error = xfs_growfs_data_private(mp, in);
458 vsema(&mp->m_growlock);
459 return error;
460}
461
462int
463xfs_growfs_log(
464 xfs_mount_t *mp,
465 xfs_growfs_log_t *in)
466{
467 int error;
468 if (!cpsema(&mp->m_growlock))
469 return XFS_ERROR(EWOULDBLOCK);
470 error = xfs_growfs_log_private(mp, in);
471 vsema(&mp->m_growlock);
472 return error;
473}
474
475/*
476 * exported through ioctl XFS_IOC_FSCOUNTS
477 */
478
479int
480xfs_fs_counts(
481 xfs_mount_t *mp,
482 xfs_fsop_counts_t *cnt)
483{
484 unsigned long s;
485
486 s = XFS_SB_LOCK(mp);
487 cnt->freedata = mp->m_sb.sb_fdblocks;
488 cnt->freertx = mp->m_sb.sb_frextents;
489 cnt->freeino = mp->m_sb.sb_ifree;
490 cnt->allocino = mp->m_sb.sb_icount;
491 XFS_SB_UNLOCK(mp, s);
492 return 0;
493}
494
495/*
496 * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
497 *
498 * xfs_reserve_blocks is called to set m_resblks
499 * in the in-core mount table. The number of unused reserved blocks
500 * is kept in m_resbls_avail.
501 *
502 * Reserve the requested number of blocks if available. Otherwise return
503 * as many as possible to satisfy the request. The actual number
504 * reserved are returned in outval
505 *
506 * A null inval pointer indicates that only the current reserved blocks
507 * available should be returned no settings are changed.
508 */
509
510int
511xfs_reserve_blocks(
512 xfs_mount_t *mp,
513 __uint64_t *inval,
514 xfs_fsop_resblks_t *outval)
515{
516 __int64_t lcounter, delta;
517 __uint64_t request;
518 unsigned long s;
519
520 /* If inval is null, report current values and return */
521
522 if (inval == (__uint64_t *)NULL) {
523 outval->resblks = mp->m_resblks;
524 outval->resblks_avail = mp->m_resblks_avail;
525 return(0);
526 }
527
528 request = *inval;
529 s = XFS_SB_LOCK(mp);
530
531 /*
532 * If our previous reservation was larger than the current value,
533 * then move any unused blocks back to the free pool.
534 */
535
536 if (mp->m_resblks > request) {
537 lcounter = mp->m_resblks_avail - request;
538 if (lcounter > 0) { /* release unused blocks */
539 mp->m_sb.sb_fdblocks += lcounter;
540 mp->m_resblks_avail -= lcounter;
541 }
542 mp->m_resblks = request;
543 } else {
544 delta = request - mp->m_resblks;
545 lcounter = mp->m_sb.sb_fdblocks - delta;
546 if (lcounter < 0) {
547 /* We can't satisfy the request, just get what we can */
548 mp->m_resblks += mp->m_sb.sb_fdblocks;
549 mp->m_resblks_avail += mp->m_sb.sb_fdblocks;
550 mp->m_sb.sb_fdblocks = 0;
551 } else {
552 mp->m_sb.sb_fdblocks = lcounter;
553 mp->m_resblks = request;
554 mp->m_resblks_avail += delta;
555 }
556 }
557
558 outval->resblks = mp->m_resblks;
559 outval->resblks_avail = mp->m_resblks_avail;
560 XFS_SB_UNLOCK(mp, s);
561 return(0);
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564int
565xfs_fs_goingdown(
566 xfs_mount_t *mp,
567 __uint32_t inflags)
568{
569 switch (inflags) {
570 case XFS_FSOP_GOING_FLAGS_DEFAULT: {
571 struct vfs *vfsp = XFS_MTOVFS(mp);
572 struct super_block *sb = freeze_bdev(vfsp->vfs_super->s_bdev);
573
574 if (sb) {
575 xfs_force_shutdown(mp, XFS_FORCE_UMOUNT);
576 thaw_bdev(sb->s_bdev, sb);
577 }
578
579 break;
580 }
581 case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
582 xfs_force_shutdown(mp, XFS_FORCE_UMOUNT);
583 break;
584 case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
585 xfs_force_shutdown(mp, XFS_FORCE_UMOUNT|XFS_LOG_IO_ERROR);
586 break;
587 default:
588 return XFS_ERROR(EINVAL);
589 }
590
591 return 0;
592}