1 /*
2 * linux/fs/minix/dir.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * minix directory handling functions
7 */
8
9 #include <linux/fs.h>
10 #include <linux/minix_fs.h>
11 #include <linux/pagemap.h>
12
13 typedef struct minix_dir_entry minix_dirent;
14
15 static int minix_readdir(struct file *, void *, filldir_t);
16
17 struct file_operations minix_dir_operations = {
18 read: generic_read_dir,
19 readdir: minix_readdir,
20 fsync: minix_sync_file,
21 };
22
dir_put_page(struct page * page)23 static inline void dir_put_page(struct page *page)
24 {
25 kunmap(page);
26 page_cache_release(page);
27 }
28
dir_pages(struct inode * inode)29 static inline unsigned long dir_pages(struct inode *inode)
30 {
31 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
32 }
33
dir_commit_chunk(struct page * page,unsigned from,unsigned to)34 static int dir_commit_chunk(struct page *page, unsigned from, unsigned to)
35 {
36 struct inode *dir = (struct inode *)page->mapping->host;
37 int err = 0;
38 page->mapping->a_ops->commit_write(NULL, page, from, to);
39 if (IS_SYNC(dir)) {
40 int err2;
41 err = writeout_one_page(page);
42 err2 = waitfor_one_page(page);
43 if (err == 0)
44 err = err2;
45 }
46 return err;
47 }
48
dir_get_page(struct inode * dir,unsigned long n)49 static struct page * dir_get_page(struct inode *dir, unsigned long n)
50 {
51 struct address_space *mapping = dir->i_mapping;
52 struct page *page = read_cache_page(mapping, n,
53 (filler_t*)mapping->a_ops->readpage, NULL);
54 if (!IS_ERR(page)) {
55 wait_on_page(page);
56 kmap(page);
57 if (!Page_Uptodate(page))
58 goto fail;
59 }
60 return page;
61
62 fail:
63 dir_put_page(page);
64 return ERR_PTR(-EIO);
65 }
66
minix_next_entry(void * de,struct minix_sb_info * sbi)67 static inline void *minix_next_entry(void *de, struct minix_sb_info *sbi)
68 {
69 return (void*)((char*)de + sbi->s_dirsize);
70 }
71
minix_readdir(struct file * filp,void * dirent,filldir_t filldir)72 static int minix_readdir(struct file * filp, void * dirent, filldir_t filldir)
73 {
74 unsigned long pos = filp->f_pos;
75 struct inode *inode = filp->f_dentry->d_inode;
76 struct super_block *sb = inode->i_sb;
77 unsigned offset = pos & ~PAGE_CACHE_MASK;
78 unsigned long n = pos >> PAGE_CACHE_SHIFT;
79 unsigned long npages = dir_pages(inode);
80 struct minix_sb_info *sbi = &sb->u.minix_sb;
81 unsigned chunk_size = sbi->s_dirsize;
82
83 pos = (pos + chunk_size-1) & ~(chunk_size-1);
84 if (pos >= inode->i_size)
85 goto done;
86
87 for ( ; n < npages; n++, offset = 0) {
88 char *p, *kaddr, *limit;
89 struct page *page = dir_get_page(inode, n);
90
91 if (IS_ERR(page))
92 continue;
93 kaddr = (char *)page_address(page);
94 p = kaddr+offset;
95 limit = kaddr + PAGE_CACHE_SIZE - chunk_size;
96 for ( ; p <= limit ; p = minix_next_entry(p, sbi)) {
97 minix_dirent *de = (minix_dirent *)p;
98 if (de->inode) {
99 int over;
100 unsigned l = strnlen(de->name,sbi->s_namelen);
101
102 offset = p - kaddr;
103 over = filldir(dirent, de->name, l,
104 (n<<PAGE_CACHE_SHIFT) | offset,
105 de->inode, DT_UNKNOWN);
106 if (over) {
107 dir_put_page(page);
108 goto done;
109 }
110 }
111 }
112 dir_put_page(page);
113 }
114
115 done:
116 filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
117 UPDATE_ATIME(inode);
118 return 0;
119 }
120
namecompare(int len,int maxlen,const char * name,const char * buffer)121 static inline int namecompare(int len, int maxlen,
122 const char * name, const char * buffer)
123 {
124 if (len < maxlen && buffer[len])
125 return 0;
126 return !memcmp(name, buffer, len);
127 }
128
129 /*
130 * minix_find_entry()
131 *
132 * finds an entry in the specified directory with the wanted name. It
133 * returns the cache buffer in which the entry was found, and the entry
134 * itself (as a parameter - res_dir). It does NOT read the inode of the
135 * entry - you'll have to do that yourself if you want to.
136 */
minix_find_entry(struct dentry * dentry,struct page ** res_page)137 minix_dirent *minix_find_entry(struct dentry *dentry, struct page **res_page)
138 {
139 const char * name = dentry->d_name.name;
140 int namelen = dentry->d_name.len;
141 struct inode * dir = dentry->d_parent->d_inode;
142 struct super_block * sb = dir->i_sb;
143 struct minix_sb_info * sbi = &sb->u.minix_sb;
144 unsigned long n;
145 unsigned long npages = dir_pages(dir);
146 struct page *page = NULL;
147 struct minix_dir_entry *de;
148
149 *res_page = NULL;
150
151 for (n = 0; n < npages; n++) {
152 char *kaddr;
153 page = dir_get_page(dir, n);
154 if (IS_ERR(page))
155 continue;
156
157 kaddr = (char*)page_address(page);
158 de = (struct minix_dir_entry *) kaddr;
159 kaddr += PAGE_CACHE_SIZE - sbi->s_dirsize;
160 for ( ; (char *) de <= kaddr ; de = minix_next_entry(de,sbi)) {
161 if (!de->inode)
162 continue;
163 if (namecompare(namelen,sbi->s_namelen,name,de->name))
164 goto found;
165 }
166 dir_put_page(page);
167 }
168 return NULL;
169
170 found:
171 *res_page = page;
172 return de;
173 }
174
minix_add_link(struct dentry * dentry,struct inode * inode)175 int minix_add_link(struct dentry *dentry, struct inode *inode)
176 {
177 struct inode *dir = dentry->d_parent->d_inode;
178 const char * name = dentry->d_name.name;
179 int namelen = dentry->d_name.len;
180 struct super_block * sb = dir->i_sb;
181 struct minix_sb_info * sbi = &sb->u.minix_sb;
182 struct page *page = NULL;
183 struct minix_dir_entry * de;
184 unsigned long npages = dir_pages(dir);
185 unsigned long n;
186 char *kaddr;
187 unsigned from, to;
188 int err;
189
190 /* We take care of directory expansion in the same loop */
191 for (n = 0; n <= npages; n++) {
192 page = dir_get_page(dir, n);
193 err = PTR_ERR(page);
194 if (IS_ERR(page))
195 goto out;
196 kaddr = (char*)page_address(page);
197 de = (minix_dirent *)kaddr;
198 kaddr += PAGE_CACHE_SIZE - sbi->s_dirsize;
199 while ((char *)de <= kaddr) {
200 if (!de->inode)
201 goto got_it;
202 err = -EEXIST;
203 if (namecompare(namelen,sbi->s_namelen,name,de->name))
204 goto out_page;
205 de = minix_next_entry(de, sbi);
206 }
207 dir_put_page(page);
208 }
209 BUG();
210 return -EINVAL;
211
212 got_it:
213 from = (char*)de - (char*)page_address(page);
214 to = from + sbi->s_dirsize;
215 lock_page(page);
216 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
217 if (err)
218 goto out_unlock;
219 memcpy (de->name, name, namelen);
220 memset (de->name + namelen, 0, sbi->s_dirsize - namelen - 2);
221 de->inode = inode->i_ino;
222 err = dir_commit_chunk(page, from, to);
223 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
224 mark_inode_dirty(dir);
225 out_unlock:
226 UnlockPage(page);
227 out_page:
228 dir_put_page(page);
229 out:
230 return err;
231 }
232
minix_delete_entry(struct minix_dir_entry * de,struct page * page)233 int minix_delete_entry(struct minix_dir_entry *de, struct page *page)
234 {
235 struct address_space *mapping = page->mapping;
236 struct inode *inode = (struct inode*)mapping->host;
237 char *kaddr = (char*)page_address(page);
238 unsigned from = (char*)de - kaddr;
239 unsigned to = from + inode->i_sb->u.minix_sb.s_dirsize;
240 int err;
241
242 lock_page(page);
243 err = mapping->a_ops->prepare_write(NULL, page, from, to);
244 if (err == 0) {
245 de->inode = 0;
246 err = dir_commit_chunk(page, from, to);
247 }
248 UnlockPage(page);
249 dir_put_page(page);
250 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
251 mark_inode_dirty(inode);
252 return err;
253 }
254
minix_make_empty(struct inode * inode,struct inode * dir)255 int minix_make_empty(struct inode *inode, struct inode *dir)
256 {
257 struct address_space *mapping = inode->i_mapping;
258 struct page *page = grab_cache_page(mapping, 0);
259 struct minix_sb_info * sbi = &inode->i_sb->u.minix_sb;
260 struct minix_dir_entry * de;
261 char *base;
262 int err;
263
264 if (!page)
265 return -ENOMEM;
266 err = mapping->a_ops->prepare_write(NULL, page, 0, 2 * sbi->s_dirsize);
267 if (err)
268 goto fail;
269
270 base = (char*)page_address(page);
271 memset(base, 0, PAGE_CACHE_SIZE);
272
273 de = (struct minix_dir_entry *) base;
274 de->inode = inode->i_ino;
275 strcpy(de->name,".");
276 de = minix_next_entry(de, sbi);
277 de->inode = dir->i_ino;
278 strcpy(de->name,"..");
279
280 err = dir_commit_chunk(page, 0, 2 * sbi->s_dirsize);
281 fail:
282 UnlockPage(page);
283 page_cache_release(page);
284 return err;
285 }
286
287 /*
288 * routine to check that the specified directory is empty (for rmdir)
289 */
minix_empty_dir(struct inode * inode)290 int minix_empty_dir(struct inode * inode)
291 {
292 struct page *page = NULL;
293 unsigned long i, npages = dir_pages(inode);
294 struct minix_sb_info *sbi = &inode->i_sb->u.minix_sb;
295
296 for (i = 0; i < npages; i++) {
297 char *kaddr;
298 minix_dirent * de;
299 page = dir_get_page(inode, i);
300
301 if (IS_ERR(page))
302 continue;
303
304 kaddr = (char *)page_address(page);
305 de = (minix_dirent *)kaddr;
306 kaddr += PAGE_CACHE_SIZE - sbi->s_dirsize;
307
308 while ((char *)de <= kaddr) {
309 if (de->inode != 0) {
310 /* check for . and .. */
311 if (de->name[0] != '.')
312 goto not_empty;
313 if (!de->name[1]) {
314 if (de->inode != inode->i_ino)
315 goto not_empty;
316 } else if (de->name[1] != '.')
317 goto not_empty;
318 else if (de->name[2])
319 goto not_empty;
320 }
321 de = minix_next_entry(de, sbi);
322 }
323 dir_put_page(page);
324 }
325 return 1;
326
327 not_empty:
328 dir_put_page(page);
329 return 0;
330 }
331
332 /* Releases the page */
minix_set_link(struct minix_dir_entry * de,struct page * page,struct inode * inode)333 void minix_set_link(struct minix_dir_entry *de, struct page *page,
334 struct inode *inode)
335 {
336 struct inode *dir = (struct inode*)page->mapping->host;
337 struct minix_sb_info *sbi = &dir->i_sb->u.minix_sb;
338 unsigned from = (char *)de-(char*)page_address(page);
339 unsigned to = from + sbi->s_dirsize;
340 int err;
341
342 lock_page(page);
343 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
344 if (err == 0) {
345 de->inode = inode->i_ino;
346 err = dir_commit_chunk(page, from, to);
347 }
348 UnlockPage(page);
349 dir_put_page(page);
350 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
351 mark_inode_dirty(dir);
352 }
353
minix_dotdot(struct inode * dir,struct page ** p)354 struct minix_dir_entry * minix_dotdot (struct inode *dir, struct page **p)
355 {
356 struct page *page = dir_get_page(dir, 0);
357 struct minix_sb_info *sbi = &dir->i_sb->u.minix_sb;
358 struct minix_dir_entry *de = NULL;
359
360 if (!IS_ERR(page)) {
361 de = minix_next_entry(page_address(page), sbi);
362 *p = page;
363 }
364 return de;
365 }
366
minix_inode_by_name(struct dentry * dentry)367 ino_t minix_inode_by_name(struct dentry *dentry)
368 {
369 struct page *page;
370 struct minix_dir_entry *de = minix_find_entry(dentry, &page);
371 ino_t res = 0;
372
373 if (de) {
374 res = de->inode;
375 dir_put_page(page);
376 }
377 return res;
378 }
379