Whamcloud - gitweb
debugfs: make stat command support inline data
[tools/e2fsprogs.git] / lib / ext2fs / inline_data.c
1 /*
2  * inline_data.c --- data in inode
3  *
4  * Copyright (C) 2012 Zheng Liu <wenqing.lz@taobao.com>
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 #include "config.h"
13 #include <stdio.h>
14 #include <time.h>
15
16 #include "ext2_fs.h"
17 #include "ext2_ext_attr.h"
18
19 #include "ext2fs.h"
20 #include "ext2fsP.h"
21
22 struct ext2_inline_data {
23         ext2_filsys fs;
24         ext2_ino_t ino;
25         size_t ea_size; /* the size of inline data in ea area */
26         void *ea_data;
27 };
28
29 static errcode_t ext2fs_inline_data_ea_set(struct ext2_inline_data *data)
30 {
31         struct ext2_xattr_handle *handle;
32         errcode_t retval;
33
34         retval = ext2fs_xattrs_open(data->fs, data->ino, &handle);
35         if (retval)
36                 return retval;
37
38         retval = ext2fs_xattrs_read(handle);
39         if (retval)
40                 goto err;
41
42         retval = ext2fs_xattr_set(handle, "system.data",
43                                   data->ea_data, data->ea_size);
44         if (retval)
45                 goto err;
46
47         retval = ext2fs_xattrs_write(handle);
48
49 err:
50         (void) ext2fs_xattrs_close(&handle);
51         return retval;
52 }
53
54 static errcode_t ext2fs_inline_data_ea_get(struct ext2_inline_data *data)
55 {
56         struct ext2_xattr_handle *handle;
57         errcode_t retval;
58
59         data->ea_size = 0;
60         data->ea_data = 0;
61
62         retval = ext2fs_xattrs_open(data->fs, data->ino, &handle);
63         if (retval)
64                 return retval;
65
66         retval = ext2fs_xattrs_read(handle);
67         if (retval)
68                 goto err;
69
70         retval = ext2fs_xattr_get(handle, "system.data",
71                                   (void **)&data->ea_data, &data->ea_size);
72         if (retval)
73                 goto err;
74
75 err:
76         (void) ext2fs_xattrs_close(&handle);
77         return retval;
78 }
79
80 errcode_t ext2fs_inline_data_size(ext2_filsys fs, ext2_ino_t ino, size_t *size)
81 {
82         struct ext2_inode inode;
83         struct ext2_inline_data data;
84         errcode_t retval;
85
86         retval = ext2fs_read_inode(fs, ino, &inode);
87         if (retval)
88                 return retval;
89
90         if (!(inode.i_flags & EXT4_INLINE_DATA_FL))
91                 return EXT2_ET_NO_INLINE_DATA;
92
93         data.fs = fs;
94         data.ino = ino;
95         retval = ext2fs_inline_data_ea_get(&data);
96         if (retval)
97                 return retval;
98
99         *size = EXT4_MIN_INLINE_DATA_SIZE + data.ea_size;
100         return ext2fs_free_mem(&data.ea_data);
101 }
102
103 int ext2fs_inline_data_dir_iterate(ext2_filsys fs, ext2_ino_t ino,
104                                    void *priv_data)
105 {
106         struct dir_context *ctx;
107         struct ext2_inode inode;
108         struct ext2_dir_entry dirent;
109         struct ext2_inline_data data;
110         int ret = BLOCK_ABORT;
111         e2_blkcnt_t blockcnt = 0;
112
113         ctx = (struct dir_context *)priv_data;
114
115         ctx->errcode = ext2fs_read_inode(fs, ino, &inode);
116         if (ctx->errcode)
117                 goto out;
118
119         if (!(inode.i_flags & EXT4_INLINE_DATA_FL)) {
120                 ctx->errcode = EXT2_ET_NO_INLINE_DATA;
121                 goto out;
122         }
123
124         if (!LINUX_S_ISDIR(inode.i_mode)) {
125                 ctx->errcode = EXT2_ET_NO_DIRECTORY;
126                 goto out;
127         }
128         ret = 0;
129
130         /* we first check '.' and '..' dir */
131         dirent.inode = ino;
132         dirent.name_len = 1;
133         ext2fs_set_rec_len(fs, EXT2_DIR_REC_LEN(2), &dirent);
134         dirent.name[0] = '.';
135         dirent.name[1] = '\0';
136         ctx->buf = (char *)&dirent;
137         ext2fs_get_rec_len(fs, &dirent, &ctx->buflen);
138         ret |= ext2fs_process_dir_block(fs, 0, blockcnt++, 0, 0, priv_data);
139         if (ret & BLOCK_ABORT)
140                 goto out;
141
142         dirent.inode = ext2fs_le32_to_cpu(inode.i_block[0]);
143         dirent.name_len = 2;
144         ext2fs_set_rec_len(fs, EXT2_DIR_REC_LEN(3), &dirent);
145         dirent.name[0] = '.';
146         dirent.name[1] = '.';
147         dirent.name[2] = '\0';
148         ctx->buf = (char *)&dirent;
149         ext2fs_get_rec_len(fs, &dirent, &ctx->buflen);
150         ret |= ext2fs_process_dir_block(fs, 0, blockcnt++, 0, 0, priv_data);
151         if (ret & BLOCK_INLINE_DATA_CHANGED) {
152                 errcode_t err;
153
154                 inode.i_block[0] = ext2fs_cpu_to_le32(dirent.inode);
155                 err = ext2fs_write_inode(fs, ino, &inode);
156                 if (err)
157                         goto out;
158                 ret &= ~BLOCK_INLINE_DATA_CHANGED;
159         }
160         if (ret & BLOCK_ABORT)
161                 goto out;
162
163         ctx->buf = (char *)inode.i_block + EXT4_INLINE_DATA_DOTDOT_SIZE;
164         ctx->buflen = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DATA_DOTDOT_SIZE;
165 #ifdef WORDS_BIGENDIAN
166         ctx->errcode = ext2fs_dirent_swab_in2(fs, ctx->buf, ctx->buflen, 0);
167         if (ctx->errcode) {
168                 ret |= BLOCK_ABORT;
169                 goto out;
170         }
171 #endif
172         ret |= ext2fs_process_dir_block(fs, 0, blockcnt++, 0, 0, priv_data);
173         if (ret & BLOCK_INLINE_DATA_CHANGED) {
174 #ifdef WORDS_BIGENDIAN
175                 ctx->errcode = ext2fs_dirent_swab_out2(fs, ctx->buf,
176                                                        ctx->buflen, 0);
177                 if (ctx->errcode) {
178                         ret |= BLOCK_ABORT;
179                         goto out;
180                 }
181 #endif
182                 ctx->errcode = ext2fs_write_inode(fs, ino, &inode);
183                 if (ctx->errcode)
184                         ret |= BLOCK_ABORT;
185                 ret &= ~BLOCK_INLINE_DATA_CHANGED;
186         }
187         if (ret & BLOCK_ABORT)
188                 goto out;
189
190         data.fs = fs;
191         data.ino = ino;
192         ctx->errcode = ext2fs_inline_data_ea_get(&data);
193         if (ctx->errcode) {
194                 ret |= BLOCK_ABORT;
195                 goto out;
196         }
197         if (data.ea_size <= 0)
198                 goto out;
199
200         ctx->buf = data.ea_data;
201         ctx->buflen = data.ea_size;
202 #ifdef WORDS_BIGENDIAN
203         ctx.errcode = ext2fs_dirent_swab_in2(fs, ctx->buf, ctx->buflen, 0);
204         if (ctx.errcode) {
205                 ret |= BLOCK_ABORT;
206                 goto out;
207         }
208 #endif
209
210         ret |= ext2fs_process_dir_block(fs, 0, blockcnt++, 0, 0, priv_data);
211         if (ret & BLOCK_INLINE_DATA_CHANGED) {
212 #ifdef WORDS_BIGENDIAN
213                 ctx->errcode = ext2fs_dirent_swab_out2(fs, ctx->buf,
214                                                       ctx->buflen, 0);
215                 if (ctx->errcode) {
216                         ret |= BLOCK_ABORT;
217                         goto out1;
218                 }
219 #endif
220                 ctx->errcode = ext2fs_inline_data_ea_set(&data);
221                 if (ctx->errcode)
222                         ret |= BLOCK_ABORT;
223         }
224
225 out1:
226         ext2fs_free_mem(&data.ea_data);
227         ctx->buf = 0;
228
229 out:
230         ret &= ~(BLOCK_ABORT | BLOCK_INLINE_DATA_CHANGED);
231         return ret;
232 }