blob: 1dbf921ca44b42de92c6dda06e014b395dfcdae9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/minix/dir.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * minix directory handling functions
Andries Brouwer939b00d2007-02-12 00:52:49 -08007 *
8 * Updated to filesystem version 3 by Daniel Aragones
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include "minix.h"
Nick Piggin4a66af92007-10-16 01:25:21 -070012#include <linux/buffer_head.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/highmem.h>
Nick Piggin4a66af92007-10-16 01:25:21 -070014#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16typedef struct minix_dir_entry minix_dirent;
Andries Brouwer939b00d2007-02-12 00:52:49 -080017typedef struct minix3_dir_entry minix3_dirent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19static int minix_readdir(struct file *, void *, filldir_t);
20
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080021const struct file_operations minix_dir_operations = {
Al Virocc467592009-06-16 23:47:45 -040022 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 .read = generic_read_dir,
24 .readdir = minix_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +020025 .fsync = generic_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -070026};
27
28static inline void dir_put_page(struct page *page)
29{
30 kunmap(page);
31 page_cache_release(page);
32}
33
34/*
35 * Return the offset into page `page_nr' of the last valid
36 * byte in that page, plus one.
37 */
38static unsigned
39minix_last_byte(struct inode *inode, unsigned long page_nr)
40{
41 unsigned last_byte = PAGE_CACHE_SIZE;
42
43 if (page_nr == (inode->i_size >> PAGE_CACHE_SHIFT))
44 last_byte = inode->i_size & (PAGE_CACHE_SIZE - 1);
45 return last_byte;
46}
47
48static inline unsigned long dir_pages(struct inode *inode)
49{
50 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
51}
52
Nick Piggin4a66af92007-10-16 01:25:21 -070053static int dir_commit_chunk(struct page *page, loff_t pos, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Nick Piggin4a66af92007-10-16 01:25:21 -070055 struct address_space *mapping = page->mapping;
56 struct inode *dir = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int err = 0;
Nick Piggin4a66af92007-10-16 01:25:21 -070058 block_write_end(NULL, mapping, pos, len, len, page, NULL);
59
60 if (pos+len > dir->i_size) {
61 i_size_write(dir, pos+len);
62 mark_inode_dirty(dir);
63 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (IS_DIRSYNC(dir))
65 err = write_one_page(page, 1);
66 else
67 unlock_page(page);
68 return err;
69}
70
71static struct page * dir_get_page(struct inode *dir, unsigned long n)
72{
73 struct address_space *mapping = dir->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -070074 struct page *page = read_mapping_page(mapping, n, NULL);
Al Viro49837a82010-05-28 11:34:50 -040075 if (!IS_ERR(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80static inline void *minix_next_entry(void *de, struct minix_sb_info *sbi)
81{
82 return (void*)((char*)de + sbi->s_dirsize);
83}
84
85static int minix_readdir(struct file * filp, void * dirent, filldir_t filldir)
86{
87 unsigned long pos = filp->f_pos;
Josef Sipekdcf258a2006-12-08 02:37:19 -080088 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct super_block *sb = inode->i_sb;
90 unsigned offset = pos & ~PAGE_CACHE_MASK;
91 unsigned long n = pos >> PAGE_CACHE_SHIFT;
92 unsigned long npages = dir_pages(inode);
93 struct minix_sb_info *sbi = minix_sb(sb);
94 unsigned chunk_size = sbi->s_dirsize;
Andries Brouwer939b00d2007-02-12 00:52:49 -080095 char *name;
96 __u32 inumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 pos = (pos + chunk_size-1) & ~(chunk_size-1);
99 if (pos >= inode->i_size)
100 goto done;
101
102 for ( ; n < npages; n++, offset = 0) {
103 char *p, *kaddr, *limit;
104 struct page *page = dir_get_page(inode, n);
105
106 if (IS_ERR(page))
107 continue;
108 kaddr = (char *)page_address(page);
109 p = kaddr+offset;
110 limit = kaddr + minix_last_byte(inode, n) - chunk_size;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800111 for ( ; p <= limit; p = minix_next_entry(p, sbi)) {
112 if (sbi->s_version == MINIX_V3) {
113 minix3_dirent *de3 = (minix3_dirent *)p;
114 name = de3->name;
115 inumber = de3->inode;
116 } else {
117 minix_dirent *de = (minix_dirent *)p;
118 name = de->name;
119 inumber = de->inode;
120 }
121 if (inumber) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 int over;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Andries Brouwer939b00d2007-02-12 00:52:49 -0800124 unsigned l = strnlen(name, sbi->s_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 offset = p - kaddr;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800126 over = filldir(dirent, name, l,
127 (n << PAGE_CACHE_SHIFT) | offset,
128 inumber, DT_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (over) {
130 dir_put_page(page);
131 goto done;
132 }
133 }
134 }
135 dir_put_page(page);
136 }
137
138done:
139 filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return 0;
141}
142
143static inline int namecompare(int len, int maxlen,
144 const char * name, const char * buffer)
145{
146 if (len < maxlen && buffer[len])
147 return 0;
148 return !memcmp(name, buffer, len);
149}
150
151/*
152 * minix_find_entry()
153 *
154 * finds an entry in the specified directory with the wanted name. It
155 * returns the cache buffer in which the entry was found, and the entry
156 * itself (as a parameter - res_dir). It does NOT read the inode of the
157 * entry - you'll have to do that yourself if you want to.
158 */
159minix_dirent *minix_find_entry(struct dentry *dentry, struct page **res_page)
160{
161 const char * name = dentry->d_name.name;
162 int namelen = dentry->d_name.len;
163 struct inode * dir = dentry->d_parent->d_inode;
164 struct super_block * sb = dir->i_sb;
165 struct minix_sb_info * sbi = minix_sb(sb);
166 unsigned long n;
167 unsigned long npages = dir_pages(dir);
168 struct page *page = NULL;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800169 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Andries Brouwer939b00d2007-02-12 00:52:49 -0800171 char *namx;
172 __u32 inumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *res_page = NULL;
174
175 for (n = 0; n < npages; n++) {
Andries Brouwer939b00d2007-02-12 00:52:49 -0800176 char *kaddr, *limit;
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 page = dir_get_page(dir, n);
179 if (IS_ERR(page))
180 continue;
181
182 kaddr = (char*)page_address(page);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800183 limit = kaddr + minix_last_byte(dir, n) - sbi->s_dirsize;
184 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
185 if (sbi->s_version == MINIX_V3) {
186 minix3_dirent *de3 = (minix3_dirent *)p;
187 namx = de3->name;
188 inumber = de3->inode;
189 } else {
190 minix_dirent *de = (minix_dirent *)p;
191 namx = de->name;
192 inumber = de->inode;
193 }
194 if (!inumber)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 continue;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800196 if (namecompare(namelen, sbi->s_namelen, name, namx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 goto found;
198 }
199 dir_put_page(page);
200 }
201 return NULL;
202
203found:
204 *res_page = page;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800205 return (minix_dirent *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208int minix_add_link(struct dentry *dentry, struct inode *inode)
209{
210 struct inode *dir = dentry->d_parent->d_inode;
211 const char * name = dentry->d_name.name;
212 int namelen = dentry->d_name.len;
213 struct super_block * sb = dir->i_sb;
214 struct minix_sb_info * sbi = minix_sb(sb);
215 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 unsigned long npages = dir_pages(dir);
217 unsigned long n;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800218 char *kaddr, *p;
219 minix_dirent *de;
220 minix3_dirent *de3;
Nick Piggin4a66af92007-10-16 01:25:21 -0700221 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int err;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800223 char *namx = NULL;
224 __u32 inumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /*
227 * We take care of directory expansion in the same loop
228 * This code plays outside i_size, so it locks the page
229 * to protect that region.
230 */
231 for (n = 0; n <= npages; n++) {
Andries Brouwer939b00d2007-02-12 00:52:49 -0800232 char *limit, *dir_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 page = dir_get_page(dir, n);
235 err = PTR_ERR(page);
236 if (IS_ERR(page))
237 goto out;
238 lock_page(page);
239 kaddr = (char*)page_address(page);
240 dir_end = kaddr + minix_last_byte(dir, n);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800241 limit = kaddr + PAGE_CACHE_SIZE - sbi->s_dirsize;
242 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
243 de = (minix_dirent *)p;
244 de3 = (minix3_dirent *)p;
245 if (sbi->s_version == MINIX_V3) {
246 namx = de3->name;
247 inumber = de3->inode;
248 } else {
249 namx = de->name;
250 inumber = de->inode;
251 }
252 if (p == dir_end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /* We hit i_size */
Andries Brouwer939b00d2007-02-12 00:52:49 -0800254 if (sbi->s_version == MINIX_V3)
255 de3->inode = 0;
256 else
257 de->inode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto got_it;
259 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800260 if (!inumber)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 goto got_it;
262 err = -EEXIST;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800263 if (namecompare(namelen, sbi->s_namelen, name, namx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 unlock_page(page);
267 dir_put_page(page);
268 }
269 BUG();
270 return -EINVAL;
271
272got_it:
Evgeniy Dushistovd6b54842009-01-06 14:42:38 -0800273 pos = page_offset(page) + p - (char *)page_address(page);
Nick Piggin4a66af92007-10-16 01:25:21 -0700274 err = __minix_write_begin(NULL, page->mapping, pos, sbi->s_dirsize,
275 AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (err)
277 goto out_unlock;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800278 memcpy (namx, name, namelen);
279 if (sbi->s_version == MINIX_V3) {
280 memset (namx + namelen, 0, sbi->s_dirsize - namelen - 4);
281 de3->inode = inode->i_ino;
282 } else {
283 memset (namx + namelen, 0, sbi->s_dirsize - namelen - 2);
284 de->inode = inode->i_ino;
285 }
Nick Piggin4a66af92007-10-16 01:25:21 -0700286 err = dir_commit_chunk(page, pos, sbi->s_dirsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
288 mark_inode_dirty(dir);
289out_put:
290 dir_put_page(page);
291out:
292 return err;
293out_unlock:
294 unlock_page(page);
295 goto out_put;
296}
297
298int minix_delete_entry(struct minix_dir_entry *de, struct page *page)
299{
300 struct address_space *mapping = page->mapping;
301 struct inode *inode = (struct inode*)mapping->host;
302 char *kaddr = page_address(page);
Nick Piggin4a66af92007-10-16 01:25:21 -0700303 loff_t pos = page_offset(page) + (char*)de - kaddr;
Doug Graham9f6c1332009-09-22 16:48:00 -0700304 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
305 unsigned len = sbi->s_dirsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int err;
307
308 lock_page(page);
Nick Piggin4a66af92007-10-16 01:25:21 -0700309 err = __minix_write_begin(NULL, mapping, pos, len,
310 AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (err == 0) {
Doug Graham9f6c1332009-09-22 16:48:00 -0700312 if (sbi->s_version == MINIX_V3)
313 ((minix3_dirent *) de)->inode = 0;
314 else
315 de->inode = 0;
Nick Piggin4a66af92007-10-16 01:25:21 -0700316 err = dir_commit_chunk(page, pos, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 } else {
318 unlock_page(page);
319 }
320 dir_put_page(page);
321 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
322 mark_inode_dirty(inode);
323 return err;
324}
325
326int minix_make_empty(struct inode *inode, struct inode *dir)
327{
328 struct address_space *mapping = inode->i_mapping;
329 struct page *page = grab_cache_page(mapping, 0);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800330 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 char *kaddr;
332 int err;
333
334 if (!page)
335 return -ENOMEM;
Nick Piggin4a66af92007-10-16 01:25:21 -0700336 err = __minix_write_begin(NULL, mapping, 0, 2 * sbi->s_dirsize,
337 AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (err) {
339 unlock_page(page);
340 goto fail;
341 }
342
343 kaddr = kmap_atomic(page, KM_USER0);
344 memset(kaddr, 0, PAGE_CACHE_SIZE);
345
Andries Brouwer939b00d2007-02-12 00:52:49 -0800346 if (sbi->s_version == MINIX_V3) {
347 minix3_dirent *de3 = (minix3_dirent *)kaddr;
348
349 de3->inode = inode->i_ino;
350 strcpy(de3->name, ".");
351 de3 = minix_next_entry(de3, sbi);
352 de3->inode = dir->i_ino;
353 strcpy(de3->name, "..");
354 } else {
355 minix_dirent *de = (minix_dirent *)kaddr;
356
357 de->inode = inode->i_ino;
358 strcpy(de->name, ".");
359 de = minix_next_entry(de, sbi);
360 de->inode = dir->i_ino;
361 strcpy(de->name, "..");
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 kunmap_atomic(kaddr, KM_USER0);
364
365 err = dir_commit_chunk(page, 0, 2 * sbi->s_dirsize);
366fail:
367 page_cache_release(page);
368 return err;
369}
370
371/*
372 * routine to check that the specified directory is empty (for rmdir)
373 */
374int minix_empty_dir(struct inode * inode)
375{
376 struct page *page = NULL;
377 unsigned long i, npages = dir_pages(inode);
378 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800379 char *name;
380 __u32 inumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 for (i = 0; i < npages; i++) {
Andries Brouwer939b00d2007-02-12 00:52:49 -0800383 char *p, *kaddr, *limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Andries Brouwer939b00d2007-02-12 00:52:49 -0800385 page = dir_get_page(inode, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (IS_ERR(page))
387 continue;
388
389 kaddr = (char *)page_address(page);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800390 limit = kaddr + minix_last_byte(inode, i) - sbi->s_dirsize;
391 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
392 if (sbi->s_version == MINIX_V3) {
393 minix3_dirent *de3 = (minix3_dirent *)p;
394 name = de3->name;
395 inumber = de3->inode;
396 } else {
397 minix_dirent *de = (minix_dirent *)p;
398 name = de->name;
399 inumber = de->inode;
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Andries Brouwer939b00d2007-02-12 00:52:49 -0800402 if (inumber != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* check for . and .. */
Andries Brouwer939b00d2007-02-12 00:52:49 -0800404 if (name[0] != '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 goto not_empty;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800406 if (!name[1]) {
407 if (inumber != inode->i_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 goto not_empty;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800409 } else if (name[1] != '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 goto not_empty;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800411 else if (name[2])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 goto not_empty;
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 dir_put_page(page);
416 }
417 return 1;
418
419not_empty:
420 dir_put_page(page);
421 return 0;
422}
423
424/* Releases the page */
425void minix_set_link(struct minix_dir_entry *de, struct page *page,
426 struct inode *inode)
427{
Nick Piggin4a66af92007-10-16 01:25:21 -0700428 struct address_space *mapping = page->mapping;
429 struct inode *dir = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct minix_sb_info *sbi = minix_sb(dir->i_sb);
Nick Piggin4a66af92007-10-16 01:25:21 -0700431 loff_t pos = page_offset(page) +
432 (char *)de-(char*)page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 int err;
434
435 lock_page(page);
Nick Piggin4a66af92007-10-16 01:25:21 -0700436
437 err = __minix_write_begin(NULL, mapping, pos, sbi->s_dirsize,
438 AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (err == 0) {
Doug Graham9f6c1332009-09-22 16:48:00 -0700440 if (sbi->s_version == MINIX_V3)
441 ((minix3_dirent *) de)->inode = inode->i_ino;
442 else
443 de->inode = inode->i_ino;
Nick Piggin4a66af92007-10-16 01:25:21 -0700444 err = dir_commit_chunk(page, pos, sbi->s_dirsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 } else {
446 unlock_page(page);
447 }
448 dir_put_page(page);
449 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
450 mark_inode_dirty(dir);
451}
452
453struct minix_dir_entry * minix_dotdot (struct inode *dir, struct page **p)
454{
455 struct page *page = dir_get_page(dir, 0);
456 struct minix_sb_info *sbi = minix_sb(dir->i_sb);
457 struct minix_dir_entry *de = NULL;
458
459 if (!IS_ERR(page)) {
460 de = minix_next_entry(page_address(page), sbi);
461 *p = page;
462 }
463 return de;
464}
465
466ino_t minix_inode_by_name(struct dentry *dentry)
467{
468 struct page *page;
469 struct minix_dir_entry *de = minix_find_entry(dentry, &page);
470 ino_t res = 0;
471
472 if (de) {
Doug Graham9f6c1332009-09-22 16:48:00 -0700473 struct address_space *mapping = page->mapping;
474 struct inode *inode = mapping->host;
475 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
476
477 if (sbi->s_version == MINIX_V3)
478 res = ((minix3_dirent *) de)->inode;
479 else
480 res = de->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 dir_put_page(page);
482 }
483 return res;
484}