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