~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/nilfs2/dir.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0+
  2 /*
  3  * NILFS directory entry operations
  4  *
  5  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  6  *
  7  * Modified for NILFS by Amagai Yoshiji.
  8  */
  9 /*
 10  *  linux/fs/ext2/dir.c
 11  *
 12  * Copyright (C) 1992, 1993, 1994, 1995
 13  * Remy Card (card@masi.ibp.fr)
 14  * Laboratoire MASI - Institut Blaise Pascal
 15  * Universite Pierre et Marie Curie (Paris VI)
 16  *
 17  *  from
 18  *
 19  *  linux/fs/minix/dir.c
 20  *
 21  *  Copyright (C) 1991, 1992  Linus Torvalds
 22  *
 23  *  ext2 directory handling functions
 24  *
 25  *  Big-endian to little-endian byte-swapping/bitmaps by
 26  *        David S. Miller (davem@caip.rutgers.edu), 1995
 27  *
 28  * All code that works with directory layout had been switched to pagecache
 29  * and moved here. AV
 30  */
 31 
 32 #include <linux/pagemap.h>
 33 #include "nilfs.h"
 34 #include "page.h"
 35 
 36 static inline unsigned int nilfs_rec_len_from_disk(__le16 dlen)
 37 {
 38         unsigned int len = le16_to_cpu(dlen);
 39 
 40 #if (PAGE_SIZE >= 65536)
 41         if (len == NILFS_MAX_REC_LEN)
 42                 return 1 << 16;
 43 #endif
 44         return len;
 45 }
 46 
 47 static inline __le16 nilfs_rec_len_to_disk(unsigned int len)
 48 {
 49 #if (PAGE_SIZE >= 65536)
 50         if (len == (1 << 16))
 51                 return cpu_to_le16(NILFS_MAX_REC_LEN);
 52 
 53         BUG_ON(len > (1 << 16));
 54 #endif
 55         return cpu_to_le16(len);
 56 }
 57 
 58 /*
 59  * nilfs uses block-sized chunks. Arguably, sector-sized ones would be
 60  * more robust, but we have what we have
 61  */
 62 static inline unsigned int nilfs_chunk_size(struct inode *inode)
 63 {
 64         return inode->i_sb->s_blocksize;
 65 }
 66 
 67 /*
 68  * Return the offset into page `page_nr' of the last valid
 69  * byte in that page, plus one.
 70  */
 71 static unsigned int nilfs_last_byte(struct inode *inode, unsigned long page_nr)
 72 {
 73         unsigned int last_byte = inode->i_size;
 74 
 75         last_byte -= page_nr << PAGE_SHIFT;
 76         if (last_byte > PAGE_SIZE)
 77                 last_byte = PAGE_SIZE;
 78         return last_byte;
 79 }
 80 
 81 static int nilfs_prepare_chunk(struct folio *folio, unsigned int from,
 82                                unsigned int to)
 83 {
 84         loff_t pos = folio_pos(folio) + from;
 85 
 86         return __block_write_begin(&folio->page, pos, to - from, nilfs_get_block);
 87 }
 88 
 89 static void nilfs_commit_chunk(struct folio *folio,
 90                 struct address_space *mapping, size_t from, size_t to)
 91 {
 92         struct inode *dir = mapping->host;
 93         loff_t pos = folio_pos(folio) + from;
 94         size_t copied, len = to - from;
 95         unsigned int nr_dirty;
 96         int err;
 97 
 98         nr_dirty = nilfs_page_count_clean_buffers(&folio->page, from, to);
 99         copied = block_write_end(NULL, mapping, pos, len, len, &folio->page, NULL);
100         if (pos + copied > dir->i_size)
101                 i_size_write(dir, pos + copied);
102         if (IS_DIRSYNC(dir))
103                 nilfs_set_transaction_flag(NILFS_TI_SYNC);
104         err = nilfs_set_file_dirty(dir, nr_dirty);
105         WARN_ON(err); /* do not happen */
106         folio_unlock(folio);
107 }
108 
109 static bool nilfs_check_folio(struct folio *folio, char *kaddr)
110 {
111         struct inode *dir = folio->mapping->host;
112         struct super_block *sb = dir->i_sb;
113         unsigned int chunk_size = nilfs_chunk_size(dir);
114         size_t offs, rec_len;
115         size_t limit = folio_size(folio);
116         struct nilfs_dir_entry *p;
117         char *error;
118 
119         if (dir->i_size < folio_pos(folio) + limit) {
120                 limit = dir->i_size - folio_pos(folio);
121                 if (limit & (chunk_size - 1))
122                         goto Ebadsize;
123                 if (!limit)
124                         goto out;
125         }
126         for (offs = 0; offs <= limit - NILFS_DIR_REC_LEN(1); offs += rec_len) {
127                 p = (struct nilfs_dir_entry *)(kaddr + offs);
128                 rec_len = nilfs_rec_len_from_disk(p->rec_len);
129 
130                 if (rec_len < NILFS_DIR_REC_LEN(1))
131                         goto Eshort;
132                 if (rec_len & 3)
133                         goto Ealign;
134                 if (rec_len < NILFS_DIR_REC_LEN(p->name_len))
135                         goto Enamelen;
136                 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
137                         goto Espan;
138                 if (unlikely(p->inode &&
139                              NILFS_PRIVATE_INODE(le64_to_cpu(p->inode))))
140                         goto Einumber;
141         }
142         if (offs != limit)
143                 goto Eend;
144 out:
145         folio_set_checked(folio);
146         return true;
147 
148         /* Too bad, we had an error */
149 
150 Ebadsize:
151         nilfs_error(sb,
152                     "size of directory #%lu is not a multiple of chunk size",
153                     dir->i_ino);
154         goto fail;
155 Eshort:
156         error = "rec_len is smaller than minimal";
157         goto bad_entry;
158 Ealign:
159         error = "unaligned directory entry";
160         goto bad_entry;
161 Enamelen:
162         error = "rec_len is too small for name_len";
163         goto bad_entry;
164 Espan:
165         error = "directory entry across blocks";
166         goto bad_entry;
167 Einumber:
168         error = "disallowed inode number";
169 bad_entry:
170         nilfs_error(sb,
171                     "bad entry in directory #%lu: %s - offset=%lu, inode=%lu, rec_len=%zd, name_len=%d",
172                     dir->i_ino, error, (folio->index << PAGE_SHIFT) + offs,
173                     (unsigned long)le64_to_cpu(p->inode),
174                     rec_len, p->name_len);
175         goto fail;
176 Eend:
177         p = (struct nilfs_dir_entry *)(kaddr + offs);
178         nilfs_error(sb,
179                     "entry in directory #%lu spans the page boundary offset=%lu, inode=%lu",
180                     dir->i_ino, (folio->index << PAGE_SHIFT) + offs,
181                     (unsigned long)le64_to_cpu(p->inode));
182 fail:
183         return false;
184 }
185 
186 static void *nilfs_get_folio(struct inode *dir, unsigned long n,
187                 struct folio **foliop)
188 {
189         struct address_space *mapping = dir->i_mapping;
190         struct folio *folio = read_mapping_folio(mapping, n, NULL);
191         void *kaddr;
192 
193         if (IS_ERR(folio))
194                 return folio;
195 
196         kaddr = kmap_local_folio(folio, 0);
197         if (unlikely(!folio_test_checked(folio))) {
198                 if (!nilfs_check_folio(folio, kaddr))
199                         goto fail;
200         }
201 
202         *foliop = folio;
203         return kaddr;
204 
205 fail:
206         folio_release_kmap(folio, kaddr);
207         return ERR_PTR(-EIO);
208 }
209 
210 /*
211  * NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure.
212  *
213  * len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller.
214  */
215 static int
216 nilfs_match(int len, const unsigned char *name, struct nilfs_dir_entry *de)
217 {
218         if (len != de->name_len)
219                 return 0;
220         if (!de->inode)
221                 return 0;
222         return !memcmp(name, de->name, len);
223 }
224 
225 /*
226  * p is at least 6 bytes before the end of page
227  */
228 static struct nilfs_dir_entry *nilfs_next_entry(struct nilfs_dir_entry *p)
229 {
230         return (struct nilfs_dir_entry *)((char *)p +
231                                           nilfs_rec_len_from_disk(p->rec_len));
232 }
233 
234 static unsigned char
235 nilfs_filetype_table[NILFS_FT_MAX] = {
236         [NILFS_FT_UNKNOWN]      = DT_UNKNOWN,
237         [NILFS_FT_REG_FILE]     = DT_REG,
238         [NILFS_FT_DIR]          = DT_DIR,
239         [NILFS_FT_CHRDEV]       = DT_CHR,
240         [NILFS_FT_BLKDEV]       = DT_BLK,
241         [NILFS_FT_FIFO]         = DT_FIFO,
242         [NILFS_FT_SOCK]         = DT_SOCK,
243         [NILFS_FT_SYMLINK]      = DT_LNK,
244 };
245 
246 #define S_SHIFT 12
247 static unsigned char
248 nilfs_type_by_mode[(S_IFMT >> S_SHIFT) + 1] = {
249         [S_IFREG >> S_SHIFT]    = NILFS_FT_REG_FILE,
250         [S_IFDIR >> S_SHIFT]    = NILFS_FT_DIR,
251         [S_IFCHR >> S_SHIFT]    = NILFS_FT_CHRDEV,
252         [S_IFBLK >> S_SHIFT]    = NILFS_FT_BLKDEV,
253         [S_IFIFO >> S_SHIFT]    = NILFS_FT_FIFO,
254         [S_IFSOCK >> S_SHIFT]   = NILFS_FT_SOCK,
255         [S_IFLNK >> S_SHIFT]    = NILFS_FT_SYMLINK,
256 };
257 
258 static void nilfs_set_de_type(struct nilfs_dir_entry *de, struct inode *inode)
259 {
260         umode_t mode = inode->i_mode;
261 
262         de->file_type = nilfs_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
263 }
264 
265 static int nilfs_readdir(struct file *file, struct dir_context *ctx)
266 {
267         loff_t pos = ctx->pos;
268         struct inode *inode = file_inode(file);
269         struct super_block *sb = inode->i_sb;
270         unsigned int offset = pos & ~PAGE_MASK;
271         unsigned long n = pos >> PAGE_SHIFT;
272         unsigned long npages = dir_pages(inode);
273 
274         if (pos > inode->i_size - NILFS_DIR_REC_LEN(1))
275                 return 0;
276 
277         for ( ; n < npages; n++, offset = 0) {
278                 char *kaddr, *limit;
279                 struct nilfs_dir_entry *de;
280                 struct folio *folio;
281 
282                 kaddr = nilfs_get_folio(inode, n, &folio);
283                 if (IS_ERR(kaddr)) {
284                         nilfs_error(sb, "bad page in #%lu", inode->i_ino);
285                         ctx->pos += PAGE_SIZE - offset;
286                         return -EIO;
287                 }
288                 de = (struct nilfs_dir_entry *)(kaddr + offset);
289                 limit = kaddr + nilfs_last_byte(inode, n) -
290                         NILFS_DIR_REC_LEN(1);
291                 for ( ; (char *)de <= limit; de = nilfs_next_entry(de)) {
292                         if (de->rec_len == 0) {
293                                 nilfs_error(sb, "zero-length directory entry");
294                                 folio_release_kmap(folio, kaddr);
295                                 return -EIO;
296                         }
297                         if (de->inode) {
298                                 unsigned char t;
299 
300                                 if (de->file_type < NILFS_FT_MAX)
301                                         t = nilfs_filetype_table[de->file_type];
302                                 else
303                                         t = DT_UNKNOWN;
304 
305                                 if (!dir_emit(ctx, de->name, de->name_len,
306                                                 le64_to_cpu(de->inode), t)) {
307                                         folio_release_kmap(folio, kaddr);
308                                         return 0;
309                                 }
310                         }
311                         ctx->pos += nilfs_rec_len_from_disk(de->rec_len);
312                 }
313                 folio_release_kmap(folio, kaddr);
314         }
315         return 0;
316 }
317 
318 /*
319  * nilfs_find_entry()
320  *
321  * Finds an entry in the specified directory with the wanted name. It
322  * returns the folio in which the entry was found, and the entry itself.
323  * The folio is mapped and unlocked.  When the caller is finished with
324  * the entry, it should call folio_release_kmap().
325  *
326  * On failure, returns an error pointer and the caller should ignore foliop.
327  */
328 struct nilfs_dir_entry *nilfs_find_entry(struct inode *dir,
329                 const struct qstr *qstr, struct folio **foliop)
330 {
331         const unsigned char *name = qstr->name;
332         int namelen = qstr->len;
333         unsigned int reclen = NILFS_DIR_REC_LEN(namelen);
334         unsigned long start, n;
335         unsigned long npages = dir_pages(dir);
336         struct nilfs_inode_info *ei = NILFS_I(dir);
337         struct nilfs_dir_entry *de;
338 
339         if (npages == 0)
340                 goto out;
341 
342         start = ei->i_dir_start_lookup;
343         if (start >= npages)
344                 start = 0;
345         n = start;
346         do {
347                 char *kaddr = nilfs_get_folio(dir, n, foliop);
348 
349                 if (IS_ERR(kaddr))
350                         return ERR_CAST(kaddr);
351 
352                 de = (struct nilfs_dir_entry *)kaddr;
353                 kaddr += nilfs_last_byte(dir, n) - reclen;
354                 while ((char *)de <= kaddr) {
355                         if (de->rec_len == 0) {
356                                 nilfs_error(dir->i_sb,
357                                             "zero-length directory entry");
358                                 folio_release_kmap(*foliop, kaddr);
359                                 goto out;
360                         }
361                         if (nilfs_match(namelen, name, de))
362                                 goto found;
363                         de = nilfs_next_entry(de);
364                 }
365                 folio_release_kmap(*foliop, kaddr);
366 
367                 if (++n >= npages)
368                         n = 0;
369                 /* next folio is past the blocks we've got */
370                 if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
371                         nilfs_error(dir->i_sb,
372                                "dir %lu size %lld exceeds block count %llu",
373                                dir->i_ino, dir->i_size,
374                                (unsigned long long)dir->i_blocks);
375                         goto out;
376                 }
377         } while (n != start);
378 out:
379         return ERR_PTR(-ENOENT);
380 
381 found:
382         ei->i_dir_start_lookup = n;
383         return de;
384 }
385 
386 struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct folio **foliop)
387 {
388         struct folio *folio;
389         struct nilfs_dir_entry *de, *next_de;
390         size_t limit;
391         char *msg;
392 
393         de = nilfs_get_folio(dir, 0, &folio);
394         if (IS_ERR(de))
395                 return NULL;
396 
397         limit = nilfs_last_byte(dir, 0);  /* is a multiple of chunk size */
398         if (unlikely(!limit || le64_to_cpu(de->inode) != dir->i_ino ||
399                      !nilfs_match(1, ".", de))) {
400                 msg = "missing '.'";
401                 goto fail;
402         }
403 
404         next_de = nilfs_next_entry(de);
405         /*
406          * If "next_de" has not reached the end of the chunk, there is
407          * at least one more record.  Check whether it matches "..".
408          */
409         if (unlikely((char *)next_de == (char *)de + nilfs_chunk_size(dir) ||
410                      !nilfs_match(2, "..", next_de))) {
411                 msg = "missing '..'";
412                 goto fail;
413         }
414         *foliop = folio;
415         return next_de;
416 
417 fail:
418         nilfs_error(dir->i_sb, "directory #%lu %s", dir->i_ino, msg);
419         folio_release_kmap(folio, de);
420         return NULL;
421 }
422 
423 int nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr, ino_t *ino)
424 {
425         struct nilfs_dir_entry *de;
426         struct folio *folio;
427 
428         de = nilfs_find_entry(dir, qstr, &folio);
429         if (IS_ERR(de))
430                 return PTR_ERR(de);
431 
432         *ino = le64_to_cpu(de->inode);
433         folio_release_kmap(folio, de);
434         return 0;
435 }
436 
437 void nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de,
438                     struct folio *folio, struct inode *inode)
439 {
440         size_t from = offset_in_folio(folio, de);
441         size_t to = from + nilfs_rec_len_from_disk(de->rec_len);
442         struct address_space *mapping = folio->mapping;
443         int err;
444 
445         folio_lock(folio);
446         err = nilfs_prepare_chunk(folio, from, to);
447         BUG_ON(err);
448         de->inode = cpu_to_le64(inode->i_ino);
449         nilfs_set_de_type(de, inode);
450         nilfs_commit_chunk(folio, mapping, from, to);
451         inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
452 }
453 
454 /*
455  *      Parent is locked.
456  */
457 int nilfs_add_link(struct dentry *dentry, struct inode *inode)
458 {
459         struct inode *dir = d_inode(dentry->d_parent);
460         const unsigned char *name = dentry->d_name.name;
461         int namelen = dentry->d_name.len;
462         unsigned int chunk_size = nilfs_chunk_size(dir);
463         unsigned int reclen = NILFS_DIR_REC_LEN(namelen);
464         unsigned short rec_len, name_len;
465         struct folio *folio = NULL;
466         struct nilfs_dir_entry *de;
467         unsigned long npages = dir_pages(dir);
468         unsigned long n;
469         size_t from, to;
470         int err;
471 
472         /*
473          * We take care of directory expansion in the same loop.
474          * This code plays outside i_size, so it locks the folio
475          * to protect that region.
476          */
477         for (n = 0; n <= npages; n++) {
478                 char *kaddr = nilfs_get_folio(dir, n, &folio);
479                 char *dir_end;
480 
481                 if (IS_ERR(kaddr))
482                         return PTR_ERR(kaddr);
483                 folio_lock(folio);
484                 dir_end = kaddr + nilfs_last_byte(dir, n);
485                 de = (struct nilfs_dir_entry *)kaddr;
486                 kaddr += folio_size(folio) - reclen;
487                 while ((char *)de <= kaddr) {
488                         if ((char *)de == dir_end) {
489                                 /* We hit i_size */
490                                 name_len = 0;
491                                 rec_len = chunk_size;
492                                 de->rec_len = nilfs_rec_len_to_disk(chunk_size);
493                                 de->inode = 0;
494                                 goto got_it;
495                         }
496                         if (de->rec_len == 0) {
497                                 nilfs_error(dir->i_sb,
498                                             "zero-length directory entry");
499                                 err = -EIO;
500                                 goto out_unlock;
501                         }
502                         err = -EEXIST;
503                         if (nilfs_match(namelen, name, de))
504                                 goto out_unlock;
505                         name_len = NILFS_DIR_REC_LEN(de->name_len);
506                         rec_len = nilfs_rec_len_from_disk(de->rec_len);
507                         if (!de->inode && rec_len >= reclen)
508                                 goto got_it;
509                         if (rec_len >= name_len + reclen)
510                                 goto got_it;
511                         de = (struct nilfs_dir_entry *)((char *)de + rec_len);
512                 }
513                 folio_unlock(folio);
514                 folio_release_kmap(folio, kaddr);
515         }
516         BUG();
517         return -EINVAL;
518 
519 got_it:
520         from = offset_in_folio(folio, de);
521         to = from + rec_len;
522         err = nilfs_prepare_chunk(folio, from, to);
523         if (err)
524                 goto out_unlock;
525         if (de->inode) {
526                 struct nilfs_dir_entry *de1;
527 
528                 de1 = (struct nilfs_dir_entry *)((char *)de + name_len);
529                 de1->rec_len = nilfs_rec_len_to_disk(rec_len - name_len);
530                 de->rec_len = nilfs_rec_len_to_disk(name_len);
531                 de = de1;
532         }
533         de->name_len = namelen;
534         memcpy(de->name, name, namelen);
535         de->inode = cpu_to_le64(inode->i_ino);
536         nilfs_set_de_type(de, inode);
537         nilfs_commit_chunk(folio, folio->mapping, from, to);
538         inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
539         nilfs_mark_inode_dirty(dir);
540         /* OFFSET_CACHE */
541 out_put:
542         folio_release_kmap(folio, de);
543         return err;
544 out_unlock:
545         folio_unlock(folio);
546         goto out_put;
547 }
548 
549 /*
550  * nilfs_delete_entry deletes a directory entry by merging it with the
551  * previous entry. Folio is up-to-date.
552  */
553 int nilfs_delete_entry(struct nilfs_dir_entry *dir, struct folio *folio)
554 {
555         struct address_space *mapping = folio->mapping;
556         struct inode *inode = mapping->host;
557         char *kaddr = (char *)((unsigned long)dir & ~(folio_size(folio) - 1));
558         size_t from, to;
559         struct nilfs_dir_entry *de, *pde = NULL;
560         int err;
561 
562         from = ((char *)dir - kaddr) & ~(nilfs_chunk_size(inode) - 1);
563         to = ((char *)dir - kaddr) + nilfs_rec_len_from_disk(dir->rec_len);
564         de = (struct nilfs_dir_entry *)(kaddr + from);
565 
566         while ((char *)de < (char *)dir) {
567                 if (de->rec_len == 0) {
568                         nilfs_error(inode->i_sb,
569                                     "zero-length directory entry");
570                         err = -EIO;
571                         goto out;
572                 }
573                 pde = de;
574                 de = nilfs_next_entry(de);
575         }
576         if (pde)
577                 from = (char *)pde - kaddr;
578         folio_lock(folio);
579         err = nilfs_prepare_chunk(folio, from, to);
580         BUG_ON(err);
581         if (pde)
582                 pde->rec_len = nilfs_rec_len_to_disk(to - from);
583         dir->inode = 0;
584         nilfs_commit_chunk(folio, mapping, from, to);
585         inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
586 out:
587         return err;
588 }
589 
590 /*
591  * Set the first fragment of directory.
592  */
593 int nilfs_make_empty(struct inode *inode, struct inode *parent)
594 {
595         struct address_space *mapping = inode->i_mapping;
596         struct folio *folio = filemap_grab_folio(mapping, 0);
597         unsigned int chunk_size = nilfs_chunk_size(inode);
598         struct nilfs_dir_entry *de;
599         int err;
600         void *kaddr;
601 
602         if (IS_ERR(folio))
603                 return PTR_ERR(folio);
604 
605         err = nilfs_prepare_chunk(folio, 0, chunk_size);
606         if (unlikely(err)) {
607                 folio_unlock(folio);
608                 goto fail;
609         }
610         kaddr = kmap_local_folio(folio, 0);
611         memset(kaddr, 0, chunk_size);
612         de = (struct nilfs_dir_entry *)kaddr;
613         de->name_len = 1;
614         de->rec_len = nilfs_rec_len_to_disk(NILFS_DIR_REC_LEN(1));
615         memcpy(de->name, ".\0\0", 4);
616         de->inode = cpu_to_le64(inode->i_ino);
617         nilfs_set_de_type(de, inode);
618 
619         de = (struct nilfs_dir_entry *)(kaddr + NILFS_DIR_REC_LEN(1));
620         de->name_len = 2;
621         de->rec_len = nilfs_rec_len_to_disk(chunk_size - NILFS_DIR_REC_LEN(1));
622         de->inode = cpu_to_le64(parent->i_ino);
623         memcpy(de->name, "..\0", 4);
624         nilfs_set_de_type(de, inode);
625         kunmap_local(kaddr);
626         nilfs_commit_chunk(folio, mapping, 0, chunk_size);
627 fail:
628         folio_put(folio);
629         return err;
630 }
631 
632 /*
633  * routine to check that the specified directory is empty (for rmdir)
634  */
635 int nilfs_empty_dir(struct inode *inode)
636 {
637         struct folio *folio = NULL;
638         char *kaddr;
639         unsigned long i, npages = dir_pages(inode);
640 
641         for (i = 0; i < npages; i++) {
642                 struct nilfs_dir_entry *de;
643 
644                 kaddr = nilfs_get_folio(inode, i, &folio);
645                 if (IS_ERR(kaddr))
646                         return 0;
647 
648                 de = (struct nilfs_dir_entry *)kaddr;
649                 kaddr += nilfs_last_byte(inode, i) - NILFS_DIR_REC_LEN(1);
650 
651                 while ((char *)de <= kaddr) {
652                         if (de->rec_len == 0) {
653                                 nilfs_error(inode->i_sb,
654                                             "zero-length directory entry (kaddr=%p, de=%p)",
655                                             kaddr, de);
656                                 goto not_empty;
657                         }
658                         if (de->inode != 0) {
659                                 /* check for . and .. */
660                                 if (de->name[0] != '.')
661                                         goto not_empty;
662                                 if (de->name_len > 2)
663                                         goto not_empty;
664                                 if (de->name_len < 2) {
665                                         if (de->inode !=
666                                             cpu_to_le64(inode->i_ino))
667                                                 goto not_empty;
668                                 } else if (de->name[1] != '.')
669                                         goto not_empty;
670                         }
671                         de = nilfs_next_entry(de);
672                 }
673                 folio_release_kmap(folio, kaddr);
674         }
675         return 1;
676 
677 not_empty:
678         folio_release_kmap(folio, kaddr);
679         return 0;
680 }
681 
682 const struct file_operations nilfs_dir_operations = {
683         .llseek         = generic_file_llseek,
684         .read           = generic_read_dir,
685         .iterate_shared = nilfs_readdir,
686         .unlocked_ioctl = nilfs_ioctl,
687 #ifdef CONFIG_COMPAT
688         .compat_ioctl   = nilfs_compat_ioctl,
689 #endif  /* CONFIG_COMPAT */
690         .fsync          = nilfs_sync_file,
691 
692 };
693 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php