Whamcloud - gitweb
212dd16fbe44f505e3b825b7f3049ee7b6ddb98e
[tools/e2fsprogs.git] / lib / ext2fs / mkjournal.c
1 /*
2  * mkjournal.c --- make a journal for a filesystem
3  *
4  * Copyright (C) 2000 Theodore Ts'o.
5  * 
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #if HAVE_ERRNO_H
18 #include <errno.h>
19 #endif
20 #include <fcntl.h>
21 #include <time.h>
22 #if HAVE_SYS_STAT_H
23 #include <sys/stat.h>
24 #endif
25 #if HAVE_SYS_TYPES_H
26 #include <sys/types.h>
27 #endif
28 #if HAVE_NETINET_IN_H
29 #include <netinet/in.h>
30 #endif
31
32 #if EXT2_FLAT_INCLUDES
33 #include "ext2_fs.h"
34 #else
35 #include <linux/ext2_fs.h>
36 #endif
37
38 #include "e2p/e2p.h"
39 #include "ext2fs.h"
40 #include "jfs_user.h"
41
42 /*
43  * This function automatically sets up the journal superblock and
44  * returns it as an allocated block.
45  */
46 errcode_t ext2fs_create_journal_superblock(ext2_filsys fs,
47                                            __u32 size, int flags,
48                                            char  **ret_jsb)
49 {
50         errcode_t               retval;
51         journal_superblock_t    *jsb;
52
53         if (size < 1024)
54                 return EXT2_ET_JOURNAL_TOO_SMALL;
55
56         if ((retval = ext2fs_get_mem(fs->blocksize, (void **) &jsb)))
57                 return retval;
58
59         memset (jsb, 0, fs->blocksize);
60
61         jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
62         if (flags & EXT2_MKJOURNAL_V1_SUPER)
63                 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V1);
64         else
65                 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
66         jsb->s_blocksize = htonl(fs->blocksize);
67         jsb->s_maxlen = htonl(size);
68         jsb->s_nr_users = htonl(1);
69         jsb->s_first = htonl(1);
70         jsb->s_sequence = htonl(1);
71         memcpy(jsb->s_uuid, fs->super->s_uuid, sizeof(fs->super->s_uuid));
72         /*
73          * If we're creating an external journal device, we need to
74          * adjust these fields.
75          */
76         if (fs->super->s_feature_incompat &
77             EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
78                 jsb->s_nr_users = 0;
79                 jsb->s_first = htonl(1);
80         }
81
82         *ret_jsb = (char *) jsb;
83         return 0;
84 }
85
86 /*
87  * This function writes a journal using POSIX routines.  It is used
88  * for creating external journals and creating journals on live
89  * filesystems.
90  */
91 static errcode_t write_journal_file(ext2_filsys fs, char *filename,
92                                     blk_t size, int flags)
93 {
94         errcode_t       retval;
95         char            *buf = 0;
96         int             i, fd, ret_size;
97
98         if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf)))
99                 return retval;
100
101         /* Open the device or journal file */
102         if ((fd = open(filename, O_WRONLY)) < 0) {
103                 retval = errno;
104                 goto errout;
105         }
106
107         /* Write the superblock out */
108         retval = EXT2_ET_SHORT_WRITE;
109         ret_size = write(fd, buf, fs->blocksize);
110         if (ret_size < 0) {
111                 retval = errno;
112                 goto errout;
113         }
114         if (ret_size != fs->blocksize)
115                 goto errout;
116         memset(buf, 0, fs->blocksize);
117
118         for (i = 1; i < size; i++) {
119                 ret_size = write(fd, buf, fs->blocksize);
120                 if (ret_size < 0) {
121                         retval = errno;
122                         goto errout;
123                 }
124                 if (ret_size != fs->blocksize)
125                         goto errout;
126         }
127         close(fd);
128
129         retval = 0;
130 errout:
131         ext2fs_free_mem((void **) &buf);
132         return retval;
133 }
134
135 /*
136  * Helper function for creating the journal using direct I/O routines
137  */
138 struct mkjournal_struct {
139         int             num_blocks;
140         int             newblocks;
141         char            *buf;
142         errcode_t       err;
143 };
144
145 static int mkjournal_proc(ext2_filsys           fs,
146                            blk_t                *blocknr,
147                            e2_blkcnt_t          blockcnt,
148                            blk_t                ref_block,
149                            int                  ref_offset,
150                            void                 *priv_data)
151 {
152         struct mkjournal_struct *es = (struct mkjournal_struct *) priv_data;
153         blk_t   new_blk;
154         static blk_t    last_blk = 0;
155         errcode_t       retval;
156         int             group;
157         
158         if (*blocknr) {
159                 last_blk = *blocknr;
160                 return 0;
161         }
162         retval = ext2fs_new_block(fs, last_blk, 0, &new_blk);
163         if (retval) {
164                 es->err = retval;
165                 return BLOCK_ABORT;
166         }
167         if (blockcnt > 0)
168                 es->num_blocks--;
169
170         es->newblocks++;
171         retval = io_channel_write_blk(fs->io, new_blk, 1, es->buf);
172
173         if (blockcnt == 0)
174                 memset(es->buf, 0, fs->blocksize);
175
176         if (retval) {
177                 es->err = retval;
178                 return BLOCK_ABORT;
179         }
180         *blocknr = new_blk;
181         ext2fs_mark_block_bitmap(fs->block_map, new_blk);
182         ext2fs_mark_bb_dirty(fs);
183         group = ext2fs_group_of_blk(fs, new_blk);
184         fs->group_desc[group].bg_free_blocks_count--;
185         fs->super->s_free_blocks_count--;
186         ext2fs_mark_super_dirty(fs);
187
188         if (es->num_blocks == 0)
189                 return (BLOCK_CHANGED | BLOCK_ABORT);
190         else
191                 return BLOCK_CHANGED;
192         
193 }
194
195 /*
196  * This function creates a journal using direct I/O routines.
197  */
198 static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
199                                      blk_t size, int flags)
200 {
201         char                    *buf;
202         errcode_t               retval;
203         struct ext2_inode       inode;
204         struct mkjournal_struct es;
205
206         if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf)))
207                 return retval;
208         
209         if ((retval = ext2fs_read_bitmaps(fs)))
210                 return retval;
211
212         if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
213                 return retval;
214
215         if (inode.i_blocks > 0)
216                 return EEXIST;
217
218         es.num_blocks = size;
219         es.newblocks = 0;
220         es.buf = buf;
221         es.err = 0;
222
223         retval = ext2fs_block_iterate2(fs, journal_ino, BLOCK_FLAG_APPEND,
224                                        0, mkjournal_proc, &es);
225         if (es.err) {
226                 retval = es.err;
227                 goto errout;
228         }
229
230         if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
231                 goto errout;
232
233         inode.i_size += fs->blocksize * size;
234         inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
235         inode.i_mtime = inode.i_ctime = time(0);
236         inode.i_links_count = 1;
237         inode.i_mode = LINUX_S_IFREG | 0600;
238
239         if ((retval = ext2fs_write_inode(fs, journal_ino, &inode)))
240                 goto errout;
241         retval = 0;
242
243 errout:
244         ext2fs_free_mem((void **) &buf);
245         return retval;
246 }
247
248 /*
249  * This function adds a journal device to a filesystem
250  */
251 errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
252 {
253         struct stat     st;
254         errcode_t       retval;
255         char            buf[1024];
256         journal_superblock_t    *jsb;
257         int             i;
258         __u32           nr_users;
259
260         /* Make sure the device exists and is a block device */
261         if (stat(journal_dev->device_name, &st) < 0)
262                 return errno;
263         
264         if (!S_ISBLK(st.st_mode))
265                 return EXT2_ET_JOURNAL_NOT_BLOCK; /* Must be a block device */
266
267         /* Get the journal superblock */
268         if ((retval = io_channel_read_blk(journal_dev->io, 1, -1024, buf)))
269                 return retval;
270
271         jsb = (journal_superblock_t *) buf;
272         if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
273             (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2)))
274                 return EXT2_ET_NO_JOURNAL_SB;
275
276         if (ntohl(jsb->s_blocksize) != fs->blocksize)
277                 return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
278
279         /* Check and see if this filesystem has already been added */
280         nr_users = ntohl(jsb->s_nr_users);
281         for (i=0; i < nr_users; i++) {
282                 if (memcmp(fs->super->s_uuid,
283                            &jsb->s_users[i*16], 16) == 0)
284                         break;
285         }
286         if (i >= nr_users) {
287                 memcpy(&jsb->s_users[nr_users*16],
288                        fs->super->s_uuid, 16);
289                 jsb->s_nr_users = htonl(nr_users+1);
290         }
291
292         /* Writeback the journal superblock */
293         if ((retval = io_channel_write_blk(journal_dev->io, 1, -1024, buf)))
294                 return retval;
295         
296         fs->super->s_journal_inum = 0;
297         fs->super->s_journal_dev = st.st_rdev;
298         memcpy(fs->super->s_journal_uuid, jsb->s_uuid,
299                sizeof(fs->super->s_journal_uuid));
300         fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
301         ext2fs_mark_super_dirty(fs);
302         return 0;
303 }
304
305 /*
306  * This function adds a journal inode to a filesystem, using either
307  * POSIX routines if the filesystem is mounted, or using direct I/O
308  * functions if it is not.
309  */
310 errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t size, int flags)
311 {
312         errcode_t               retval;
313         ext2_ino_t              journal_ino;
314         struct stat             st;
315         char                    jfile[1024];
316         int                     fd, mount_flags;
317
318         if ((retval = ext2fs_check_mount_point(fs->device_name, &mount_flags,
319                                                jfile, sizeof(jfile)-10)))
320                 return retval;
321
322         if (mount_flags & EXT2_MF_MOUNTED) {
323                 strcat(jfile, "/.journal");
324
325                 /* Create the journal file */
326                 if ((fd = open(jfile, O_CREAT|O_WRONLY, 0600)) < 0)
327                         return errno;
328                 close(fd);
329
330                 if ((retval = write_journal_file(fs, jfile, size, flags)))
331                         return retval;
332
333                 /* Get inode number of the journal file */
334                 if (stat(jfile, &st) < 0)
335                         return errno;
336
337                 if ((retval = fsetflags(jfile,
338                                         EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL)))
339                         return retval;
340                 
341                 journal_ino = st.st_ino;
342         } else {
343                 journal_ino = EXT2_JOURNAL_INO;
344                 if ((retval = write_journal_inode(fs, journal_ino,
345                                                   size, flags)))
346                         return retval;
347         }
348         
349         fs->super->s_journal_inum = journal_ino;
350         fs->super->s_journal_dev = 0;
351         memset(fs->super->s_journal_uuid, 0,
352                sizeof(fs->super->s_journal_uuid));
353         fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
354
355         ext2fs_mark_super_dirty(fs);
356         return 0;
357 }
358
359 #ifdef DEBUG
360 main(int argc, char **argv)
361 {
362         errcode_t       retval;
363         char            *device_name;
364         ext2_filsys     fs;
365
366         if (argc < 2) {
367                 fprintf(stderr, "Usage: %s filesystem\n", argv[0]);
368                 exit(1);
369         }
370         device_name = argv[1];
371         
372         retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
373                               unix_io_manager, &fs);
374         if (retval) {
375                 com_err(argv[0], retval, "while opening %s", device_name);
376                 exit(1);
377         }
378
379         retval = ext2fs_add_journal_inode(fs, 1024);
380         if (retval) {
381                 com_err(argv[0], retval, "while adding journal to %s",
382                         device_name);
383                 exit(1);
384         }
385         retval = ext2fs_flush(fs);
386         if (retval) {
387                 printf("Warning, had trouble writing out superblocks.\n");
388         }
389         ext2fs_close(fs);
390         exit(0);
391         
392 }
393 #endif