0177-configfs-implement-binary-attributes.patch 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. From 25daa4b3566d9b695d965c48a8eae312c566c709 Mon Sep 17 00:00:00 2001
  2. From: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
  3. Date: Thu, 22 Oct 2015 23:30:04 +0300
  4. Subject: [PATCH 177/381] configfs: implement binary attributes
  5. ConfigFS lacked binary attributes up until now. This patch
  6. introduces support for binary attributes in a somewhat similar
  7. manner of sysfs binary attributes albeit with changes that
  8. fit the configfs usage model.
  9. Problems that configfs binary attributes fix are everything that
  10. requires a binary blob as part of the configuration of a resource,
  11. such as bitstream loading for FPGAs, DTBs for dynamically created
  12. devices etc.
  13. Look at Documentation/filesystems/configfs/configfs.txt for internals
  14. and howto use them.
  15. This patch is against linux-next as of today that contains
  16. Christoph's configfs rework.
  17. Signed-off-by: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
  18. [hch: folded a fix from Geert Uytterhoeven <geert+renesas@glider.be>]
  19. [hch: a few tiny updates based on review feedback]
  20. Signed-off-by: Christoph Hellwig <hch@lst.de>
  21. ---
  22. Documentation/filesystems/configfs/configfs.txt | 57 +++++-
  23. fs/configfs/configfs_internal.h | 14 +-
  24. fs/configfs/dir.c | 18 +-
  25. fs/configfs/file.c | 255 +++++++++++++++++++++++-
  26. fs/configfs/inode.c | 2 +-
  27. include/linux/configfs.h | 50 +++++
  28. 6 files changed, 374 insertions(+), 22 deletions(-)
  29. --- a/Documentation/filesystems/configfs/configfs.txt
  30. +++ b/Documentation/filesystems/configfs/configfs.txt
  31. @@ -51,15 +51,27 @@ configfs tree is always there, whether m
  32. An item is created via mkdir(2). The item's attributes will also
  33. appear at this time. readdir(3) can determine what the attributes are,
  34. read(2) can query their default values, and write(2) can store new
  35. -values. Like sysfs, attributes should be ASCII text files, preferably
  36. -with only one value per file. The same efficiency caveats from sysfs
  37. -apply. Don't mix more than one attribute in one attribute file.
  38. -
  39. -Like sysfs, configfs expects write(2) to store the entire buffer at
  40. -once. When writing to configfs attributes, userspace processes should
  41. -first read the entire file, modify the portions they wish to change, and
  42. -then write the entire buffer back. Attribute files have a maximum size
  43. -of one page (PAGE_SIZE, 4096 on i386).
  44. +values. Don't mix more than one attribute in one attribute file.
  45. +
  46. +There are two types of configfs attributes:
  47. +
  48. +* Normal attributes, which similar to sysfs attributes, are small ASCII text
  49. +files, with a maximum size of one page (PAGE_SIZE, 4096 on i386). Preferably
  50. +only one value per file should be used, and the same caveats from sysfs apply.
  51. +Configfs expects write(2) to store the entire buffer at once. When writing to
  52. +normal configfs attributes, userspace processes should first read the entire
  53. +file, modify the portions they wish to change, and then write the entire
  54. +buffer back.
  55. +
  56. +* Binary attributes, which are somewhat similar to sysfs binary attributes,
  57. +but with a few slight changes to semantics. The PAGE_SIZE limitation does not
  58. +apply, but the whole binary item must fit in single kernel vmalloc'ed buffer.
  59. +The write(2) calls from user space are buffered, and the attributes'
  60. +write_bin_attribute method will be invoked on the final close, therefore it is
  61. +imperative for user-space to check the return code of close(2) in order to
  62. +verify that the operation finished successfully.
  63. +To avoid a malicious user OOMing the kernel, there's a per-binary attribute
  64. +maximum buffer value.
  65. When an item needs to be destroyed, remove it with rmdir(2). An
  66. item cannot be destroyed if any other item has a link to it (via
  67. @@ -171,6 +183,7 @@ among other things. For that, it needs
  68. struct configfs_item_operations *ct_item_ops;
  69. struct configfs_group_operations *ct_group_ops;
  70. struct configfs_attribute **ct_attrs;
  71. + struct configfs_bin_attribute **ct_bin_attrs;
  72. };
  73. The most basic function of a config_item_type is to define what
  74. @@ -201,6 +214,32 @@ be called whenever userspace asks for a
  75. attribute is writable and provides a ->store method, that method will be
  76. be called whenever userspace asks for a write(2) on the attribute.
  77. +[struct configfs_bin_attribute]
  78. +
  79. + struct configfs_attribute {
  80. + struct configfs_attribute cb_attr;
  81. + void *cb_private;
  82. + size_t cb_max_size;
  83. + };
  84. +
  85. +The binary attribute is used when the one needs to use binary blob to
  86. +appear as the contents of a file in the item's configfs directory.
  87. +To do so add the binary attribute to the NULL-terminated array
  88. +config_item_type->ct_bin_attrs, and the item appears in configfs, the
  89. +attribute file will appear with the configfs_bin_attribute->cb_attr.ca_name
  90. +filename. configfs_bin_attribute->cb_attr.ca_mode specifies the file
  91. +permissions.
  92. +The cb_private member is provided for use by the driver, while the
  93. +cb_max_size member specifies the maximum amount of vmalloc buffer
  94. +to be used.
  95. +
  96. +If binary attribute is readable and the config_item provides a
  97. +ct_item_ops->read_bin_attribute() method, that method will be called
  98. +whenever userspace asks for a read(2) on the attribute. The converse
  99. +will happen for write(2). The reads/writes are bufferred so only a
  100. +single read/write will occur; the attributes' need not concern itself
  101. +with it.
  102. +
  103. [struct config_group]
  104. A config_item cannot live in a vacuum. The only way one can be created
  105. --- a/fs/configfs/configfs_internal.h
  106. +++ b/fs/configfs/configfs_internal.h
  107. @@ -53,13 +53,14 @@ struct configfs_dirent {
  108. #define CONFIGFS_ROOT 0x0001
  109. #define CONFIGFS_DIR 0x0002
  110. #define CONFIGFS_ITEM_ATTR 0x0004
  111. +#define CONFIGFS_ITEM_BIN_ATTR 0x0008
  112. #define CONFIGFS_ITEM_LINK 0x0020
  113. #define CONFIGFS_USET_DIR 0x0040
  114. #define CONFIGFS_USET_DEFAULT 0x0080
  115. #define CONFIGFS_USET_DROPPING 0x0100
  116. #define CONFIGFS_USET_IN_MKDIR 0x0200
  117. #define CONFIGFS_USET_CREATING 0x0400
  118. -#define CONFIGFS_NOT_PINNED (CONFIGFS_ITEM_ATTR)
  119. +#define CONFIGFS_NOT_PINNED (CONFIGFS_ITEM_ATTR | CONFIGFS_ITEM_BIN_ATTR)
  120. extern struct mutex configfs_symlink_mutex;
  121. extern spinlock_t configfs_dirent_lock;
  122. @@ -72,6 +73,8 @@ extern struct inode * configfs_new_inode
  123. extern int configfs_create(struct dentry *, umode_t mode, void (*init)(struct inode *));
  124. extern int configfs_create_file(struct config_item *, const struct configfs_attribute *);
  125. +extern int configfs_create_bin_file(struct config_item *,
  126. + const struct configfs_bin_attribute *);
  127. extern int configfs_make_dirent(struct configfs_dirent *,
  128. struct dentry *, void *, umode_t, int);
  129. extern int configfs_dirent_is_ready(struct configfs_dirent *);
  130. @@ -88,7 +91,7 @@ extern void configfs_release_fs(void);
  131. extern struct rw_semaphore configfs_rename_sem;
  132. extern const struct file_operations configfs_dir_operations;
  133. extern const struct file_operations configfs_file_operations;
  134. -extern const struct file_operations bin_fops;
  135. +extern const struct file_operations configfs_bin_file_operations;
  136. extern const struct inode_operations configfs_dir_inode_operations;
  137. extern const struct inode_operations configfs_root_inode_operations;
  138. extern const struct inode_operations configfs_symlink_inode_operations;
  139. @@ -119,6 +122,13 @@ static inline struct configfs_attribute
  140. return ((struct configfs_attribute *) sd->s_element);
  141. }
  142. +static inline struct configfs_bin_attribute *to_bin_attr(struct dentry *dentry)
  143. +{
  144. + struct configfs_attribute *attr = to_attr(dentry);
  145. +
  146. + return container_of(attr, struct configfs_bin_attribute, cb_attr);
  147. +}
  148. +
  149. static inline struct config_item *configfs_get_config_item(struct dentry *dentry)
  150. {
  151. struct config_item * item = NULL;
  152. --- a/fs/configfs/dir.c
  153. +++ b/fs/configfs/dir.c
  154. @@ -255,6 +255,12 @@ static void configfs_init_file(struct in
  155. inode->i_fop = &configfs_file_operations;
  156. }
  157. +static void configfs_init_bin_file(struct inode *inode)
  158. +{
  159. + inode->i_size = 0;
  160. + inode->i_fop = &configfs_bin_file_operations;
  161. +}
  162. +
  163. static void init_symlink(struct inode * inode)
  164. {
  165. inode->i_op = &configfs_symlink_inode_operations;
  166. @@ -423,7 +429,9 @@ static int configfs_attach_attr(struct c
  167. spin_unlock(&configfs_dirent_lock);
  168. error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
  169. - configfs_init_file);
  170. + (sd->s_type & CONFIGFS_ITEM_BIN_ATTR) ?
  171. + configfs_init_bin_file :
  172. + configfs_init_file);
  173. if (error) {
  174. configfs_put(sd);
  175. return error;
  176. @@ -583,6 +591,7 @@ static int populate_attrs(struct config_
  177. {
  178. struct config_item_type *t = item->ci_type;
  179. struct configfs_attribute *attr;
  180. + struct configfs_bin_attribute *bin_attr;
  181. int error = 0;
  182. int i;
  183. @@ -594,6 +603,13 @@ static int populate_attrs(struct config_
  184. break;
  185. }
  186. }
  187. + if (t->ct_bin_attrs) {
  188. + for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
  189. + error = configfs_create_bin_file(item, bin_attr);
  190. + if (error)
  191. + break;
  192. + }
  193. + }
  194. if (error)
  195. detach_attrs(item);
  196. --- a/fs/configfs/file.c
  197. +++ b/fs/configfs/file.c
  198. @@ -28,6 +28,7 @@
  199. #include <linux/module.h>
  200. #include <linux/slab.h>
  201. #include <linux/mutex.h>
  202. +#include <linux/vmalloc.h>
  203. #include <asm/uaccess.h>
  204. #include <linux/configfs.h>
  205. @@ -48,6 +49,10 @@ struct configfs_buffer {
  206. struct configfs_item_operations * ops;
  207. struct mutex mutex;
  208. int needs_read_fill;
  209. + bool read_in_progress;
  210. + bool write_in_progress;
  211. + char *bin_buffer;
  212. + int bin_buffer_size;
  213. };
  214. @@ -123,6 +128,87 @@ out:
  215. return retval;
  216. }
  217. +/**
  218. + * configfs_read_bin_file - read a binary attribute.
  219. + * @file: file pointer.
  220. + * @buf: buffer to fill.
  221. + * @count: number of bytes to read.
  222. + * @ppos: starting offset in file.
  223. + *
  224. + * Userspace wants to read a binary attribute file. The attribute
  225. + * descriptor is in the file's ->d_fsdata. The target item is in the
  226. + * directory's ->d_fsdata.
  227. + *
  228. + * We check whether we need to refill the buffer. If so we will
  229. + * call the attributes' attr->read() twice. The first time we
  230. + * will pass a NULL as a buffer pointer, which the attributes' method
  231. + * will use to return the size of the buffer required. If no error
  232. + * occurs we will allocate the buffer using vmalloc and call
  233. + * attr->read() again passing that buffer as an argument.
  234. + * Then we just copy to user-space using simple_read_from_buffer.
  235. + */
  236. +
  237. +static ssize_t
  238. +configfs_read_bin_file(struct file *file, char __user *buf,
  239. + size_t count, loff_t *ppos)
  240. +{
  241. + struct configfs_buffer *buffer = file->private_data;
  242. + struct dentry *dentry = file->f_path.dentry;
  243. + struct config_item *item = to_item(dentry->d_parent);
  244. + struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
  245. + ssize_t retval = 0;
  246. + ssize_t len = min_t(size_t, count, PAGE_SIZE);
  247. +
  248. + mutex_lock(&buffer->mutex);
  249. +
  250. + /* we don't support switching read/write modes */
  251. + if (buffer->write_in_progress) {
  252. + retval = -ETXTBSY;
  253. + goto out;
  254. + }
  255. + buffer->read_in_progress = 1;
  256. +
  257. + if (buffer->needs_read_fill) {
  258. + /* perform first read with buf == NULL to get extent */
  259. + len = bin_attr->read(item, NULL, 0);
  260. + if (len <= 0) {
  261. + retval = len;
  262. + goto out;
  263. + }
  264. +
  265. + /* do not exceed the maximum value */
  266. + if (bin_attr->cb_max_size && len > bin_attr->cb_max_size) {
  267. + retval = -EFBIG;
  268. + goto out;
  269. + }
  270. +
  271. + buffer->bin_buffer = vmalloc(len);
  272. + if (buffer->bin_buffer == NULL) {
  273. + retval = -ENOMEM;
  274. + goto out;
  275. + }
  276. + buffer->bin_buffer_size = len;
  277. +
  278. + /* perform second read to fill buffer */
  279. + len = bin_attr->read(item, buffer->bin_buffer, len);
  280. + if (len < 0) {
  281. + retval = len;
  282. + vfree(buffer->bin_buffer);
  283. + buffer->bin_buffer_size = 0;
  284. + buffer->bin_buffer = NULL;
  285. + goto out;
  286. + }
  287. +
  288. + buffer->needs_read_fill = 0;
  289. + }
  290. +
  291. + retval = simple_read_from_buffer(buf, count, ppos, buffer->bin_buffer,
  292. + buffer->bin_buffer_size);
  293. +out:
  294. + mutex_unlock(&buffer->mutex);
  295. + return retval;
  296. +}
  297. +
  298. /**
  299. * fill_write_buffer - copy buffer from userspace.
  300. @@ -209,10 +295,80 @@ configfs_write_file(struct file *file, c
  301. return len;
  302. }
  303. -static int check_perm(struct inode * inode, struct file * file)
  304. +/**
  305. + * configfs_write_bin_file - write a binary attribute.
  306. + * @file: file pointer
  307. + * @buf: data to write
  308. + * @count: number of bytes
  309. + * @ppos: starting offset
  310. + *
  311. + * Writing to a binary attribute file is similar to a normal read.
  312. + * We buffer the consecutive writes (binary attribute files do not
  313. + * support lseek) in a continuously growing buffer, but we don't
  314. + * commit until the close of the file.
  315. + */
  316. +
  317. +static ssize_t
  318. +configfs_write_bin_file(struct file *file, const char __user *buf,
  319. + size_t count, loff_t *ppos)
  320. +{
  321. + struct configfs_buffer *buffer = file->private_data;
  322. + struct dentry *dentry = file->f_path.dentry;
  323. + struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
  324. + void *tbuf = NULL;
  325. + ssize_t len;
  326. +
  327. + mutex_lock(&buffer->mutex);
  328. +
  329. + /* we don't support switching read/write modes */
  330. + if (buffer->read_in_progress) {
  331. + len = -ETXTBSY;
  332. + goto out;
  333. + }
  334. + buffer->write_in_progress = 1;
  335. +
  336. + /* buffer grows? */
  337. + if (*ppos + count > buffer->bin_buffer_size) {
  338. +
  339. + if (bin_attr->cb_max_size &&
  340. + *ppos + count > bin_attr->cb_max_size) {
  341. + len = -EFBIG;
  342. + }
  343. +
  344. + tbuf = vmalloc(*ppos + count);
  345. + if (tbuf == NULL) {
  346. + len = -ENOMEM;
  347. + goto out;
  348. + }
  349. +
  350. + /* copy old contents */
  351. + if (buffer->bin_buffer) {
  352. + memcpy(tbuf, buffer->bin_buffer,
  353. + buffer->bin_buffer_size);
  354. + vfree(buffer->bin_buffer);
  355. + }
  356. +
  357. + /* clear the new area */
  358. + memset(tbuf + buffer->bin_buffer_size, 0,
  359. + *ppos + count - buffer->bin_buffer_size);
  360. + buffer->bin_buffer = tbuf;
  361. + buffer->bin_buffer_size = *ppos + count;
  362. + }
  363. +
  364. + len = simple_write_to_buffer(buffer->bin_buffer,
  365. + buffer->bin_buffer_size, ppos, buf, count);
  366. + if (len > 0)
  367. + *ppos += len;
  368. +out:
  369. + mutex_unlock(&buffer->mutex);
  370. + return len;
  371. +}
  372. +
  373. +static int check_perm(struct inode * inode, struct file * file, int type)
  374. {
  375. struct config_item *item = configfs_get_config_item(file->f_path.dentry->d_parent);
  376. struct configfs_attribute * attr = to_attr(file->f_path.dentry);
  377. + struct configfs_bin_attribute *bin_attr = NULL;
  378. struct configfs_buffer * buffer;
  379. struct configfs_item_operations * ops = NULL;
  380. int error = 0;
  381. @@ -220,6 +376,9 @@ static int check_perm(struct inode * ino
  382. if (!item || !attr)
  383. goto Einval;
  384. + if (type & CONFIGFS_ITEM_BIN_ATTR)
  385. + bin_attr = to_bin_attr(file->f_path.dentry);
  386. +
  387. /* Grab the module reference for this attribute if we have one */
  388. if (!try_module_get(attr->ca_owner)) {
  389. error = -ENODEV;
  390. @@ -236,9 +395,14 @@ static int check_perm(struct inode * ino
  391. * and we must have a store method.
  392. */
  393. if (file->f_mode & FMODE_WRITE) {
  394. - if (!(inode->i_mode & S_IWUGO) || !attr->store)
  395. + if (!(inode->i_mode & S_IWUGO))
  396. goto Eaccess;
  397. + if ((type & CONFIGFS_ITEM_ATTR) && !attr->store)
  398. + goto Eaccess;
  399. +
  400. + if ((type & CONFIGFS_ITEM_BIN_ATTR) && !bin_attr->write)
  401. + goto Eaccess;
  402. }
  403. /* File needs read support.
  404. @@ -246,7 +410,13 @@ static int check_perm(struct inode * ino
  405. * must be a show method for it.
  406. */
  407. if (file->f_mode & FMODE_READ) {
  408. - if (!(inode->i_mode & S_IRUGO) || !attr->show)
  409. + if (!(inode->i_mode & S_IRUGO))
  410. + goto Eaccess;
  411. +
  412. + if ((type & CONFIGFS_ITEM_ATTR) && !attr->show)
  413. + goto Eaccess;
  414. +
  415. + if ((type & CONFIGFS_ITEM_BIN_ATTR) && !bin_attr->read)
  416. goto Eaccess;
  417. }
  418. @@ -260,6 +430,8 @@ static int check_perm(struct inode * ino
  419. }
  420. mutex_init(&buffer->mutex);
  421. buffer->needs_read_fill = 1;
  422. + buffer->read_in_progress = 0;
  423. + buffer->write_in_progress = 0;
  424. buffer->ops = ops;
  425. file->private_data = buffer;
  426. goto Done;
  427. @@ -277,12 +449,7 @@ static int check_perm(struct inode * ino
  428. return error;
  429. }
  430. -static int configfs_open_file(struct inode * inode, struct file * filp)
  431. -{
  432. - return check_perm(inode,filp);
  433. -}
  434. -
  435. -static int configfs_release(struct inode * inode, struct file * filp)
  436. +static int configfs_release(struct inode *inode, struct file *filp)
  437. {
  438. struct config_item * item = to_item(filp->f_path.dentry->d_parent);
  439. struct configfs_attribute * attr = to_attr(filp->f_path.dentry);
  440. @@ -303,6 +470,47 @@ static int configfs_release(struct inode
  441. return 0;
  442. }
  443. +static int configfs_open_file(struct inode *inode, struct file *filp)
  444. +{
  445. + return check_perm(inode, filp, CONFIGFS_ITEM_ATTR);
  446. +}
  447. +
  448. +static int configfs_open_bin_file(struct inode *inode, struct file *filp)
  449. +{
  450. + return check_perm(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
  451. +}
  452. +
  453. +static int configfs_release_bin_file(struct inode *inode, struct file *filp)
  454. +{
  455. + struct configfs_buffer *buffer = filp->private_data;
  456. + struct dentry *dentry = filp->f_path.dentry;
  457. + struct config_item *item = to_item(dentry->d_parent);
  458. + struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
  459. + ssize_t len = 0;
  460. + int ret;
  461. +
  462. + buffer->read_in_progress = 0;
  463. +
  464. + if (buffer->write_in_progress) {
  465. + buffer->write_in_progress = 0;
  466. +
  467. + len = bin_attr->write(item, buffer->bin_buffer,
  468. + buffer->bin_buffer_size);
  469. +
  470. + /* vfree on NULL is safe */
  471. + vfree(buffer->bin_buffer);
  472. + buffer->bin_buffer = NULL;
  473. + buffer->bin_buffer_size = 0;
  474. + buffer->needs_read_fill = 1;
  475. + }
  476. +
  477. + ret = configfs_release(inode, filp);
  478. + if (len < 0)
  479. + return len;
  480. + return ret;
  481. +}
  482. +
  483. +
  484. const struct file_operations configfs_file_operations = {
  485. .read = configfs_read_file,
  486. .write = configfs_write_file,
  487. @@ -311,6 +519,14 @@ const struct file_operations configfs_fi
  488. .release = configfs_release,
  489. };
  490. +const struct file_operations configfs_bin_file_operations = {
  491. + .read = configfs_read_bin_file,
  492. + .write = configfs_write_bin_file,
  493. + .llseek = NULL, /* bin file is not seekable */
  494. + .open = configfs_open_bin_file,
  495. + .release = configfs_release_bin_file,
  496. +};
  497. +
  498. /**
  499. * configfs_create_file - create an attribute file for an item.
  500. * @item: item we're creating for.
  501. @@ -332,3 +548,24 @@ int configfs_create_file(struct config_i
  502. return error;
  503. }
  504. +/**
  505. + * configfs_create_bin_file - create a binary attribute file for an item.
  506. + * @item: item we're creating for.
  507. + * @attr: atrribute descriptor.
  508. + */
  509. +
  510. +int configfs_create_bin_file(struct config_item *item,
  511. + const struct configfs_bin_attribute *bin_attr)
  512. +{
  513. + struct dentry *dir = item->ci_dentry;
  514. + struct configfs_dirent *parent_sd = dir->d_fsdata;
  515. + umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG;
  516. + int error = 0;
  517. +
  518. + mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_NORMAL);
  519. + error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
  520. + CONFIGFS_ITEM_BIN_ATTR);
  521. + mutex_unlock(&dir->d_inode->i_mutex);
  522. +
  523. + return error;
  524. +}
  525. --- a/fs/configfs/inode.c
  526. +++ b/fs/configfs/inode.c
  527. @@ -218,7 +218,7 @@ const unsigned char * configfs_get_name(
  528. if (sd->s_type & (CONFIGFS_DIR | CONFIGFS_ITEM_LINK))
  529. return sd->s_dentry->d_name.name;
  530. - if (sd->s_type & CONFIGFS_ITEM_ATTR) {
  531. + if (sd->s_type & (CONFIGFS_ITEM_ATTR | CONFIGFS_ITEM_BIN_ATTR)) {
  532. attr = sd->s_element;
  533. return attr->ca_name;
  534. }
  535. --- a/include/linux/configfs.h
  536. +++ b/include/linux/configfs.h
  537. @@ -51,6 +51,7 @@ struct module;
  538. struct configfs_item_operations;
  539. struct configfs_group_operations;
  540. struct configfs_attribute;
  541. +struct configfs_bin_attribute;
  542. struct configfs_subsystem;
  543. struct config_item {
  544. @@ -84,6 +85,7 @@ struct config_item_type {
  545. struct configfs_item_operations *ct_item_ops;
  546. struct configfs_group_operations *ct_group_ops;
  547. struct configfs_attribute **ct_attrs;
  548. + struct configfs_bin_attribute **ct_bin_attrs;
  549. };
  550. /**
  551. @@ -154,6 +156,54 @@ static struct configfs_attribute _pfx##a
  552. .store = _pfx##_name##_store, \
  553. }
  554. +struct file;
  555. +struct vm_area_struct;
  556. +
  557. +struct configfs_bin_attribute {
  558. + struct configfs_attribute cb_attr; /* std. attribute */
  559. + void *cb_private; /* for user */
  560. + size_t cb_max_size; /* max core size */
  561. + ssize_t (*read)(struct config_item *, void *, size_t);
  562. + ssize_t (*write)(struct config_item *, const void *, size_t);
  563. +};
  564. +
  565. +#define CONFIGFS_BIN_ATTR(_pfx, _name, _priv, _maxsz) \
  566. +static struct configfs_bin_attribute _pfx##attr_##_name = { \
  567. + .cb_attr = { \
  568. + .ca_name = __stringify(_name), \
  569. + .ca_mode = S_IRUGO | S_IWUSR, \
  570. + .ca_owner = THIS_MODULE, \
  571. + }, \
  572. + .cb_private = _priv, \
  573. + .cb_max_size = _maxsz, \
  574. + .read = _pfx##_name##_read, \
  575. + .write = _pfx##_name##_write, \
  576. +}
  577. +
  578. +#define CONFIGFS_BIN_ATTR_RO(_pfx, _name, _priv, _maxsz) \
  579. +static struct configfs_attribute _pfx##attr_##_name = { \
  580. + .cb_attr = { \
  581. + .ca_name = __stringify(_name), \
  582. + .ca_mode = S_IRUGO, \
  583. + .ca_owner = THIS_MODULE, \
  584. + }, \
  585. + .cb_private = _priv, \
  586. + .cb_max_size = _maxsz, \
  587. + .read = _pfx##_name##_read, \
  588. +}
  589. +
  590. +#define CONFIGFS_BIN_ATTR_WO(_pfx, _name, _priv, _maxsz) \
  591. +static struct configfs_attribute _pfx##attr_##_name = { \
  592. + .cb_attr = { \
  593. + .ca_name = __stringify(_name), \
  594. + .ca_mode = S_IWUSR, \
  595. + .ca_owner = THIS_MODULE, \
  596. + }, \
  597. + .cb_private = _priv, \
  598. + .cb_max_size = _maxsz, \
  599. + .write = _pfx##_name##_write, \
  600. +}
  601. +
  602. /*
  603. * If allow_link() exists, the item can symlink(2) out to other
  604. * items. If the item is a group, it may support mkdir(2).