drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / reiserfs / dir.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
1da177e4
LT
5#include <linux/string.h>
6#include <linux/errno.h>
7#include <linux/fs.h>
f466c6fd 8#include "reiserfs.h"
1da177e4 9#include <linux/stat.h>
1da177e4 10#include <linux/buffer_head.h>
5a0e3ad6 11#include <linux/slab.h>
1da177e4
LT
12#include <asm/uaccess.h>
13
5a1b6391 14extern const struct reiserfs_key MIN_KEY;
1da177e4 15
bd4c625c 16static int reiserfs_readdir(struct file *, void *, filldir_t);
02c24a82
JB
17static int reiserfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
18 int datasync);
1da177e4 19
4b6f5d20 20const struct file_operations reiserfs_dir_operations = {
ca572727 21 .llseek = generic_file_llseek,
bd4c625c
LT
22 .read = generic_read_dir,
23 .readdir = reiserfs_readdir,
24 .fsync = reiserfs_dir_fsync,
205cb37b 25 .unlocked_ioctl = reiserfs_ioctl,
52b499c4
DH
26#ifdef CONFIG_COMPAT
27 .compat_ioctl = reiserfs_compat_ioctl,
28#endif
1da177e4
LT
29};
30
02c24a82
JB
31static int reiserfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
32 int datasync)
bd4c625c 33{
7ea80859 34 struct inode *inode = filp->f_mapping->host;
bd4c625c 35 int err;
02c24a82
JB
36
37 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
38 if (err)
39 return err;
40
41 mutex_lock(&inode->i_mutex);
bd4c625c
LT
42 reiserfs_write_lock(inode->i_sb);
43 err = reiserfs_commit_for_inode(inode);
44 reiserfs_write_unlock(inode->i_sb);
02c24a82 45 mutex_unlock(&inode->i_mutex);
bd4c625c
LT
46 if (err < 0)
47 return err;
48 return 0;
1da177e4
LT
49}
50
1da177e4
LT
51#define store_ih(where,what) copy_item_head (where, what)
52
677c9b2e
JM
53static inline bool is_privroot_deh(struct dentry *dir,
54 struct reiserfs_de_head *deh)
55{
677c9b2e 56 struct dentry *privroot = REISERFS_SB(dir->d_sb)->priv_root;
73422811
JM
57 return (dir == dir->d_parent && privroot->d_inode &&
58 deh->deh_objectid == INODE_PKEY(privroot->d_inode)->k_objectid);
677c9b2e
JM
59}
60
a41f1a47
JM
61int reiserfs_readdir_dentry(struct dentry *dentry, void *dirent,
62 filldir_t filldir, loff_t *pos)
1da177e4 63{
a41f1a47 64 struct inode *inode = dentry->d_inode;
bd4c625c
LT
65 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
66 INITIALIZE_PATH(path_to_entry);
67 struct buffer_head *bh;
68 int item_num, entry_num;
69 const struct reiserfs_key *rkey;
70 struct item_head *ih, tmp_ih;
71 int search_res;
72 char *local_buf;
73 loff_t next_pos;
74 char small_buf[32]; /* avoid kmalloc if we can */
75 struct reiserfs_dir_entry de;
76 int ret = 0;
77
78 reiserfs_write_lock(inode->i_sb);
79
80 reiserfs_check_lock_depth(inode->i_sb, "readdir");
81
82 /* form key for search the next directory entry using f_pos field of
83 file structure */
a41f1a47 84 make_cpu_key(&pos_key, inode, *pos ?: DOT_OFFSET, TYPE_DIRENTRY, 3);
bd4c625c
LT
85 next_pos = cpu_key_k_offset(&pos_key);
86
bd4c625c
LT
87 path_to_entry.reada = PATH_READA;
88 while (1) {
89 research:
90 /* search the directory item, containing entry with specified key */
91 search_res =
92 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
93 &de);
94 if (search_res == IO_ERROR) {
95 // FIXME: we could just skip part of directory which could
96 // not be read
97 ret = -EIO;
1da177e4 98 goto out;
1da177e4 99 }
bd4c625c
LT
100 entry_num = de.de_entry_num;
101 bh = de.de_bh;
102 item_num = de.de_item_num;
103 ih = de.de_ih;
104 store_ih(&tmp_ih, ih);
105
106 /* we must have found item, that is item of this directory, */
107 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
108 "vs-9000: found item %h does not match to dir we readdir %K",
109 ih, &pos_key);
110 RFALSE(item_num > B_NR_ITEMS(bh) - 1,
111 "vs-9005 item_num == %d, item amount == %d",
112 item_num, B_NR_ITEMS(bh));
113
114 /* and entry must be not more than number of entries in the item */
115 RFALSE(I_ENTRY_COUNT(ih) < entry_num,
116 "vs-9010: entry number is too big %d (%d)",
117 entry_num, I_ENTRY_COUNT(ih));
118
119 if (search_res == POSITION_FOUND
120 || entry_num < I_ENTRY_COUNT(ih)) {
121 /* go through all entries in the directory item beginning from the entry, that has been found */
122 struct reiserfs_de_head *deh =
123 B_I_DEH(bh, ih) + entry_num;
124
125 for (; entry_num < I_ENTRY_COUNT(ih);
126 entry_num++, deh++) {
127 int d_reclen;
128 char *d_name;
129 off_t d_off;
130 ino_t d_ino;
1d06be05 131 loff_t cur_pos = deh_offset(deh);
bd4c625c
LT
132
133 if (!de_visible(deh))
134 /* it is hidden entry */
135 continue;
136 d_reclen = entry_length(bh, ih, entry_num);
137 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
80eb68d2
LW
138
139 if (d_reclen <= 0 ||
140 d_name + d_reclen > bh->b_data + bh->b_size) {
141 /* There is corrupted data in entry,
142 * We'd better stop here */
143 pathrelse(&path_to_entry);
144 ret = -EIO;
145 goto out;
146 }
147
bd4c625c
LT
148 if (!d_name[d_reclen - 1])
149 d_reclen = strlen(d_name);
150
151 if (d_reclen >
152 REISERFS_MAX_NAME(inode->i_sb->
153 s_blocksize)) {
154 /* too big to send back to VFS */
155 continue;
156 }
157
158 /* Ignore the .reiserfs_priv entry */
677c9b2e 159 if (is_privroot_deh(dentry, deh))
bd4c625c 160 continue;
bd4c625c
LT
161
162 d_off = deh_offset(deh);
a41f1a47 163 *pos = d_off;
bd4c625c
LT
164 d_ino = deh_objectid(deh);
165 if (d_reclen <= 32) {
166 local_buf = small_buf;
167 } else {
d739b42b
PE
168 local_buf = kmalloc(d_reclen,
169 GFP_NOFS);
bd4c625c
LT
170 if (!local_buf) {
171 pathrelse(&path_to_entry);
172 ret = -ENOMEM;
173 goto out;
174 }
175 if (item_moved(&tmp_ih, &path_to_entry)) {
d739b42b 176 kfree(local_buf);
bd4c625c
LT
177 goto research;
178 }
179 }
180 // Note, that we copy name to user space via temporary
181 // buffer (local_buf) because filldir will block if
182 // user space buffer is swapped out. At that time
183 // entry can move to somewhere else
184 memcpy(local_buf, d_name, d_reclen);
8ebc4232
FW
185
186 /*
187 * Since filldir might sleep, we can release
188 * the write lock here for other waiters
189 */
190 reiserfs_write_unlock(inode->i_sb);
bd4c625c
LT
191 if (filldir
192 (dirent, local_buf, d_reclen, d_off, d_ino,
193 DT_UNKNOWN) < 0) {
8ebc4232 194 reiserfs_write_lock(inode->i_sb);
bd4c625c 195 if (local_buf != small_buf) {
d739b42b 196 kfree(local_buf);
bd4c625c
LT
197 }
198 goto end;
199 }
8ebc4232 200 reiserfs_write_lock(inode->i_sb);
bd4c625c 201 if (local_buf != small_buf) {
d739b42b 202 kfree(local_buf);
bd4c625c 203 }
1d06be05
JM
204
205 /* deh_offset(deh) may be invalid now. */
206 next_pos = cur_pos + 1;
bd4c625c
LT
207
208 if (item_moved(&tmp_ih, &path_to_entry)) {
0bdc7acb
JM
209 set_cpu_key_k_offset(&pos_key,
210 next_pos);
bd4c625c
LT
211 goto research;
212 }
213 } /* for */
1da177e4
LT
214 }
215
bd4c625c
LT
216 if (item_num != B_NR_ITEMS(bh) - 1)
217 // end of directory has been reached
218 goto end;
219
220 /* item we went through is last item of node. Using right
221 delimiting key check is it directory end */
222 rkey = get_rkey(&path_to_entry, inode->i_sb);
223 if (!comp_le_keys(rkey, &MIN_KEY)) {
224 /* set pos_key to key, that is the smallest and greater
225 that key of the last entry in the item */
226 set_cpu_key_k_offset(&pos_key, next_pos);
227 continue;
228 }
1da177e4 229
bd4c625c
LT
230 if (COMP_SHORT_KEYS(rkey, &pos_key)) {
231 // end of directory has been reached
232 goto end;
1da177e4 233 }
bd4c625c
LT
234
235 /* directory continues in the right neighboring block */
236 set_cpu_key_k_offset(&pos_key,
237 le_key_k_offset(KEY_FORMAT_3_5, rkey));
238
239 } /* while */
240
a41f1a47
JM
241end:
242 *pos = next_pos;
bd4c625c
LT
243 pathrelse(&path_to_entry);
244 reiserfs_check_path(&path_to_entry);
a41f1a47 245out:
bd4c625c
LT
246 reiserfs_write_unlock(inode->i_sb);
247 return ret;
1da177e4
LT
248}
249
a41f1a47
JM
250static int reiserfs_readdir(struct file *file, void *dirent, filldir_t filldir)
251{
252 struct dentry *dentry = file->f_path.dentry;
253 return reiserfs_readdir_dentry(dentry, dirent, filldir, &file->f_pos);
254}
255
1da177e4
LT
256/* compose directory item containing "." and ".." entries (entries are
257 not aligned to 4 byte boundary) */
258/* the last four params are LE */
bd4c625c
LT
259void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
260 __le32 par_dirid, __le32 par_objid)
1da177e4 261{
bd4c625c
LT
262 struct reiserfs_de_head *deh;
263
264 memset(body, 0, EMPTY_DIR_SIZE_V1);
265 deh = (struct reiserfs_de_head *)body;
266
267 /* direntry header of "." */
268 put_deh_offset(&(deh[0]), DOT_OFFSET);
269 /* these two are from make_le_item_head, and are are LE */
270 deh[0].deh_dir_id = dirid;
271 deh[0].deh_objectid = objid;
272 deh[0].deh_state = 0; /* Endian safe if 0 */
273 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE_V1 - strlen("."));
274 mark_de_visible(&(deh[0]));
275
276 /* direntry header of ".." */
277 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
278 /* key of ".." for the root directory */
279 /* these two are from the inode, and are are LE */
280 deh[1].deh_dir_id = par_dirid;
281 deh[1].deh_objectid = par_objid;
282 deh[1].deh_state = 0; /* Endian safe if 0 */
283 put_deh_location(&(deh[1]), deh_location(&(deh[0])) - strlen(".."));
284 mark_de_visible(&(deh[1]));
285
286 /* copy ".." and "." */
287 memcpy(body + deh_location(&(deh[0])), ".", 1);
288 memcpy(body + deh_location(&(deh[1])), "..", 2);
1da177e4
LT
289}
290
291/* compose directory item containing "." and ".." entries */
bd4c625c
LT
292void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
293 __le32 par_dirid, __le32 par_objid)
1da177e4 294{
bd4c625c
LT
295 struct reiserfs_de_head *deh;
296
297 memset(body, 0, EMPTY_DIR_SIZE);
298 deh = (struct reiserfs_de_head *)body;
299
300 /* direntry header of "." */
301 put_deh_offset(&(deh[0]), DOT_OFFSET);
302 /* these two are from make_le_item_head, and are are LE */
303 deh[0].deh_dir_id = dirid;
304 deh[0].deh_objectid = objid;
305 deh[0].deh_state = 0; /* Endian safe if 0 */
306 put_deh_location(&(deh[0]), EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
307 mark_de_visible(&(deh[0]));
308
309 /* direntry header of ".." */
310 put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
311 /* key of ".." for the root directory */
312 /* these two are from the inode, and are are LE */
313 deh[1].deh_dir_id = par_dirid;
314 deh[1].deh_objectid = par_objid;
315 deh[1].deh_state = 0; /* Endian safe if 0 */
316 put_deh_location(&(deh[1]),
317 deh_location(&(deh[0])) - ROUND_UP(strlen("..")));
318 mark_de_visible(&(deh[1]));
319
320 /* copy ".." and "." */
321 memcpy(body + deh_location(&(deh[0])), ".", 1);
322 memcpy(body + deh_location(&(deh[1])), "..", 2);
1da177e4 323}