Skip to content

Commit eec38f5

Browse files
jagalacticweiny2
authored andcommitted
dax: Add fs_dax_get() func to prepare dax for fs-dax usage
The fs_dax_get() function should be called by fs-dax file systems after opening a fsdev dax device. This adds holder_operations, which provides a memory failure callback path and effects exclusivity between callers of fs_dax_get(). fs_dax_get() is specific to fsdev_dax, so it checks the driver type (which required touching bus.[ch]). fs_dax_get() fails if fsdev_dax is not bound to the memory. This function serves the same role as fs_dax_get_by_bdev(), which dax file systems call after opening the pmem block device. This can't be located in fsdev.c because struct dax_device is opaque there. This will be called by fs/fuse/famfs.c in a subsequent commit. Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com> Reviewed-by: Dave Jiang <dave.jiang@intel.com> Signed-off-by: John Groves <john@groves.net> Link: https://patch.msgid.link/0100019d311d8750-75395c22-031b-4d5f-aebe-790dca656b87-000000@email.amazonses.com Signed-off-by: Ira Weiny <ira.weiny@intel.com>
1 parent 700ecbc commit eec38f5

4 files changed

Lines changed: 79 additions & 7 deletions

File tree

drivers/dax/bus.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ static int dax_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
3939
return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);
4040
}
4141

42-
#define to_dax_drv(__drv) container_of_const(__drv, struct dax_device_driver, drv)
43-
4442
static struct dax_id *__dax_match_id(const struct dax_device_driver *dax_drv,
4543
const char *dev_name)
4644
{

drivers/dax/bus.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ struct dax_device_driver {
4242
void (*remove)(struct dev_dax *dev);
4343
};
4444

45+
#define to_dax_drv(__drv) container_of_const(__drv, struct dax_device_driver, drv)
46+
4547
int __dax_driver_register(struct dax_device_driver *dax_drv,
4648
struct module *module, const char *mod_name);
4749
#define dax_driver_register(driver) \

drivers/dax/super.c

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/fs.h>
1515
#include <linux/cacheinfo.h>
1616
#include "dax-private.h"
17+
#include "bus.h"
1718

1819
/**
1920
* struct dax_device - anchor object for dax services
@@ -111,6 +112,10 @@ struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, u64 *start_off,
111112
}
112113
EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev);
113114

115+
#endif /* CONFIG_BLOCK && CONFIG_FS_DAX */
116+
117+
#if IS_ENABLED(CONFIG_FS_DAX)
118+
114119
void fs_put_dax(struct dax_device *dax_dev, void *holder)
115120
{
116121
if (dax_dev && holder &&
@@ -119,7 +124,66 @@ void fs_put_dax(struct dax_device *dax_dev, void *holder)
119124
put_dax(dax_dev);
120125
}
121126
EXPORT_SYMBOL_GPL(fs_put_dax);
122-
#endif /* CONFIG_BLOCK && CONFIG_FS_DAX */
127+
128+
/**
129+
* fs_dax_get() - get ownership of a devdax via holder/holder_ops
130+
*
131+
* fs-dax file systems call this function to prepare to use a devdax device for
132+
* fsdax. This is like fs_dax_get_by_bdev(), but the caller already has struct
133+
* dev_dax (and there is no bdev). The holder makes this exclusive.
134+
*
135+
* @dax_dev: dev to be prepared for fs-dax usage
136+
* @holder: filesystem or mapped device inside the dax_device
137+
* @hops: operations for the inner holder
138+
*
139+
* Returns: 0 on success, <0 on failure
140+
*/
141+
int fs_dax_get(struct dax_device *dax_dev, void *holder,
142+
const struct dax_holder_operations *hops)
143+
{
144+
struct dev_dax *dev_dax;
145+
struct dax_device_driver *dax_drv;
146+
int id;
147+
148+
id = dax_read_lock();
149+
if (!dax_dev || !dax_alive(dax_dev) || !igrab(&dax_dev->inode)) {
150+
dax_read_unlock(id);
151+
return -ENODEV;
152+
}
153+
dax_read_unlock(id);
154+
155+
/* Verify the device is bound to fsdev_dax driver */
156+
dev_dax = dax_get_private(dax_dev);
157+
if (!dev_dax) {
158+
iput(&dax_dev->inode);
159+
return -ENODEV;
160+
}
161+
162+
device_lock(&dev_dax->dev);
163+
if (!dev_dax->dev.driver) {
164+
device_unlock(&dev_dax->dev);
165+
iput(&dax_dev->inode);
166+
return -ENODEV;
167+
}
168+
dax_drv = to_dax_drv(dev_dax->dev.driver);
169+
if (dax_drv->type != DAXDRV_FSDEV_TYPE) {
170+
device_unlock(&dev_dax->dev);
171+
iput(&dax_dev->inode);
172+
return -EOPNOTSUPP;
173+
}
174+
device_unlock(&dev_dax->dev);
175+
176+
if (cmpxchg(&dax_dev->holder_data, NULL, holder)) {
177+
iput(&dax_dev->inode);
178+
return -EBUSY;
179+
}
180+
181+
dax_dev->holder_ops = hops;
182+
183+
return 0;
184+
}
185+
EXPORT_SYMBOL_GPL(fs_dax_get);
186+
#endif /* CONFIG_FS_DAX */
123187

124188
enum dax_device_flags {
125189
/* !alive + rcu grace period == no new operations / mappings */

include/linux/dax.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk);
130130
void dax_remove_host(struct gendisk *disk);
131131
struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, u64 *start_off,
132132
void *holder, const struct dax_holder_operations *ops);
133-
void fs_put_dax(struct dax_device *dax_dev, void *holder);
134133
#else
135134
static inline int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk)
136135
{
@@ -145,12 +144,12 @@ static inline struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev,
145144
{
146145
return NULL;
147146
}
148-
static inline void fs_put_dax(struct dax_device *dax_dev, void *holder)
149-
{
150-
}
151147
#endif /* CONFIG_BLOCK && CONFIG_FS_DAX */
152148

153149
#if IS_ENABLED(CONFIG_FS_DAX)
150+
void fs_put_dax(struct dax_device *dax_dev, void *holder);
151+
int fs_dax_get(struct dax_device *dax_dev, void *holder,
152+
const struct dax_holder_operations *hops);
154153
int dax_writeback_mapping_range(struct address_space *mapping,
155154
struct dax_device *dax_dev, struct writeback_control *wbc);
156155
int dax_folio_reset_order(struct folio *folio);
@@ -164,6 +163,15 @@ dax_entry_t dax_lock_mapping_entry(struct address_space *mapping,
164163
void dax_unlock_mapping_entry(struct address_space *mapping,
165164
unsigned long index, dax_entry_t cookie);
166165
#else
166+
static inline void fs_put_dax(struct dax_device *dax_dev, void *holder)
167+
{
168+
}
169+
170+
static inline int fs_dax_get(struct dax_device *dax_dev, void *holder,
171+
const struct dax_holder_operations *hops)
172+
{
173+
return -EOPNOTSUPP;
174+
}
167175
static inline struct page *dax_layout_busy_page(struct address_space *mapping)
168176
{
169177
return NULL;

0 commit comments

Comments
 (0)