Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright (C) 2011 Novell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/rbtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include "overlayfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) struct ovl_cache_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 	unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 	unsigned int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 	u64 real_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 	u64 ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 	struct list_head l_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 	struct rb_node node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 	struct ovl_cache_entry *next_maybe_whiteout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 	bool is_upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 	bool is_whiteout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 	char name[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) struct ovl_dir_cache {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	long refcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	u64 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	struct list_head entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	struct rb_root root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) struct ovl_readdir_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	struct dir_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	bool is_lowest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	struct rb_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	struct list_head *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	struct list_head middle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	struct ovl_cache_entry *first_maybe_whiteout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	bool is_upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	bool d_type_supported;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) struct ovl_dir_file {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	bool is_real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	bool is_upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	struct ovl_dir_cache *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	struct list_head *cursor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	struct file *realfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	struct file *upperfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	return rb_entry(n, struct ovl_cache_entry, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) static bool ovl_cache_entry_find_link(const char *name, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 				      struct rb_node ***link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 				      struct rb_node **parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	struct rb_node **newp = *link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	while (!found && *newp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 		int cmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		struct ovl_cache_entry *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 		*parent = *newp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		tmp = ovl_cache_entry_from_node(*newp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 		cmp = strncmp(name, tmp->name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 		if (cmp > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 			newp = &tmp->node.rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 		else if (cmp < 0 || len < tmp->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 			newp = &tmp->node.rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 			found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	*link = newp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 						    const char *name, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	struct rb_node *node = root->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	int cmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 		cmp = strncmp(name, p->name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 		if (cmp > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 			node = p->node.rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		else if (cmp < 0 || len < p->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 			node = p->node.rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 			return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) static bool ovl_calc_d_ino(struct ovl_readdir_data *rdd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 			   struct ovl_cache_entry *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	/* Don't care if not doing ovl_iter() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	if (!rdd->dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	/* Always recalc d_ino when remapping lower inode numbers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	if (ovl_xino_bits(rdd->dentry->d_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	/* Always recalc d_ino for parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	if (strcmp(p->name, "..") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	/* If this is lower, then native d_ino will do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	if (!rdd->is_upper)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	 * Recalc d_ino for '.' and for all entries if dir is impure (contains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	 * copied up entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	if ((p->name[0] == '.' && p->len == 1) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	    ovl_test_flag(OVL_IMPURE, d_inode(rdd->dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 						   const char *name, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 						   u64 ino, unsigned int d_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	struct ovl_cache_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	p = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	memcpy(p->name, name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	p->name[len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	p->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	p->type = d_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	p->real_ino = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	p->ino = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	/* Defer setting d_ino for upper entry to ovl_iterate() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	if (ovl_calc_d_ino(rdd, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 		p->ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	p->is_upper = rdd->is_upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	p->is_whiteout = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	if (d_type == DT_CHR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		p->next_maybe_whiteout = rdd->first_maybe_whiteout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		rdd->first_maybe_whiteout = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 				  const char *name, int len, u64 ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 				  unsigned int d_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	struct rb_node **newp = &rdd->root->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	struct ovl_cache_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	if (ovl_cache_entry_find_link(name, len, &newp, &parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	p = ovl_cache_entry_new(rdd, name, len, ino, d_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	if (p == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		rdd->err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	list_add_tail(&p->l_node, rdd->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	rb_link_node(&p->node, parent, newp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	rb_insert_color(&p->node, rdd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) static int ovl_fill_lowest(struct ovl_readdir_data *rdd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 			   const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 			   loff_t offset, u64 ino, unsigned int d_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	struct ovl_cache_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	p = ovl_cache_entry_find(rdd->root, name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		list_move_tail(&p->l_node, &rdd->middle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		if (p == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 			rdd->err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 			list_add_tail(&p->l_node, &rdd->middle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	return rdd->err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) void ovl_cache_free(struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	struct ovl_cache_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	struct ovl_cache_entry *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	list_for_each_entry_safe(p, n, list, l_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	INIT_LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) void ovl_dir_cache_free(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	struct ovl_dir_cache *cache = ovl_dir_cache(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	if (cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		ovl_cache_free(&cache->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		kfree(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	struct ovl_dir_cache *cache = od->cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	WARN_ON(cache->refcount <= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	cache->refcount--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	if (!cache->refcount) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		if (ovl_dir_cache(d_inode(dentry)) == cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 			ovl_set_dir_cache(d_inode(dentry), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		ovl_cache_free(&cache->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		kfree(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) static int ovl_fill_merge(struct dir_context *ctx, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 			  int namelen, loff_t offset, u64 ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 			  unsigned int d_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	struct ovl_readdir_data *rdd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		container_of(ctx, struct ovl_readdir_data, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	rdd->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	if (!rdd->is_lowest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	struct ovl_cache_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	old_cred = ovl_override_creds(rdd->dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	err = down_write_killable(&dir->d_inode->i_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		while (rdd->first_maybe_whiteout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 			p = rdd->first_maybe_whiteout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 			rdd->first_maybe_whiteout = p->next_maybe_whiteout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 			dentry = lookup_one_len(p->name, dir, p->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 			if (!IS_ERR(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 				p->is_whiteout = ovl_is_whiteout(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 				dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		inode_unlock(dir->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	ovl_revert_creds(rdd->dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) static inline int ovl_dir_read(struct path *realpath,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 			       struct ovl_readdir_data *rdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	struct file *realfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	realfile = ovl_path_open(realpath, O_RDONLY | O_LARGEFILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	if (IS_ERR(realfile))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		return PTR_ERR(realfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	rdd->first_maybe_whiteout = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	rdd->ctx.pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		rdd->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		rdd->err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		err = iterate_dir(realfile, &rdd->ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		if (err >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 			err = rdd->err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	} while (!err && rdd->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	if (!err && rdd->first_maybe_whiteout && rdd->dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		err = ovl_check_whiteouts(realpath->dentry, rdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	fput(realfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) static void ovl_dir_reset(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	struct ovl_dir_file *od = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	struct ovl_dir_cache *cache = od->cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	struct dentry *dentry = file->f_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	bool is_real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	if (cache && ovl_dentry_version_get(dentry) != cache->version) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		ovl_cache_put(od, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		od->cache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		od->cursor = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	is_real = ovl_dir_is_real(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	if (od->is_real != is_real) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		/* is_real can only become false when dir is copied up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		if (WARN_ON(is_real))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		od->is_real = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	struct rb_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	struct path realpath;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	struct ovl_readdir_data rdd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		.ctx.actor = ovl_fill_merge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		.dentry = dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		.list = list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		.root = root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		.is_lowest = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	int idx, next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	for (idx = 0; idx != -1; idx = next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		next = ovl_path_next(idx, dentry, &realpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		rdd.is_upper = ovl_dentry_upper(dentry) == realpath.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		if (next != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 			err = ovl_dir_read(&realpath, &rdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 			 * Insert lowest layer entries before upper ones, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 			 * allows offsets to be reasonably constant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 			list_add(&rdd.middle, rdd.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 			rdd.is_lowest = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 			err = ovl_dir_read(&realpath, &rdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 			list_del(&rdd.middle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	struct list_head *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	loff_t off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	list_for_each(p, &od->cache->entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		if (off >= pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		off++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	/* Cursor is safe since the cache is stable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	od->cursor = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	struct ovl_dir_cache *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	cache = ovl_dir_cache(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	if (cache && ovl_dentry_version_get(dentry) == cache->version) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		WARN_ON(!cache->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		cache->refcount++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		return cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	ovl_set_dir_cache(d_inode(dentry), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	if (!cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	cache->refcount = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	INIT_LIST_HEAD(&cache->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	cache->root = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	res = ovl_dir_read_merged(dentry, &cache->entries, &cache->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		ovl_cache_free(&cache->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		kfree(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		return ERR_PTR(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	cache->version = ovl_dentry_version_get(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	ovl_set_dir_cache(d_inode(dentry), cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	return cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) /* Map inode number to lower fs unique range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) static u64 ovl_remap_lower_ino(u64 ino, int xinobits, int fsid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 			       const char *name, int namelen, bool warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	unsigned int xinoshift = 64 - xinobits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	if (unlikely(ino >> xinoshift)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		if (warn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			pr_warn_ratelimited("d_ino too big (%.*s, ino=%llu, xinobits=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 					    namelen, name, ino, xinobits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		return ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	 * The lowest xinobit is reserved for mapping the non-peresistent inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	 * numbers range, but this range is only exposed via st_ino, not here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	return ino | ((u64)fsid) << (xinoshift + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449)  * Set d_ino for upper entries. Non-upper entries should always report
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  * the uppermost real inode ino and should not call this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452)  * When not all layer are on same fs, report real ino also for upper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454)  * When all layers are on the same fs, and upper has a reference to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455)  * copy up origin, call vfs_getattr() on the overlay entry to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456)  * sure that d_ino will be consistent with st_ino from stat(2).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) static int ovl_cache_update_ino(struct path *path, struct ovl_cache_entry *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	struct dentry *dir = path->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	struct dentry *this = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	enum ovl_path_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	u64 ino = p->real_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	int xinobits = ovl_xino_bits(dir->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	if (!ovl_same_dev(dir->d_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	if (p->name[0] == '.') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		if (p->len == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 			this = dget(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 			goto get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		if (p->len == 2 && p->name[1] == '.') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 			/* we shall not be moved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 			this = dget(dir->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 			goto get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	this = lookup_one_len(p->name, dir, p->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	if (IS_ERR_OR_NULL(this) || !this->d_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		if (IS_ERR(this)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 			err = PTR_ERR(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 			this = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) get:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	type = ovl_path_type(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	if (OVL_TYPE_ORIGIN(type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		struct kstat stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		struct path statpath = *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		statpath.dentry = this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		 * Directory inode is always on overlay st_dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		 * Non-dir with ovl_same_dev() could be on pseudo st_dev in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		 * of xino bits overflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		WARN_ON_ONCE(S_ISDIR(stat.mode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 			     dir->d_sb->s_dev != stat.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		ino = stat.ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	} else if (xinobits && !OVL_TYPE_UPPER(type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		ino = ovl_remap_lower_ino(ino, xinobits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 					  ovl_layer_lower(this)->fsid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 					  p->name, p->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 					  ovl_xino_warn(dir->d_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	p->ino = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	dput(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	pr_warn_ratelimited("failed to look up (%s) for ino (%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 			    p->name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) static int ovl_fill_plain(struct dir_context *ctx, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 			  int namelen, loff_t offset, u64 ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 			  unsigned int d_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	struct ovl_cache_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	struct ovl_readdir_data *rdd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		container_of(ctx, struct ovl_readdir_data, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	rdd->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	if (p == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		rdd->err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	list_add_tail(&p->l_node, rdd->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) static int ovl_dir_read_impure(struct path *path,  struct list_head *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 			       struct rb_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	struct path realpath;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	struct ovl_cache_entry *p, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	struct ovl_readdir_data rdd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		.ctx.actor = ovl_fill_plain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		.list = list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		.root = root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	INIT_LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	*root = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	ovl_path_upper(path->dentry, &realpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	err = ovl_dir_read(&realpath, &rdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	list_for_each_entry_safe(p, n, list, l_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		if (strcmp(p->name, ".") != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		    strcmp(p->name, "..") != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 			err = ovl_cache_update_ino(path, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		if (p->ino == p->real_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 			list_del(&p->l_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 			kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 			struct rb_node **newp = &root->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 			struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 			if (WARN_ON(ovl_cache_entry_find_link(p->name, p->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 							      &newp, &parent)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 			rb_link_node(&p->node, parent, newp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 			rb_insert_color(&p->node, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) static struct ovl_dir_cache *ovl_cache_get_impure(struct path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	struct dentry *dentry = path->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	struct ovl_dir_cache *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	cache = ovl_dir_cache(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	if (cache && ovl_dentry_version_get(dentry) == cache->version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		return cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	/* Impure cache is not refcounted, free it here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	ovl_dir_cache_free(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	ovl_set_dir_cache(d_inode(dentry), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	if (!cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	res = ovl_dir_read_impure(path, &cache->entries, &cache->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		ovl_cache_free(&cache->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		kfree(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		return ERR_PTR(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	if (list_empty(&cache->entries)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		 * A good opportunity to get rid of an unneeded "impure" flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		 * Removing the "impure" xattr is best effort.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		if (!ovl_want_write(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 			ovl_do_removexattr(ofs, ovl_dentry_upper(dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 					   OVL_XATTR_IMPURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 			ovl_drop_write(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		ovl_clear_flag(OVL_IMPURE, d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		kfree(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	cache->version = ovl_dentry_version_get(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	ovl_set_dir_cache(d_inode(dentry), cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	return cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) struct ovl_readdir_translate {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	struct dir_context *orig_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	struct ovl_dir_cache *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	struct dir_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	u64 parent_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	int fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	int xinobits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	bool xinowarn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) static int ovl_fill_real(struct dir_context *ctx, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 			   int namelen, loff_t offset, u64 ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 			   unsigned int d_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	struct ovl_readdir_translate *rdt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		container_of(ctx, struct ovl_readdir_translate, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	struct dir_context *orig_ctx = rdt->orig_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	if (rdt->parent_ino && strcmp(name, "..") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		ino = rdt->parent_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	} else if (rdt->cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		struct ovl_cache_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		p = ovl_cache_entry_find(&rdt->cache->root, name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 			ino = p->ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	} else if (rdt->xinobits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		ino = ovl_remap_lower_ino(ino, rdt->xinobits, rdt->fsid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 					  name, namelen, rdt->xinowarn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	return orig_ctx->actor(orig_ctx, name, namelen, offset, ino, d_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) static bool ovl_is_impure_dir(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	struct ovl_dir_file *od = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	struct inode *dir = d_inode(file->f_path.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	 * Only upper dir can be impure, but if we are in the middle of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	 * iterating a lower real dir, dir could be copied up and marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	 * impure. We only want the impure cache if we started iterating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	 * a real upper dir to begin with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	return od->is_upper && ovl_test_flag(OVL_IMPURE, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) static int ovl_iterate_real(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	struct ovl_dir_file *od = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	struct dentry *dir = file->f_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	const struct ovl_layer *lower_layer = ovl_layer_lower(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	struct ovl_readdir_translate rdt = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		.ctx.actor = ovl_fill_real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		.orig_ctx = ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		.xinobits = ovl_xino_bits(dir->d_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		.xinowarn = ovl_xino_warn(dir->d_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	if (rdt.xinobits && lower_layer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		rdt.fsid = lower_layer->fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	if (OVL_TYPE_MERGE(ovl_path_type(dir->d_parent))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		struct kstat stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		struct path statpath = file->f_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		statpath.dentry = dir->d_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		rdt.parent_ino = stat.ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	if (ovl_is_impure_dir(file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		rdt.cache = ovl_cache_get_impure(&file->f_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		if (IS_ERR(rdt.cache))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 			return PTR_ERR(rdt.cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	err = iterate_dir(od->realfile, &rdt.ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	ctx->pos = rdt.ctx.pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) static int ovl_iterate(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	struct ovl_dir_file *od = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	struct dentry *dentry = file->f_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	struct ovl_cache_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	old_cred = ovl_override_creds(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	if (!ctx->pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		ovl_dir_reset(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	if (od->is_real) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		 * If parent is merge, then need to adjust d_ino for '..', if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		 * dir is impure then need to adjust d_ino for copied up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		 * entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		if (ovl_xino_bits(dentry->d_sb) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		    (ovl_same_fs(dentry->d_sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		     (ovl_is_impure_dir(file) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		      OVL_TYPE_MERGE(ovl_path_type(dentry->d_parent))))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			err = ovl_iterate_real(file, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 			err = iterate_dir(od->realfile, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	if (!od->cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		struct ovl_dir_cache *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		cache = ovl_cache_get(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		err = PTR_ERR(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		if (IS_ERR(cache))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		od->cache = cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		ovl_seek_cursor(od, ctx->pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	while (od->cursor != &od->cache->entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		if (!p->is_whiteout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 			if (!p->ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 				err = ovl_cache_update_ino(&file->f_path, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 				if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 			if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		od->cursor = p->l_node.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		ctx->pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	ovl_revert_creds(dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	loff_t res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	struct ovl_dir_file *od = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	inode_lock(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	if (!file->f_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		ovl_dir_reset(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	if (od->is_real) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		res = vfs_llseek(od->realfile, offset, origin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		file->f_pos = od->realfile->f_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		res = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		switch (origin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		case SEEK_CUR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			offset += file->f_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		case SEEK_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		if (offset < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		if (offset != file->f_pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 			file->f_pos = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 			if (od->cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 				ovl_seek_cursor(od, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		res = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	inode_unlock(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) static struct file *ovl_dir_open_realfile(const struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 					  struct path *realpath)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	struct file *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	res = ovl_path_open(realpath, O_RDONLY | (file->f_flags & O_LARGEFILE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	ovl_revert_creds(file_inode(file)->i_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845)  * Like ovl_real_fdget(), returns upperfile if dir was copied up since open.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846)  * Unlike ovl_real_fdget(), this caches upperfile in file->private_data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848)  * TODO: use same abstract type for file->private_data of dir and file so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849)  * upperfile could also be cached for files as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) struct file *ovl_dir_real_file(const struct file *file, bool want_upper)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	struct ovl_dir_file *od = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	struct dentry *dentry = file->f_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	struct file *old, *realfile = od->realfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	if (!OVL_TYPE_UPPER(ovl_path_type(dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		return want_upper ? NULL : realfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	 * Need to check if we started out being a lower dir, but got copied up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	if (!od->is_upper) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		realfile = READ_ONCE(od->upperfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		if (!realfile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 			struct path upperpath;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 			ovl_path_upper(dentry, &upperpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			realfile = ovl_dir_open_realfile(file, &upperpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 			if (IS_ERR(realfile))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 				return realfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 			old = cmpxchg_release(&od->upperfile, NULL, realfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 			if (old) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 				fput(realfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 				realfile = old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	return realfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 			 int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	struct file *realfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	err = ovl_sync_status(OVL_FS(file->f_path.dentry->d_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	if (err <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	realfile = ovl_dir_real_file(file, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	err = PTR_ERR_OR_ZERO(realfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	/* Nothing to sync for lower */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	if (!realfile || err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	return vfs_fsync_range(realfile, start, end, datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) static int ovl_dir_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	struct ovl_dir_file *od = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	if (od->cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		ovl_cache_put(od, file->f_path.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	fput(od->realfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	if (od->upperfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		fput(od->upperfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	kfree(od);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) static int ovl_dir_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	struct path realpath;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	struct file *realfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	struct ovl_dir_file *od;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	enum ovl_path_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	if (!od)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	type = ovl_path_real(file->f_path.dentry, &realpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	realfile = ovl_dir_open_realfile(file, &realpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	if (IS_ERR(realfile)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		kfree(od);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		return PTR_ERR(realfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	od->realfile = realfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	od->is_real = ovl_dir_is_real(file->f_path.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	od->is_upper = OVL_TYPE_UPPER(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	file->private_data = od;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) const struct file_operations ovl_dir_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	.read		= generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	.open		= ovl_dir_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	.iterate	= ovl_iterate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	.llseek		= ovl_dir_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	.fsync		= ovl_dir_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	.release	= ovl_dir_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	.unlocked_ioctl	= ovl_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	.compat_ioctl	= ovl_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	struct ovl_cache_entry *p, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	struct rb_root root = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	old_cred = ovl_override_creds(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	err = ovl_dir_read_merged(dentry, list, &root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	ovl_revert_creds(dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	list_for_each_entry_safe(p, n, list, l_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		 * Select whiteouts in upperdir, they should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		 * be cleared when deleting this directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		if (p->is_whiteout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 			if (p->is_upper)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 			goto del_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		if (p->name[0] == '.') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 			if (p->len == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 				goto del_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 			if (p->len == 2 && p->name[1] == '.')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 				goto del_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		err = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) del_entry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		list_del(&p->l_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	struct ovl_cache_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	inode_lock_nested(upper->d_inode, I_MUTEX_CHILD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	list_for_each_entry(p, list, l_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		if (WARN_ON(!p->is_whiteout || !p->is_upper))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		dentry = lookup_one_len(p->name, upper, p->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		if (IS_ERR(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 			pr_err("lookup '%s/%.*s' failed (%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 			       upper->d_name.name, p->len, p->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 			       (int) PTR_ERR(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		if (dentry->d_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 			ovl_cleanup(upper->d_inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	inode_unlock(upper->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) static int ovl_check_d_type(struct dir_context *ctx, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 			  int namelen, loff_t offset, u64 ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 			  unsigned int d_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	struct ovl_readdir_data *rdd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		container_of(ctx, struct ovl_readdir_data, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	/* Even if d_type is not supported, DT_DIR is returned for . and .. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	if (d_type != DT_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		rdd->d_type_supported = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)  * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)  * if error is encountered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) int ovl_check_d_type_supported(struct path *realpath)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	struct ovl_readdir_data rdd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		.ctx.actor = ovl_check_d_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		.d_type_supported = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	err = ovl_dir_read(realpath, &rdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	return rdd.d_type_supported;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) #define OVL_INCOMPATDIR_NAME "incompat"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) static int ovl_workdir_cleanup_recurse(struct path *path, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	struct inode *dir = path->dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	struct rb_root root = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	struct ovl_cache_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	struct ovl_readdir_data rdd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		.ctx.actor = ovl_fill_merge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		.dentry = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		.list = &list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		.root = &root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		.is_lowest = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	bool incompat = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	 * The "work/incompat" directory is treated specially - if it is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	 * empty, instead of printing a generic error and mounting read-only,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	 * we will error about incompat features and fail the mount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	 * When called from ovl_indexdir_cleanup(), path->dentry->d_name.name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	 * starts with '#'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	if (level == 2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	    !strcmp(path->dentry->d_name.name, OVL_INCOMPATDIR_NAME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 		incompat = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	err = ovl_dir_read(path, &rdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	inode_lock_nested(dir, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	list_for_each_entry(p, &list, l_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		if (p->name[0] == '.') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			if (p->len == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 			if (p->len == 2 && p->name[1] == '.')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		} else if (incompat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 			pr_err("overlay with incompat feature '%s' cannot be mounted\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 				p->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		dentry = lookup_one_len(p->name, path->dentry, p->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		if (IS_ERR(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		if (dentry->d_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 			err = ovl_workdir_cleanup(dir, path->mnt, dentry, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	inode_unlock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	ovl_cache_free(&list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) int ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 			 struct dentry *dentry, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	if (!d_is_dir(dentry) || level > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		return ovl_cleanup(dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	err = ovl_do_rmdir(dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		struct path path = { .mnt = mnt, .dentry = dentry };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		inode_unlock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		err = ovl_workdir_cleanup_recurse(&path, level + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		inode_lock_nested(dir, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 			err = ovl_cleanup(dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) int ovl_indexdir_cleanup(struct ovl_fs *ofs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	struct dentry *indexdir = ofs->indexdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	struct dentry *index = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	struct inode *dir = indexdir->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	struct path path = { .mnt = ovl_upper_mnt(ofs), .dentry = indexdir };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	struct rb_root root = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	struct ovl_cache_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	struct ovl_readdir_data rdd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		.ctx.actor = ovl_fill_merge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		.dentry = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		.list = &list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		.root = &root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		.is_lowest = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	err = ovl_dir_read(&path, &rdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	inode_lock_nested(dir, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	list_for_each_entry(p, &list, l_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		if (p->name[0] == '.') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 			if (p->len == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 			if (p->len == 2 && p->name[1] == '.')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		index = lookup_one_len(p->name, indexdir, p->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		if (IS_ERR(index)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 			err = PTR_ERR(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 			index = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		/* Cleanup leftover from index create/cleanup attempt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		if (index->d_name.name[0] == '#') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 			err = ovl_workdir_cleanup(dir, path.mnt, index, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		err = ovl_verify_index(ofs, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		} else if (err == -ESTALE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 			/* Cleanup stale index entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 			err = ovl_cleanup(dir, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		} else if (err != -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 			 * Abort mount to avoid corrupting the index if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 			 * an incompatible index entry was found or on out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 			 * of memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		} else if (ofs->config.nfs_export) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 			 * Whiteout orphan index to block future open by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 			 * handle after overlay nlink dropped to zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 			err = ovl_cleanup_and_whiteout(ofs, dir, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 			/* Cleanup orphan index entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 			err = ovl_cleanup(dir, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		dput(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		index = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	dput(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	inode_unlock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	ovl_cache_free(&list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		pr_err("failed index dir cleanup (%i)\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) }