blob: 88e38cd8f5c9ae435979a71c367d22f65de26759 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/sysv/dir.c
4 *
5 * minix/dir.c
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 *
8 * coh/dir.c
9 * Copyright (C) 1993 Pascal Haible, Bruno Haible
10 *
11 * sysv/dir.c
12 * Copyright (C) 1993 Bruno Haible
13 *
14 * SystemV/Coherent directory handling functions
15 */
16
17#include <linux/pagemap.h>
18#include <linux/highmem.h>
Nick Piggin26a64412007-10-16 01:25:21 -070019#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "sysv.h"
21
Al Viro80886292013-05-15 18:51:49 -040022static int sysv_readdir(struct file *, struct dir_context *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080024const struct file_operations sysv_dir_operations = {
Al Viro5ac34552009-06-16 23:59:37 -040025 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 .read = generic_read_dir,
Al Viro3b0a3c12016-04-20 23:42:46 -040027 .iterate_shared = sysv_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +020028 .fsync = generic_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029};
30
31static inline void dir_put_page(struct page *page)
32{
33 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030034 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
Nick Piggin26a64412007-10-16 01:25:21 -070037static int dir_commit_chunk(struct page *page, loff_t pos, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Nick Piggin26a64412007-10-16 01:25:21 -070039 struct address_space *mapping = page->mapping;
40 struct inode *dir = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 int err = 0;
42
Nick Piggin26a64412007-10-16 01:25:21 -070043 block_write_end(NULL, mapping, pos, len, len, page, NULL);
44 if (pos+len > dir->i_size) {
45 i_size_write(dir, pos+len);
46 mark_inode_dirty(dir);
47 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 if (IS_DIRSYNC(dir))
Jeff Layton2b69c822017-07-05 15:26:48 -040049 err = write_one_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 else
51 unlock_page(page);
52 return err;
53}
54
55static struct page * dir_get_page(struct inode *dir, unsigned long n)
56{
57 struct address_space *mapping = dir->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -070058 struct page *page = read_mapping_page(mapping, n, NULL);
Nick Piggin6fe69002007-05-06 14:49:04 -070059 if (!IS_ERR(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
Al Viro80886292013-05-15 18:51:49 -040064static int sysv_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Al Viro80886292013-05-15 18:51:49 -040066 unsigned long pos = ctx->pos;
67 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 unsigned long npages = dir_pages(inode);
Al Viro80886292013-05-15 18:51:49 -040070 unsigned offset;
71 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Al Viro80886292013-05-15 18:51:49 -040073 ctx->pos = pos = (pos + SYSV_DIRSIZE-1) & ~(SYSV_DIRSIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (pos >= inode->i_size)
Al Viro80886292013-05-15 18:51:49 -040075 return 0;
76
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030077 offset = pos & ~PAGE_MASK;
78 n = pos >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 for ( ; n < npages; n++, offset = 0) {
81 char *kaddr, *limit;
82 struct sysv_dir_entry *de;
83 struct page *page = dir_get_page(inode, n);
84
85 if (IS_ERR(page))
86 continue;
87 kaddr = (char *)page_address(page);
88 de = (struct sysv_dir_entry *)(kaddr+offset);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030089 limit = kaddr + PAGE_SIZE - SYSV_DIRSIZE;
Al Viro80886292013-05-15 18:51:49 -040090 for ( ;(char*)de <= limit; de++, ctx->pos += sizeof(*de)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 char *name = de->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 if (!de->inode)
94 continue;
95
Al Viro80886292013-05-15 18:51:49 -040096 if (!dir_emit(ctx, name, strnlen(name,SYSV_NAMELEN),
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 fs16_to_cpu(SYSV_SB(sb), de->inode),
Al Viro80886292013-05-15 18:51:49 -040098 DT_UNKNOWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 dir_put_page(page);
Al Viro80886292013-05-15 18:51:49 -0400100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102 }
103 dir_put_page(page);
104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 0;
106}
107
108/* compare strings: name[0..len-1] (not zero-terminated) and
109 * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1])
110 */
111static inline int namecompare(int len, int maxlen,
112 const char * name, const char * buffer)
113{
114 if (len < maxlen && buffer[len])
115 return 0;
116 return !memcmp(name, buffer, len);
117}
118
119/*
120 * sysv_find_entry()
121 *
122 * finds an entry in the specified directory with the wanted name. It
123 * returns the cache buffer in which the entry was found, and the entry
124 * itself (as a parameter - res_dir). It does NOT read the inode of the
125 * entry - you'll have to do that yourself if you want to.
126 */
127struct sysv_dir_entry *sysv_find_entry(struct dentry *dentry, struct page **res_page)
128{
129 const char * name = dentry->d_name.name;
130 int namelen = dentry->d_name.len;
David Howells2b0143b2015-03-17 22:25:59 +0000131 struct inode * dir = d_inode(dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unsigned long start, n;
133 unsigned long npages = dir_pages(dir);
134 struct page *page = NULL;
135 struct sysv_dir_entry *de;
136
137 *res_page = NULL;
138
139 start = SYSV_I(dir)->i_dir_start_lookup;
140 if (start >= npages)
141 start = 0;
142 n = start;
143
144 do {
145 char *kaddr;
146 page = dir_get_page(dir, n);
147 if (!IS_ERR(page)) {
148 kaddr = (char*)page_address(page);
149 de = (struct sysv_dir_entry *) kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300150 kaddr += PAGE_SIZE - SYSV_DIRSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 for ( ; (char *) de <= kaddr ; de++) {
152 if (!de->inode)
153 continue;
154 if (namecompare(namelen, SYSV_NAMELEN,
155 name, de->name))
156 goto found;
157 }
Dan Carpenter404e7812010-04-21 12:30:32 +0200158 dir_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 if (++n >= npages)
162 n = 0;
163 } while (n != start);
164
165 return NULL;
166
167found:
168 SYSV_I(dir)->i_dir_start_lookup = n;
169 *res_page = page;
170 return de;
171}
172
173int sysv_add_link(struct dentry *dentry, struct inode *inode)
174{
David Howells2b0143b2015-03-17 22:25:59 +0000175 struct inode *dir = d_inode(dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 const char * name = dentry->d_name.name;
177 int namelen = dentry->d_name.len;
178 struct page *page = NULL;
179 struct sysv_dir_entry * de;
180 unsigned long npages = dir_pages(dir);
181 unsigned long n;
182 char *kaddr;
Nick Piggin26a64412007-10-16 01:25:21 -0700183 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int err;
185
186 /* We take care of directory expansion in the same loop */
187 for (n = 0; n <= npages; n++) {
188 page = dir_get_page(dir, n);
189 err = PTR_ERR(page);
190 if (IS_ERR(page))
191 goto out;
192 kaddr = (char*)page_address(page);
193 de = (struct sysv_dir_entry *)kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300194 kaddr += PAGE_SIZE - SYSV_DIRSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 while ((char *)de <= kaddr) {
196 if (!de->inode)
197 goto got_it;
198 err = -EEXIST;
199 if (namecompare(namelen, SYSV_NAMELEN, name, de->name))
200 goto out_page;
201 de++;
202 }
203 dir_put_page(page);
204 }
205 BUG();
206 return -EINVAL;
207
208got_it:
Nick Piggin26a64412007-10-16 01:25:21 -0700209 pos = page_offset(page) +
210 (char*)de - (char*)page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200212 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (err)
214 goto out_unlock;
215 memcpy (de->name, name, namelen);
216 memset (de->name + namelen, 0, SYSV_DIRSIZE - namelen - 2);
217 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
Nick Piggin26a64412007-10-16 01:25:21 -0700218 err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700219 dir->i_mtime = dir->i_ctime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 mark_inode_dirty(dir);
221out_page:
222 dir_put_page(page);
223out:
224 return err;
225out_unlock:
226 unlock_page(page);
227 goto out_page;
228}
229
230int sysv_delete_entry(struct sysv_dir_entry *de, struct page *page)
231{
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200232 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 char *kaddr = (char*)page_address(page);
Nick Piggin26a64412007-10-16 01:25:21 -0700234 loff_t pos = page_offset(page) + (char *)de - kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 int err;
236
237 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200238 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
Eric Sesterhennd6735bf2006-04-02 13:39:21 +0200239 BUG_ON(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 de->inode = 0;
Nick Piggin26a64412007-10-16 01:25:21 -0700241 err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 dir_put_page(page);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700243 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 mark_inode_dirty(inode);
245 return err;
246}
247
248int sysv_make_empty(struct inode *inode, struct inode *dir)
249{
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200250 struct page *page = grab_cache_page(inode->i_mapping, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct sysv_dir_entry * de;
252 char *base;
253 int err;
254
255 if (!page)
256 return -ENOMEM;
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200257 err = sysv_prepare_chunk(page, 0, 2 * SYSV_DIRSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (err) {
259 unlock_page(page);
260 goto fail;
261 }
Nick Piggin26a64412007-10-16 01:25:21 -0700262 kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 base = (char*)page_address(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300265 memset(base, 0, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 de = (struct sysv_dir_entry *) base;
268 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
269 strcpy(de->name,".");
270 de++;
271 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), dir->i_ino);
272 strcpy(de->name,"..");
273
Nick Piggin26a64412007-10-16 01:25:21 -0700274 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 err = dir_commit_chunk(page, 0, 2 * SYSV_DIRSIZE);
276fail:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300277 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return err;
279}
280
281/*
282 * routine to check that the specified directory is empty (for rmdir)
283 */
284int sysv_empty_dir(struct inode * inode)
285{
286 struct super_block *sb = inode->i_sb;
287 struct page *page = NULL;
288 unsigned long i, npages = dir_pages(inode);
289
290 for (i = 0; i < npages; i++) {
291 char *kaddr;
292 struct sysv_dir_entry * de;
293 page = dir_get_page(inode, i);
294
295 if (IS_ERR(page))
296 continue;
297
298 kaddr = (char *)page_address(page);
299 de = (struct sysv_dir_entry *)kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300300 kaddr += PAGE_SIZE-SYSV_DIRSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 for ( ;(char *)de <= kaddr; de++) {
303 if (!de->inode)
304 continue;
305 /* check for . and .. */
306 if (de->name[0] != '.')
307 goto not_empty;
308 if (!de->name[1]) {
309 if (de->inode == cpu_to_fs16(SYSV_SB(sb),
310 inode->i_ino))
311 continue;
312 goto not_empty;
313 }
314 if (de->name[1] != '.' || de->name[2])
315 goto not_empty;
316 }
317 dir_put_page(page);
318 }
319 return 1;
320
321not_empty:
322 dir_put_page(page);
323 return 0;
324}
325
326/* Releases the page */
327void sysv_set_link(struct sysv_dir_entry *de, struct page *page,
328 struct inode *inode)
329{
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200330 struct inode *dir = page->mapping->host;
Nick Piggin26a64412007-10-16 01:25:21 -0700331 loff_t pos = page_offset(page) +
332 (char *)de-(char*)page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int err;
334
335 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200336 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
Eric Sesterhennd6735bf2006-04-02 13:39:21 +0200337 BUG_ON(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
Nick Piggin26a64412007-10-16 01:25:21 -0700339 err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 dir_put_page(page);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700341 dir->i_mtime = dir->i_ctime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 mark_inode_dirty(dir);
343}
344
345struct sysv_dir_entry * sysv_dotdot (struct inode *dir, struct page **p)
346{
347 struct page *page = dir_get_page(dir, 0);
348 struct sysv_dir_entry *de = NULL;
349
350 if (!IS_ERR(page)) {
351 de = (struct sysv_dir_entry*) page_address(page) + 1;
352 *p = page;
353 }
354 return de;
355}
356
357ino_t sysv_inode_by_name(struct dentry *dentry)
358{
359 struct page *page;
360 struct sysv_dir_entry *de = sysv_find_entry (dentry, &page);
361 ino_t res = 0;
362
363 if (de) {
364 res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode);
365 dir_put_page(page);
366 }
367 return res;
368}