vm: fix potential null deref
[minix.git] / servers / ext2 / misc.c
blob430a1f6022e62ace40b969995e600b2ff51508de
1 /* Created (MFS based):
2 * February 2010 (Evgeniy Ivanov)
3 */
5 #include "fs.h"
6 #include <assert.h>
7 #include <minix/vfsif.h>
8 #include <minix/bdev.h>
9 #include "inode.h"
10 #include "super.h"
12 /*===========================================================================*
13 * fs_sync *
14 *===========================================================================*/
15 int fs_sync()
17 /* Perform the sync() system call. Flush all the tables.
18 * The order in which the various tables are flushed is critical. The
19 * blocks must be flushed last, since rw_inode() leaves its results in
20 * the block cache.
22 struct inode *rip;
24 assert(lmfs_nr_bufs() > 0);
26 if (superblock->s_rd_only)
27 return(OK); /* nothing to sync */
29 /* Write all the dirty inodes to the disk. */
30 for(rip = &inode[0]; rip < &inode[NR_INODES]; rip++)
31 if(rip->i_count > 0 && rip->i_dirt == IN_DIRTY) rw_inode(rip, WRITING);
33 lmfs_flushall();
35 if (superblock->s_dev != NO_DEV) {
36 superblock->s_wtime = clock_time();
37 write_super(superblock);
40 return(OK); /* sync() can't fail */
44 /*===========================================================================*
45 * fs_flush *
46 *===========================================================================*/
47 int fs_flush()
49 /* Flush the blocks of a device from the cache after writing any dirty blocks
50 * to disk.
52 dev_t dev = (dev_t) fs_m_in.REQ_DEV;
54 if(dev == fs_dev) return(EBUSY);
56 lmfs_flushall();
57 lmfs_invalidate(dev);
59 return(OK);
62 /*===========================================================================*
63 * fs_new_driver *
64 *===========================================================================*/
65 int fs_new_driver(void)
67 /* Set a new driver endpoint for this device. */
68 dev_t dev;
69 cp_grant_id_t label_gid;
70 size_t label_len;
71 char label[sizeof(fs_dev_label)];
72 int r;
74 dev = (dev_t) fs_m_in.REQ_DEV;
75 label_gid = (cp_grant_id_t) fs_m_in.REQ_GRANT;
76 label_len = (size_t) fs_m_in.REQ_PATH_LEN;
78 if (label_len > sizeof(label))
79 return(EINVAL);
81 r = sys_safecopyfrom(fs_m_in.m_source, label_gid, (vir_bytes) 0,
82 (vir_bytes) label, label_len);
84 if (r != OK) {
85 printf("ext2: fs_new_driver safecopyfrom failed (%d)\n", r);
86 return(EINVAL);
89 bdev_driver(dev, label);
91 return(OK);