blob: d19ac258105aadb44382650de7b50504b6eaf237 [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
Al Viro80886292013-05-15 18:51:49 -040019static int minix_readdir(struct file *, struct dir_context *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
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,
Al Viro80886292013-05-15 18:51:49 -040024 .iterate = 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
Nick Piggin4a66af92007-10-16 01:25:21 -070048static int dir_commit_chunk(struct page *page, loff_t pos, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Nick Piggin4a66af92007-10-16 01:25:21 -070050 struct address_space *mapping = page->mapping;
51 struct inode *dir = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int err = 0;
Nick Piggin4a66af92007-10-16 01:25:21 -070053 block_write_end(NULL, mapping, pos, len, len, page, NULL);
54
55 if (pos+len > dir->i_size) {
56 i_size_write(dir, pos+len);
57 mark_inode_dirty(dir);
58 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (IS_DIRSYNC(dir))
60 err = write_one_page(page, 1);
61 else
62 unlock_page(page);
63 return err;
64}
65
66static struct page * dir_get_page(struct inode *dir, unsigned long n)
67{
68 struct address_space *mapping = dir->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -070069 struct page *page = read_mapping_page(mapping, n, NULL);
Al Viro49837a82010-05-28 11:34:50 -040070 if (!IS_ERR(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75static inline void *minix_next_entry(void *de, struct minix_sb_info *sbi)
76{
77 return (void*)((char*)de + sbi->s_dirsize);
78}
79
Al Viro80886292013-05-15 18:51:49 -040080static int minix_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Al Viro80886292013-05-15 18:51:49 -040082 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct minix_sb_info *sbi = minix_sb(sb);
85 unsigned chunk_size = sbi->s_dirsize;
Al Viro80886292013-05-15 18:51:49 -040086 unsigned long npages = dir_pages(inode);
87 unsigned long pos = ctx->pos;
88 unsigned offset;
89 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Dan Carpenter642b7042013-06-19 10:08:10 +100091 ctx->pos = pos = ALIGN(pos, chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (pos >= inode->i_size)
Al Viro80886292013-05-15 18:51:49 -040093 return 0;
94
95 offset = pos & ~PAGE_CACHE_MASK;
96 n = pos >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 for ( ; n < npages; n++, offset = 0) {
99 char *p, *kaddr, *limit;
100 struct page *page = dir_get_page(inode, n);
101
102 if (IS_ERR(page))
103 continue;
104 kaddr = (char *)page_address(page);
105 p = kaddr+offset;
106 limit = kaddr + minix_last_byte(inode, n) - chunk_size;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800107 for ( ; p <= limit; p = minix_next_entry(p, sbi)) {
Al Viro80886292013-05-15 18:51:49 -0400108 const char *name;
109 __u32 inumber;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800110 if (sbi->s_version == MINIX_V3) {
111 minix3_dirent *de3 = (minix3_dirent *)p;
112 name = de3->name;
113 inumber = de3->inode;
114 } else {
115 minix_dirent *de = (minix_dirent *)p;
116 name = de->name;
117 inumber = de->inode;
118 }
119 if (inumber) {
Andries Brouwer939b00d2007-02-12 00:52:49 -0800120 unsigned l = strnlen(name, sbi->s_namelen);
Al Viro80886292013-05-15 18:51:49 -0400121 if (!dir_emit(ctx, name, l,
122 inumber, DT_UNKNOWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 dir_put_page(page);
Al Viro80886292013-05-15 18:51:49 -0400124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126 }
Al Viro80886292013-05-15 18:51:49 -0400127 ctx->pos += chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129 dir_put_page(page);
130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return 0;
132}
133
134static inline int namecompare(int len, int maxlen,
135 const char * name, const char * buffer)
136{
137 if (len < maxlen && buffer[len])
138 return 0;
139 return !memcmp(name, buffer, len);
140}
141
142/*
143 * minix_find_entry()
144 *
145 * finds an entry in the specified directory with the wanted name. It
146 * returns the cache buffer in which the entry was found, and the entry
147 * itself (as a parameter - res_dir). It does NOT read the inode of the
148 * entry - you'll have to do that yourself if you want to.
149 */
150minix_dirent *minix_find_entry(struct dentry *dentry, struct page **res_page)
151{
152 const char * name = dentry->d_name.name;
153 int namelen = dentry->d_name.len;
David Howells2b0143b2015-03-17 22:25:59 +0000154 struct inode * dir = d_inode(dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct super_block * sb = dir->i_sb;
156 struct minix_sb_info * sbi = minix_sb(sb);
157 unsigned long n;
158 unsigned long npages = dir_pages(dir);
159 struct page *page = NULL;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800160 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Andries Brouwer939b00d2007-02-12 00:52:49 -0800162 char *namx;
163 __u32 inumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 *res_page = NULL;
165
166 for (n = 0; n < npages; n++) {
Andries Brouwer939b00d2007-02-12 00:52:49 -0800167 char *kaddr, *limit;
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 page = dir_get_page(dir, n);
170 if (IS_ERR(page))
171 continue;
172
173 kaddr = (char*)page_address(page);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800174 limit = kaddr + minix_last_byte(dir, n) - sbi->s_dirsize;
175 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
176 if (sbi->s_version == MINIX_V3) {
177 minix3_dirent *de3 = (minix3_dirent *)p;
178 namx = de3->name;
179 inumber = de3->inode;
180 } else {
181 minix_dirent *de = (minix_dirent *)p;
182 namx = de->name;
183 inumber = de->inode;
184 }
185 if (!inumber)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 continue;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800187 if (namecompare(namelen, sbi->s_namelen, name, namx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto found;
189 }
190 dir_put_page(page);
191 }
192 return NULL;
193
194found:
195 *res_page = page;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800196 return (minix_dirent *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199int minix_add_link(struct dentry *dentry, struct inode *inode)
200{
David Howells2b0143b2015-03-17 22:25:59 +0000201 struct inode *dir = d_inode(dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 const char * name = dentry->d_name.name;
203 int namelen = dentry->d_name.len;
204 struct super_block * sb = dir->i_sb;
205 struct minix_sb_info * sbi = minix_sb(sb);
206 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 unsigned long npages = dir_pages(dir);
208 unsigned long n;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800209 char *kaddr, *p;
210 minix_dirent *de;
211 minix3_dirent *de3;
Nick Piggin4a66af92007-10-16 01:25:21 -0700212 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 int err;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800214 char *namx = NULL;
215 __u32 inumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /*
218 * We take care of directory expansion in the same loop
219 * This code plays outside i_size, so it locks the page
220 * to protect that region.
221 */
222 for (n = 0; n <= npages; n++) {
Andries Brouwer939b00d2007-02-12 00:52:49 -0800223 char *limit, *dir_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 page = dir_get_page(dir, n);
226 err = PTR_ERR(page);
227 if (IS_ERR(page))
228 goto out;
229 lock_page(page);
230 kaddr = (char*)page_address(page);
231 dir_end = kaddr + minix_last_byte(dir, n);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800232 limit = kaddr + PAGE_CACHE_SIZE - sbi->s_dirsize;
233 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
234 de = (minix_dirent *)p;
235 de3 = (minix3_dirent *)p;
236 if (sbi->s_version == MINIX_V3) {
237 namx = de3->name;
238 inumber = de3->inode;
239 } else {
240 namx = de->name;
241 inumber = de->inode;
242 }
243 if (p == dir_end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* We hit i_size */
Andries Brouwer939b00d2007-02-12 00:52:49 -0800245 if (sbi->s_version == MINIX_V3)
246 de3->inode = 0;
247 else
248 de->inode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 goto got_it;
250 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800251 if (!inumber)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 goto got_it;
253 err = -EEXIST;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800254 if (namecompare(namelen, sbi->s_namelen, name, namx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257 unlock_page(page);
258 dir_put_page(page);
259 }
260 BUG();
261 return -EINVAL;
262
263got_it:
Evgeniy Dushistovd6b54842009-01-06 14:42:38 -0800264 pos = page_offset(page) + p - (char *)page_address(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200265 err = minix_prepare_chunk(page, pos, sbi->s_dirsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (err)
267 goto out_unlock;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800268 memcpy (namx, name, namelen);
269 if (sbi->s_version == MINIX_V3) {
270 memset (namx + namelen, 0, sbi->s_dirsize - namelen - 4);
271 de3->inode = inode->i_ino;
272 } else {
273 memset (namx + namelen, 0, sbi->s_dirsize - namelen - 2);
274 de->inode = inode->i_ino;
275 }
Nick Piggin4a66af92007-10-16 01:25:21 -0700276 err = dir_commit_chunk(page, pos, sbi->s_dirsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
278 mark_inode_dirty(dir);
279out_put:
280 dir_put_page(page);
281out:
282 return err;
283out_unlock:
284 unlock_page(page);
285 goto out_put;
286}
287
288int minix_delete_entry(struct minix_dir_entry *de, struct page *page)
289{
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200290 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 char *kaddr = page_address(page);
Nick Piggin4a66af92007-10-16 01:25:21 -0700292 loff_t pos = page_offset(page) + (char*)de - kaddr;
Doug Graham9f6c1332009-09-22 16:48:00 -0700293 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
294 unsigned len = sbi->s_dirsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int err;
296
297 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200298 err = minix_prepare_chunk(page, pos, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (err == 0) {
Doug Graham9f6c1332009-09-22 16:48:00 -0700300 if (sbi->s_version == MINIX_V3)
301 ((minix3_dirent *) de)->inode = 0;
302 else
303 de->inode = 0;
Nick Piggin4a66af92007-10-16 01:25:21 -0700304 err = dir_commit_chunk(page, pos, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 } else {
306 unlock_page(page);
307 }
308 dir_put_page(page);
309 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
310 mark_inode_dirty(inode);
311 return err;
312}
313
314int minix_make_empty(struct inode *inode, struct inode *dir)
315{
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200316 struct page *page = grab_cache_page(inode->i_mapping, 0);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800317 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 char *kaddr;
319 int err;
320
321 if (!page)
322 return -ENOMEM;
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200323 err = minix_prepare_chunk(page, 0, 2 * sbi->s_dirsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (err) {
325 unlock_page(page);
326 goto fail;
327 }
328
Cong Wang27a6d5c2011-11-25 23:14:32 +0800329 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 memset(kaddr, 0, PAGE_CACHE_SIZE);
331
Andries Brouwer939b00d2007-02-12 00:52:49 -0800332 if (sbi->s_version == MINIX_V3) {
333 minix3_dirent *de3 = (minix3_dirent *)kaddr;
334
335 de3->inode = inode->i_ino;
336 strcpy(de3->name, ".");
337 de3 = minix_next_entry(de3, sbi);
338 de3->inode = dir->i_ino;
339 strcpy(de3->name, "..");
340 } else {
341 minix_dirent *de = (minix_dirent *)kaddr;
342
343 de->inode = inode->i_ino;
344 strcpy(de->name, ".");
345 de = minix_next_entry(de, sbi);
346 de->inode = dir->i_ino;
347 strcpy(de->name, "..");
348 }
Cong Wang27a6d5c2011-11-25 23:14:32 +0800349 kunmap_atomic(kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 err = dir_commit_chunk(page, 0, 2 * sbi->s_dirsize);
352fail:
353 page_cache_release(page);
354 return err;
355}
356
357/*
358 * routine to check that the specified directory is empty (for rmdir)
359 */
360int minix_empty_dir(struct inode * inode)
361{
362 struct page *page = NULL;
363 unsigned long i, npages = dir_pages(inode);
364 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800365 char *name;
366 __u32 inumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 for (i = 0; i < npages; i++) {
Andries Brouwer939b00d2007-02-12 00:52:49 -0800369 char *p, *kaddr, *limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Andries Brouwer939b00d2007-02-12 00:52:49 -0800371 page = dir_get_page(inode, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (IS_ERR(page))
373 continue;
374
375 kaddr = (char *)page_address(page);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800376 limit = kaddr + minix_last_byte(inode, i) - sbi->s_dirsize;
377 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
378 if (sbi->s_version == MINIX_V3) {
379 minix3_dirent *de3 = (minix3_dirent *)p;
380 name = de3->name;
381 inumber = de3->inode;
382 } else {
383 minix_dirent *de = (minix_dirent *)p;
384 name = de->name;
385 inumber = de->inode;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Andries Brouwer939b00d2007-02-12 00:52:49 -0800388 if (inumber != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /* check for . and .. */
Andries Brouwer939b00d2007-02-12 00:52:49 -0800390 if (name[0] != '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 goto not_empty;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800392 if (!name[1]) {
393 if (inumber != inode->i_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 goto not_empty;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800395 } else if (name[1] != '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 goto not_empty;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800397 else if (name[2])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 goto not_empty;
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401 dir_put_page(page);
402 }
403 return 1;
404
405not_empty:
406 dir_put_page(page);
407 return 0;
408}
409
410/* Releases the page */
411void minix_set_link(struct minix_dir_entry *de, struct page *page,
412 struct inode *inode)
413{
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200414 struct inode *dir = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct minix_sb_info *sbi = minix_sb(dir->i_sb);
Nick Piggin4a66af92007-10-16 01:25:21 -0700416 loff_t pos = page_offset(page) +
417 (char *)de-(char*)page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 int err;
419
420 lock_page(page);
Nick Piggin4a66af92007-10-16 01:25:21 -0700421
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200422 err = minix_prepare_chunk(page, pos, sbi->s_dirsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (err == 0) {
Doug Graham9f6c1332009-09-22 16:48:00 -0700424 if (sbi->s_version == MINIX_V3)
425 ((minix3_dirent *) de)->inode = inode->i_ino;
426 else
427 de->inode = inode->i_ino;
Nick Piggin4a66af92007-10-16 01:25:21 -0700428 err = dir_commit_chunk(page, pos, sbi->s_dirsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 } else {
430 unlock_page(page);
431 }
432 dir_put_page(page);
433 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
434 mark_inode_dirty(dir);
435}
436
437struct minix_dir_entry * minix_dotdot (struct inode *dir, struct page **p)
438{
439 struct page *page = dir_get_page(dir, 0);
440 struct minix_sb_info *sbi = minix_sb(dir->i_sb);
441 struct minix_dir_entry *de = NULL;
442
443 if (!IS_ERR(page)) {
444 de = minix_next_entry(page_address(page), sbi);
445 *p = page;
446 }
447 return de;
448}
449
450ino_t minix_inode_by_name(struct dentry *dentry)
451{
452 struct page *page;
453 struct minix_dir_entry *de = minix_find_entry(dentry, &page);
454 ino_t res = 0;
455
456 if (de) {
Doug Graham9f6c1332009-09-22 16:48:00 -0700457 struct address_space *mapping = page->mapping;
458 struct inode *inode = mapping->host;
459 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
460
461 if (sbi->s_version == MINIX_V3)
462 res = ((minix3_dirent *) de)->inode;
463 else
464 res = de->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 dir_put_page(page);
466 }
467 return res;
468}