00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <types.h>
00037 #include <kern/errno.h>
00038 #include <lib.h>
00039 #include <array.h>
00040 #include <bitmap.h>
00041 #include <uio.h>
00042 #include <vfs.h>
00043 #include <device.h>
00044 #include <sfs.h>
00045
00046
00047 #define SFS_FS_BITMAPSIZE(sfs) SFS_BITMAPSIZE((sfs)->sfs_super.sp_nblocks)
00048 #define SFS_FS_BITBLOCKS(sfs) SFS_BITBLOCKS((sfs)->sfs_super.sp_nblocks)
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 static
00069 int
00070 sfs_mapio(struct sfs_fs *sfs, enum uio_rw rw)
00071 {
00072 uint32_t j, mapsize;
00073 char *bitdata;
00074 int result;
00075
00076
00077 mapsize = SFS_FS_BITBLOCKS(sfs);
00078
00079
00080 bitdata = bitmap_getdata(sfs->sfs_freemap);
00081
00082
00083 for (j=0; j<mapsize; j++) {
00084
00085
00086 void *ptr = bitdata + j*SFS_BLOCKSIZE;
00087
00088
00089 if (rw == UIO_READ) {
00090 result = sfs_rblock(sfs, ptr, SFS_MAP_LOCATION+j);
00091 }
00092 else {
00093 result = sfs_wblock(sfs, ptr, SFS_MAP_LOCATION+j);
00094 }
00095
00096
00097 if (result) {
00098 return result;
00099 }
00100 }
00101 return 0;
00102 }
00103
00104
00105
00106
00107
00108
00109 static
00110 int
00111 sfs_sync(struct fs *fs)
00112 {
00113 struct sfs_fs *sfs;
00114 unsigned i, num;
00115 int result;
00116
00117 vfs_biglock_acquire();
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 sfs = fs->fs_data;
00150
00151
00152 num = vnodearray_num(sfs->sfs_vnodes);
00153 for (i=0; i<num; i++) {
00154 struct vnode *v = vnodearray_get(sfs->sfs_vnodes, i);
00155 VOP_FSYNC(v);
00156 }
00157
00158
00159 if (sfs->sfs_freemapdirty) {
00160 result = sfs_mapio(sfs, UIO_WRITE);
00161 if (result) {
00162 vfs_biglock_release();
00163 return result;
00164 }
00165 sfs->sfs_freemapdirty = false;
00166 }
00167
00168
00169 if (sfs->sfs_superdirty) {
00170 result = sfs_wblock(sfs, &sfs->sfs_super, SFS_SB_LOCATION);
00171 if (result) {
00172 vfs_biglock_release();
00173 return result;
00174 }
00175 sfs->sfs_superdirty = false;
00176 }
00177
00178 vfs_biglock_release();
00179 return 0;
00180 }
00181
00182
00183
00184
00185
00186
00187 static
00188 const char *
00189 sfs_getvolname(struct fs *fs)
00190 {
00191 struct sfs_fs *sfs = fs->fs_data;
00192 const char *ret;
00193
00194 vfs_biglock_acquire();
00195 ret = sfs->sfs_super.sp_volname;
00196 vfs_biglock_release();
00197
00198 return ret;
00199 }
00200
00201
00202
00203
00204
00205
00206 static
00207 int
00208 sfs_unmount(struct fs *fs)
00209 {
00210 struct sfs_fs *sfs = fs->fs_data;
00211
00212 vfs_biglock_acquire();
00213
00214
00215 if (vnodearray_num(sfs->sfs_vnodes) > 0) {
00216 vfs_biglock_release();
00217 return EBUSY;
00218 }
00219
00220
00221 KASSERT(sfs->sfs_superdirty == false);
00222 KASSERT(sfs->sfs_freemapdirty == false);
00223
00224
00225 vnodearray_destroy(sfs->sfs_vnodes);
00226 bitmap_destroy(sfs->sfs_freemap);
00227
00228
00229 (void)sfs->sfs_device;
00230
00231
00232 kfree(sfs);
00233
00234
00235 vfs_biglock_release();
00236 return 0;
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 static
00254 int
00255 sfs_domount(void *options, struct device *dev, struct fs **ret)
00256 {
00257 int result;
00258 struct sfs_fs *sfs;
00259
00260 vfs_biglock_acquire();
00261
00262
00263 (void)options;
00264
00265
00266
00267
00268 KASSERT(sizeof(struct sfs_super)==SFS_BLOCKSIZE);
00269 KASSERT(sizeof(struct sfs_inode)==SFS_BLOCKSIZE);
00270 KASSERT(SFS_BLOCKSIZE % sizeof(struct sfs_dir) == 0);
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 if (dev->d_blocksize != SFS_BLOCKSIZE) {
00281 vfs_biglock_release();
00282 return ENXIO;
00283 }
00284
00285
00286 sfs = kmalloc(sizeof(struct sfs_fs));
00287 if (sfs==NULL) {
00288 vfs_biglock_release();
00289 return ENOMEM;
00290 }
00291
00292
00293 sfs->sfs_vnodes = vnodearray_create();
00294 if (sfs->sfs_vnodes == NULL) {
00295 kfree(sfs);
00296 vfs_biglock_release();
00297 return ENOMEM;
00298 }
00299
00300
00301 sfs->sfs_device = dev;
00302
00303
00304 result = sfs_rblock(sfs, &sfs->sfs_super, SFS_SB_LOCATION);
00305 if (result) {
00306 vnodearray_destroy(sfs->sfs_vnodes);
00307 kfree(sfs);
00308 vfs_biglock_release();
00309 return result;
00310 }
00311
00312
00313
00314 if (sfs->sfs_super.sp_magic != SFS_MAGIC) {
00315 kprintf("sfs: Wrong magic number in superblock "
00316 "(0x%x, should be 0x%x)\n",
00317 sfs->sfs_super.sp_magic,
00318 SFS_MAGIC);
00319 vnodearray_destroy(sfs->sfs_vnodes);
00320 kfree(sfs);
00321 vfs_biglock_release();
00322 return EINVAL;
00323 }
00324
00325 if (sfs->sfs_super.sp_nblocks > dev->d_blocks) {
00326 kprintf("sfs: warning - fs has %u blocks, device has %u\n",
00327 sfs->sfs_super.sp_nblocks, dev->d_blocks);
00328 }
00329
00330
00331 sfs->sfs_super.sp_volname[sizeof(sfs->sfs_super.sp_volname)-1] = 0;
00332
00333
00334 sfs->sfs_freemap = bitmap_create(SFS_FS_BITMAPSIZE(sfs));
00335 if (sfs->sfs_freemap == NULL) {
00336 vnodearray_destroy(sfs->sfs_vnodes);
00337 kfree(sfs);
00338 vfs_biglock_release();
00339 return ENOMEM;
00340 }
00341 result = sfs_mapio(sfs, UIO_READ);
00342 if (result) {
00343 bitmap_destroy(sfs->sfs_freemap);
00344 vnodearray_destroy(sfs->sfs_vnodes);
00345 kfree(sfs);
00346 vfs_biglock_release();
00347 return result;
00348 }
00349
00350
00351 sfs->sfs_absfs.fs_sync = sfs_sync;
00352 sfs->sfs_absfs.fs_getvolname = sfs_getvolname;
00353 sfs->sfs_absfs.fs_getroot = sfs_getroot;
00354 sfs->sfs_absfs.fs_unmount = sfs_unmount;
00355 sfs->sfs_absfs.fs_data = sfs;
00356
00357
00358 sfs->sfs_superdirty = false;
00359 sfs->sfs_freemapdirty = false;
00360
00361
00362 *ret = &sfs->sfs_absfs;
00363
00364 vfs_biglock_release();
00365 return 0;
00366 }
00367
00368
00369
00370
00371
00372 int
00373 sfs_mount(const char *device)
00374 {
00375 return vfs_mount(device, NULL, sfs_domount);
00376 }