xattr.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/reiserfs/xattr.c
  4. *
  5. * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
  6. *
  7. */
  8. /*
  9. * In order to implement EA/ACLs in a clean, backwards compatible manner,
  10. * they are implemented as files in a "private" directory.
  11. * Each EA is in it's own file, with the directory layout like so (/ is assumed
  12. * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
  13. * directories named using the capital-hex form of the objectid and
  14. * generation number are used. Inside each directory are individual files
  15. * named with the name of the extended attribute.
  16. *
  17. * So, for objectid 12648430, we could have:
  18. * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
  19. * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
  20. * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
  21. * .. or similar.
  22. *
  23. * The file contents are the text of the EA. The size is known based on the
  24. * stat data describing the file.
  25. *
  26. * In the case of system.posix_acl_access and system.posix_acl_default, since
  27. * these are special cases for filesystem ACLs, they are interpreted by the
  28. * kernel, in addition, they are negatively and positively cached and attached
  29. * to the inode so that unnecessary lookups are avoided.
  30. *
  31. * Locking works like so:
  32. * Directory components (xattr root, xattr dir) are protectd by their i_mutex.
  33. * The xattrs themselves are protected by the xattr_sem.
  34. */
  35. #include "reiserfs.h"
  36. #include <linux/capability.h>
  37. #include <linux/dcache.h>
  38. #include <linux/namei.h>
  39. #include <linux/errno.h>
  40. #include <linux/gfp.h>
  41. #include <linux/fs.h>
  42. #include <linux/file.h>
  43. #include <linux/pagemap.h>
  44. #include <linux/xattr.h>
  45. #include "xattr.h"
  46. #include "acl.h"
  47. #include <linux/uaccess.h>
  48. #include <net/checksum.h>
  49. #include <linux/stat.h>
  50. #include <linux/quotaops.h>
  51. #include <linux/security.h>
  52. #include <linux/posix_acl_xattr.h>
  53. #define PRIVROOT_NAME ".reiserfs_priv"
  54. #define XAROOT_NAME "xattrs"
  55. /*
  56. * Helpers for inode ops. We do this so that we don't have all the VFS
  57. * overhead and also for proper i_mutex annotation.
  58. * dir->i_mutex must be held for all of them.
  59. */
  60. #ifdef CONFIG_REISERFS_FS_XATTR
  61. static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
  62. {
  63. BUG_ON(!inode_is_locked(dir));
  64. return dir->i_op->create(dir, dentry, mode, true);
  65. }
  66. #endif
  67. static int xattr_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  68. {
  69. BUG_ON(!inode_is_locked(dir));
  70. return dir->i_op->mkdir(dir, dentry, mode);
  71. }
  72. /*
  73. * We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
  74. * mutation ops aren't called during rename or splace, which are the
  75. * only other users of I_MUTEX_CHILD. It violates the ordering, but that's
  76. * better than allocating another subclass just for this code.
  77. */
  78. static int xattr_unlink(struct inode *dir, struct dentry *dentry)
  79. {
  80. int error;
  81. BUG_ON(!inode_is_locked(dir));
  82. inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
  83. error = dir->i_op->unlink(dir, dentry);
  84. inode_unlock(d_inode(dentry));
  85. if (!error)
  86. d_delete(dentry);
  87. return error;
  88. }
  89. static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
  90. {
  91. int error;
  92. BUG_ON(!inode_is_locked(dir));
  93. inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
  94. error = dir->i_op->rmdir(dir, dentry);
  95. if (!error)
  96. d_inode(dentry)->i_flags |= S_DEAD;
  97. inode_unlock(d_inode(dentry));
  98. if (!error)
  99. d_delete(dentry);
  100. return error;
  101. }
  102. #define xattr_may_create(flags) (!flags || flags & XATTR_CREATE)
  103. static struct dentry *open_xa_root(struct super_block *sb, int flags)
  104. {
  105. struct dentry *privroot = REISERFS_SB(sb)->priv_root;
  106. struct dentry *xaroot;
  107. if (d_really_is_negative(privroot))
  108. return ERR_PTR(-ENODATA);
  109. inode_lock_nested(d_inode(privroot), I_MUTEX_XATTR);
  110. xaroot = dget(REISERFS_SB(sb)->xattr_root);
  111. if (!xaroot)
  112. xaroot = ERR_PTR(-ENODATA);
  113. else if (d_really_is_negative(xaroot)) {
  114. int err = -ENODATA;
  115. if (xattr_may_create(flags))
  116. err = xattr_mkdir(d_inode(privroot), xaroot, 0700);
  117. if (err) {
  118. dput(xaroot);
  119. xaroot = ERR_PTR(err);
  120. }
  121. }
  122. inode_unlock(d_inode(privroot));
  123. return xaroot;
  124. }
  125. static struct dentry *open_xa_dir(const struct inode *inode, int flags)
  126. {
  127. struct dentry *xaroot, *xadir;
  128. char namebuf[17];
  129. xaroot = open_xa_root(inode->i_sb, flags);
  130. if (IS_ERR(xaroot))
  131. return xaroot;
  132. snprintf(namebuf, sizeof(namebuf), "%X.%X",
  133. le32_to_cpu(INODE_PKEY(inode)->k_objectid),
  134. inode->i_generation);
  135. inode_lock_nested(d_inode(xaroot), I_MUTEX_XATTR);
  136. xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
  137. if (!IS_ERR(xadir) && d_really_is_negative(xadir)) {
  138. int err = -ENODATA;
  139. if (xattr_may_create(flags))
  140. err = xattr_mkdir(d_inode(xaroot), xadir, 0700);
  141. if (err) {
  142. dput(xadir);
  143. xadir = ERR_PTR(err);
  144. }
  145. }
  146. inode_unlock(d_inode(xaroot));
  147. dput(xaroot);
  148. return xadir;
  149. }
  150. /*
  151. * The following are side effects of other operations that aren't explicitly
  152. * modifying extended attributes. This includes operations such as permissions
  153. * or ownership changes, object deletions, etc.
  154. */
  155. struct reiserfs_dentry_buf {
  156. struct dir_context ctx;
  157. struct dentry *xadir;
  158. int count;
  159. int err;
  160. struct dentry *dentries[8];
  161. };
  162. static int
  163. fill_with_dentries(struct dir_context *ctx, const char *name, int namelen,
  164. loff_t offset, u64 ino, unsigned int d_type)
  165. {
  166. struct reiserfs_dentry_buf *dbuf =
  167. container_of(ctx, struct reiserfs_dentry_buf, ctx);
  168. struct dentry *dentry;
  169. WARN_ON_ONCE(!inode_is_locked(d_inode(dbuf->xadir)));
  170. if (dbuf->count == ARRAY_SIZE(dbuf->dentries))
  171. return -ENOSPC;
  172. if (name[0] == '.' && (namelen < 2 ||
  173. (namelen == 2 && name[1] == '.')))
  174. return 0;
  175. dentry = lookup_one_len(name, dbuf->xadir, namelen);
  176. if (IS_ERR(dentry)) {
  177. dbuf->err = PTR_ERR(dentry);
  178. return PTR_ERR(dentry);
  179. } else if (d_really_is_negative(dentry)) {
  180. /* A directory entry exists, but no file? */
  181. reiserfs_error(dentry->d_sb, "xattr-20003",
  182. "Corrupted directory: xattr %pd listed but "
  183. "not found for file %pd.\n",
  184. dentry, dbuf->xadir);
  185. dput(dentry);
  186. dbuf->err = -EIO;
  187. return -EIO;
  188. }
  189. dbuf->dentries[dbuf->count++] = dentry;
  190. return 0;
  191. }
  192. static void
  193. cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
  194. {
  195. int i;
  196. for (i = 0; i < buf->count; i++)
  197. if (buf->dentries[i])
  198. dput(buf->dentries[i]);
  199. }
  200. static int reiserfs_for_each_xattr(struct inode *inode,
  201. int (*action)(struct dentry *, void *),
  202. void *data)
  203. {
  204. struct dentry *dir;
  205. int i, err = 0;
  206. struct reiserfs_dentry_buf buf = {
  207. .ctx.actor = fill_with_dentries,
  208. };
  209. /* Skip out, an xattr has no xattrs associated with it */
  210. if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
  211. return 0;
  212. dir = open_xa_dir(inode, XATTR_REPLACE);
  213. if (IS_ERR(dir)) {
  214. err = PTR_ERR(dir);
  215. goto out;
  216. } else if (d_really_is_negative(dir)) {
  217. err = 0;
  218. goto out_dir;
  219. }
  220. inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
  221. buf.xadir = dir;
  222. while (1) {
  223. err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
  224. if (err)
  225. break;
  226. if (buf.err) {
  227. err = buf.err;
  228. break;
  229. }
  230. if (!buf.count)
  231. break;
  232. for (i = 0; !err && i < buf.count && buf.dentries[i]; i++) {
  233. struct dentry *dentry = buf.dentries[i];
  234. if (!d_is_dir(dentry))
  235. err = action(dentry, data);
  236. dput(dentry);
  237. buf.dentries[i] = NULL;
  238. }
  239. if (err)
  240. break;
  241. buf.count = 0;
  242. }
  243. inode_unlock(d_inode(dir));
  244. cleanup_dentry_buf(&buf);
  245. if (!err) {
  246. /*
  247. * We start a transaction here to avoid a ABBA situation
  248. * between the xattr root's i_mutex and the journal lock.
  249. * This doesn't incur much additional overhead since the
  250. * new transaction will just nest inside the
  251. * outer transaction.
  252. */
  253. int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
  254. 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
  255. struct reiserfs_transaction_handle th;
  256. reiserfs_write_lock(inode->i_sb);
  257. err = journal_begin(&th, inode->i_sb, blocks);
  258. reiserfs_write_unlock(inode->i_sb);
  259. if (!err) {
  260. int jerror;
  261. inode_lock_nested(d_inode(dir->d_parent),
  262. I_MUTEX_XATTR);
  263. err = action(dir, data);
  264. reiserfs_write_lock(inode->i_sb);
  265. jerror = journal_end(&th);
  266. reiserfs_write_unlock(inode->i_sb);
  267. inode_unlock(d_inode(dir->d_parent));
  268. err = jerror ?: err;
  269. }
  270. }
  271. out_dir:
  272. dput(dir);
  273. out:
  274. /* -ENODATA isn't an error */
  275. if (err == -ENODATA)
  276. err = 0;
  277. return err;
  278. }
  279. static int delete_one_xattr(struct dentry *dentry, void *data)
  280. {
  281. struct inode *dir = d_inode(dentry->d_parent);
  282. /* This is the xattr dir, handle specially. */
  283. if (d_is_dir(dentry))
  284. return xattr_rmdir(dir, dentry);
  285. return xattr_unlink(dir, dentry);
  286. }
  287. static int chown_one_xattr(struct dentry *dentry, void *data)
  288. {
  289. struct iattr *attrs = data;
  290. int ia_valid = attrs->ia_valid;
  291. int err;
  292. /*
  293. * We only want the ownership bits. Otherwise, we'll do
  294. * things like change a directory to a regular file if
  295. * ATTR_MODE is set.
  296. */
  297. attrs->ia_valid &= (ATTR_UID|ATTR_GID);
  298. err = reiserfs_setattr(dentry, attrs);
  299. attrs->ia_valid = ia_valid;
  300. return err;
  301. }
  302. /* No i_mutex, but the inode is unconnected. */
  303. int reiserfs_delete_xattrs(struct inode *inode)
  304. {
  305. int err = reiserfs_for_each_xattr(inode, delete_one_xattr, NULL);
  306. if (err)
  307. reiserfs_warning(inode->i_sb, "jdm-20004",
  308. "Couldn't delete all xattrs (%d)\n", err);
  309. return err;
  310. }
  311. /* inode->i_mutex: down */
  312. int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  313. {
  314. int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
  315. if (err)
  316. reiserfs_warning(inode->i_sb, "jdm-20007",
  317. "Couldn't chown all xattrs (%d)\n", err);
  318. return err;
  319. }
  320. #ifdef CONFIG_REISERFS_FS_XATTR
  321. /*
  322. * Returns a dentry corresponding to a specific extended attribute file
  323. * for the inode. If flags allow, the file is created. Otherwise, a
  324. * valid or negative dentry, or an error is returned.
  325. */
  326. static struct dentry *xattr_lookup(struct inode *inode, const char *name,
  327. int flags)
  328. {
  329. struct dentry *xadir, *xafile;
  330. int err = 0;
  331. xadir = open_xa_dir(inode, flags);
  332. if (IS_ERR(xadir))
  333. return ERR_CAST(xadir);
  334. inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
  335. xafile = lookup_one_len(name, xadir, strlen(name));
  336. if (IS_ERR(xafile)) {
  337. err = PTR_ERR(xafile);
  338. goto out;
  339. }
  340. if (d_really_is_positive(xafile) && (flags & XATTR_CREATE))
  341. err = -EEXIST;
  342. if (d_really_is_negative(xafile)) {
  343. err = -ENODATA;
  344. if (xattr_may_create(flags))
  345. err = xattr_create(d_inode(xadir), xafile,
  346. 0700|S_IFREG);
  347. }
  348. if (err)
  349. dput(xafile);
  350. out:
  351. inode_unlock(d_inode(xadir));
  352. dput(xadir);
  353. if (err)
  354. return ERR_PTR(err);
  355. return xafile;
  356. }
  357. /* Internal operations on file data */
  358. static inline void reiserfs_put_page(struct page *page)
  359. {
  360. kunmap(page);
  361. put_page(page);
  362. }
  363. static struct page *reiserfs_get_page(struct inode *dir, size_t n)
  364. {
  365. struct address_space *mapping = dir->i_mapping;
  366. struct page *page;
  367. /*
  368. * We can deadlock if we try to free dentries,
  369. * and an unlink/rmdir has just occurred - GFP_NOFS avoids this
  370. */
  371. mapping_set_gfp_mask(mapping, GFP_NOFS);
  372. page = read_mapping_page(mapping, n >> PAGE_SHIFT, NULL);
  373. if (!IS_ERR(page)) {
  374. kmap(page);
  375. if (PageError(page))
  376. goto fail;
  377. }
  378. return page;
  379. fail:
  380. reiserfs_put_page(page);
  381. return ERR_PTR(-EIO);
  382. }
  383. static inline __u32 xattr_hash(const char *msg, int len)
  384. {
  385. return csum_partial(msg, len, 0);
  386. }
  387. int reiserfs_commit_write(struct file *f, struct page *page,
  388. unsigned from, unsigned to);
  389. static void update_ctime(struct inode *inode)
  390. {
  391. struct timespec64 now = current_time(inode);
  392. if (inode_unhashed(inode) || !inode->i_nlink ||
  393. timespec64_equal(&inode->i_ctime, &now))
  394. return;
  395. inode->i_ctime = current_time(inode);
  396. mark_inode_dirty(inode);
  397. }
  398. static int lookup_and_delete_xattr(struct inode *inode, const char *name)
  399. {
  400. int err = 0;
  401. struct dentry *dentry, *xadir;
  402. xadir = open_xa_dir(inode, XATTR_REPLACE);
  403. if (IS_ERR(xadir))
  404. return PTR_ERR(xadir);
  405. inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
  406. dentry = lookup_one_len(name, xadir, strlen(name));
  407. if (IS_ERR(dentry)) {
  408. err = PTR_ERR(dentry);
  409. goto out_dput;
  410. }
  411. if (d_really_is_positive(dentry)) {
  412. err = xattr_unlink(d_inode(xadir), dentry);
  413. update_ctime(inode);
  414. }
  415. dput(dentry);
  416. out_dput:
  417. inode_unlock(d_inode(xadir));
  418. dput(xadir);
  419. return err;
  420. }
  421. /* Generic extended attribute operations that can be used by xa plugins */
  422. /*
  423. * inode->i_mutex: down
  424. */
  425. int
  426. reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
  427. struct inode *inode, const char *name,
  428. const void *buffer, size_t buffer_size, int flags)
  429. {
  430. int err = 0;
  431. struct dentry *dentry;
  432. struct page *page;
  433. char *data;
  434. size_t file_pos = 0;
  435. size_t buffer_pos = 0;
  436. size_t new_size;
  437. __u32 xahash = 0;
  438. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  439. return -EOPNOTSUPP;
  440. if (!buffer) {
  441. err = lookup_and_delete_xattr(inode, name);
  442. return err;
  443. }
  444. dentry = xattr_lookup(inode, name, flags);
  445. if (IS_ERR(dentry))
  446. return PTR_ERR(dentry);
  447. down_write(&REISERFS_I(inode)->i_xattr_sem);
  448. xahash = xattr_hash(buffer, buffer_size);
  449. while (buffer_pos < buffer_size || buffer_pos == 0) {
  450. size_t chunk;
  451. size_t skip = 0;
  452. size_t page_offset = (file_pos & (PAGE_SIZE - 1));
  453. if (buffer_size - buffer_pos > PAGE_SIZE)
  454. chunk = PAGE_SIZE;
  455. else
  456. chunk = buffer_size - buffer_pos;
  457. page = reiserfs_get_page(d_inode(dentry), file_pos);
  458. if (IS_ERR(page)) {
  459. err = PTR_ERR(page);
  460. goto out_unlock;
  461. }
  462. lock_page(page);
  463. data = page_address(page);
  464. if (file_pos == 0) {
  465. struct reiserfs_xattr_header *rxh;
  466. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  467. if (chunk + skip > PAGE_SIZE)
  468. chunk = PAGE_SIZE - skip;
  469. rxh = (struct reiserfs_xattr_header *)data;
  470. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  471. rxh->h_hash = cpu_to_le32(xahash);
  472. }
  473. reiserfs_write_lock(inode->i_sb);
  474. err = __reiserfs_write_begin(page, page_offset, chunk + skip);
  475. if (!err) {
  476. if (buffer)
  477. memcpy(data + skip, buffer + buffer_pos, chunk);
  478. err = reiserfs_commit_write(NULL, page, page_offset,
  479. page_offset + chunk +
  480. skip);
  481. }
  482. reiserfs_write_unlock(inode->i_sb);
  483. unlock_page(page);
  484. reiserfs_put_page(page);
  485. buffer_pos += chunk;
  486. file_pos += chunk;
  487. skip = 0;
  488. if (err || buffer_size == 0 || !buffer)
  489. break;
  490. }
  491. new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
  492. if (!err && new_size < i_size_read(d_inode(dentry))) {
  493. struct iattr newattrs = {
  494. .ia_ctime = current_time(inode),
  495. .ia_size = new_size,
  496. .ia_valid = ATTR_SIZE | ATTR_CTIME,
  497. };
  498. inode_lock_nested(d_inode(dentry), I_MUTEX_XATTR);
  499. inode_dio_wait(d_inode(dentry));
  500. err = reiserfs_setattr(dentry, &newattrs);
  501. inode_unlock(d_inode(dentry));
  502. } else
  503. update_ctime(inode);
  504. out_unlock:
  505. up_write(&REISERFS_I(inode)->i_xattr_sem);
  506. dput(dentry);
  507. return err;
  508. }
  509. /* We need to start a transaction to maintain lock ordering */
  510. int reiserfs_xattr_set(struct inode *inode, const char *name,
  511. const void *buffer, size_t buffer_size, int flags)
  512. {
  513. struct reiserfs_transaction_handle th;
  514. int error, error2;
  515. size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
  516. if (!(flags & XATTR_REPLACE))
  517. jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
  518. reiserfs_write_lock(inode->i_sb);
  519. error = journal_begin(&th, inode->i_sb, jbegin_count);
  520. reiserfs_write_unlock(inode->i_sb);
  521. if (error) {
  522. return error;
  523. }
  524. error = reiserfs_xattr_set_handle(&th, inode, name,
  525. buffer, buffer_size, flags);
  526. reiserfs_write_lock(inode->i_sb);
  527. error2 = journal_end(&th);
  528. reiserfs_write_unlock(inode->i_sb);
  529. if (error == 0)
  530. error = error2;
  531. return error;
  532. }
  533. /*
  534. * inode->i_mutex: down
  535. */
  536. int
  537. reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
  538. size_t buffer_size)
  539. {
  540. ssize_t err = 0;
  541. struct dentry *dentry;
  542. size_t isize;
  543. size_t file_pos = 0;
  544. size_t buffer_pos = 0;
  545. struct page *page;
  546. __u32 hash = 0;
  547. if (name == NULL)
  548. return -EINVAL;
  549. /*
  550. * We can't have xattrs attached to v1 items since they don't have
  551. * generation numbers
  552. */
  553. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  554. return -EOPNOTSUPP;
  555. dentry = xattr_lookup(inode, name, XATTR_REPLACE);
  556. if (IS_ERR(dentry)) {
  557. err = PTR_ERR(dentry);
  558. goto out;
  559. }
  560. down_read(&REISERFS_I(inode)->i_xattr_sem);
  561. isize = i_size_read(d_inode(dentry));
  562. /* Just return the size needed */
  563. if (buffer == NULL) {
  564. err = isize - sizeof(struct reiserfs_xattr_header);
  565. goto out_unlock;
  566. }
  567. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  568. err = -ERANGE;
  569. goto out_unlock;
  570. }
  571. while (file_pos < isize) {
  572. size_t chunk;
  573. char *data;
  574. size_t skip = 0;
  575. if (isize - file_pos > PAGE_SIZE)
  576. chunk = PAGE_SIZE;
  577. else
  578. chunk = isize - file_pos;
  579. page = reiserfs_get_page(d_inode(dentry), file_pos);
  580. if (IS_ERR(page)) {
  581. err = PTR_ERR(page);
  582. goto out_unlock;
  583. }
  584. lock_page(page);
  585. data = page_address(page);
  586. if (file_pos == 0) {
  587. struct reiserfs_xattr_header *rxh =
  588. (struct reiserfs_xattr_header *)data;
  589. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  590. chunk -= skip;
  591. /* Magic doesn't match up.. */
  592. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  593. unlock_page(page);
  594. reiserfs_put_page(page);
  595. reiserfs_warning(inode->i_sb, "jdm-20001",
  596. "Invalid magic for xattr (%s) "
  597. "associated with %k", name,
  598. INODE_PKEY(inode));
  599. err = -EIO;
  600. goto out_unlock;
  601. }
  602. hash = le32_to_cpu(rxh->h_hash);
  603. }
  604. memcpy(buffer + buffer_pos, data + skip, chunk);
  605. unlock_page(page);
  606. reiserfs_put_page(page);
  607. file_pos += chunk;
  608. buffer_pos += chunk;
  609. skip = 0;
  610. }
  611. err = isize - sizeof(struct reiserfs_xattr_header);
  612. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  613. hash) {
  614. reiserfs_warning(inode->i_sb, "jdm-20002",
  615. "Invalid hash for xattr (%s) associated "
  616. "with %k", name, INODE_PKEY(inode));
  617. err = -EIO;
  618. }
  619. out_unlock:
  620. up_read(&REISERFS_I(inode)->i_xattr_sem);
  621. dput(dentry);
  622. out:
  623. return err;
  624. }
  625. /*
  626. * In order to implement different sets of xattr operations for each xattr
  627. * prefix with the generic xattr API, a filesystem should create a
  628. * null-terminated array of struct xattr_handler (one for each prefix) and
  629. * hang a pointer to it off of the s_xattr field of the superblock.
  630. *
  631. * The generic_fooxattr() functions will use this list to dispatch xattr
  632. * operations to the correct xattr_handler.
  633. */
  634. #define for_each_xattr_handler(handlers, handler) \
  635. for ((handler) = *(handlers)++; \
  636. (handler) != NULL; \
  637. (handler) = *(handlers)++)
  638. /* This is the implementation for the xattr plugin infrastructure */
  639. static inline const struct xattr_handler *
  640. find_xattr_handler_prefix(const struct xattr_handler **handlers,
  641. const char *name)
  642. {
  643. const struct xattr_handler *xah;
  644. if (!handlers)
  645. return NULL;
  646. for_each_xattr_handler(handlers, xah) {
  647. const char *prefix = xattr_prefix(xah);
  648. if (strncmp(prefix, name, strlen(prefix)) == 0)
  649. break;
  650. }
  651. return xah;
  652. }
  653. struct listxattr_buf {
  654. struct dir_context ctx;
  655. size_t size;
  656. size_t pos;
  657. char *buf;
  658. struct dentry *dentry;
  659. };
  660. static int listxattr_filler(struct dir_context *ctx, const char *name,
  661. int namelen, loff_t offset, u64 ino,
  662. unsigned int d_type)
  663. {
  664. struct listxattr_buf *b =
  665. container_of(ctx, struct listxattr_buf, ctx);
  666. size_t size;
  667. if (name[0] != '.' ||
  668. (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  669. const struct xattr_handler *handler;
  670. handler = find_xattr_handler_prefix(b->dentry->d_sb->s_xattr,
  671. name);
  672. if (!handler /* Unsupported xattr name */ ||
  673. (handler->list && !handler->list(b->dentry)))
  674. return 0;
  675. size = namelen + 1;
  676. if (b->buf) {
  677. if (b->pos + size > b->size) {
  678. b->pos = -ERANGE;
  679. return -ERANGE;
  680. }
  681. memcpy(b->buf + b->pos, name, namelen);
  682. b->buf[b->pos + namelen] = 0;
  683. }
  684. b->pos += size;
  685. }
  686. return 0;
  687. }
  688. /*
  689. * Inode operation listxattr()
  690. *
  691. * We totally ignore the generic listxattr here because it would be stupid
  692. * not to. Since the xattrs are organized in a directory, we can just
  693. * readdir to find them.
  694. */
  695. ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
  696. {
  697. struct dentry *dir;
  698. int err = 0;
  699. struct listxattr_buf buf = {
  700. .ctx.actor = listxattr_filler,
  701. .dentry = dentry,
  702. .buf = buffer,
  703. .size = buffer ? size : 0,
  704. };
  705. if (d_really_is_negative(dentry))
  706. return -EINVAL;
  707. if (!dentry->d_sb->s_xattr ||
  708. get_inode_sd_version(d_inode(dentry)) == STAT_DATA_V1)
  709. return -EOPNOTSUPP;
  710. dir = open_xa_dir(d_inode(dentry), XATTR_REPLACE);
  711. if (IS_ERR(dir)) {
  712. err = PTR_ERR(dir);
  713. if (err == -ENODATA)
  714. err = 0; /* Not an error if there aren't any xattrs */
  715. goto out;
  716. }
  717. inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
  718. err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
  719. inode_unlock(d_inode(dir));
  720. if (!err)
  721. err = buf.pos;
  722. dput(dir);
  723. out:
  724. return err;
  725. }
  726. static int create_privroot(struct dentry *dentry)
  727. {
  728. int err;
  729. struct inode *inode = d_inode(dentry->d_parent);
  730. WARN_ON_ONCE(!inode_is_locked(inode));
  731. err = xattr_mkdir(inode, dentry, 0700);
  732. if (err || d_really_is_negative(dentry)) {
  733. reiserfs_warning(dentry->d_sb, "jdm-20006",
  734. "xattrs/ACLs enabled and couldn't "
  735. "find/create .reiserfs_priv. "
  736. "Failing mount.");
  737. return -EOPNOTSUPP;
  738. }
  739. d_inode(dentry)->i_flags |= S_PRIVATE;
  740. reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
  741. "storage.\n", PRIVROOT_NAME);
  742. return 0;
  743. }
  744. #else
  745. int __init reiserfs_xattr_register_handlers(void) { return 0; }
  746. void reiserfs_xattr_unregister_handlers(void) {}
  747. static int create_privroot(struct dentry *dentry) { return 0; }
  748. #endif
  749. /* Actual operations that are exported to VFS-land */
  750. static const struct xattr_handler *reiserfs_xattr_handlers[] = {
  751. #ifdef CONFIG_REISERFS_FS_XATTR
  752. &reiserfs_xattr_user_handler,
  753. &reiserfs_xattr_trusted_handler,
  754. #endif
  755. #ifdef CONFIG_REISERFS_FS_SECURITY
  756. &reiserfs_xattr_security_handler,
  757. #endif
  758. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  759. &posix_acl_access_xattr_handler,
  760. &posix_acl_default_xattr_handler,
  761. #endif
  762. NULL
  763. };
  764. static int xattr_mount_check(struct super_block *s)
  765. {
  766. /*
  767. * We need generation numbers to ensure that the oid mapping is correct
  768. * v3.5 filesystems don't have them.
  769. */
  770. if (old_format_only(s)) {
  771. if (reiserfs_xattrs_optional(s)) {
  772. /*
  773. * Old format filesystem, but optional xattrs have
  774. * been enabled. Error out.
  775. */
  776. reiserfs_warning(s, "jdm-2005",
  777. "xattrs/ACLs not supported "
  778. "on pre-v3.6 format filesystems. "
  779. "Failing mount.");
  780. return -EOPNOTSUPP;
  781. }
  782. }
  783. return 0;
  784. }
  785. int reiserfs_permission(struct inode *inode, int mask)
  786. {
  787. /*
  788. * We don't do permission checks on the internal objects.
  789. * Permissions are determined by the "owning" object.
  790. */
  791. if (IS_PRIVATE(inode))
  792. return 0;
  793. return generic_permission(inode, mask);
  794. }
  795. static int xattr_hide_revalidate(struct dentry *dentry, unsigned int flags)
  796. {
  797. return -EPERM;
  798. }
  799. static const struct dentry_operations xattr_lookup_poison_ops = {
  800. .d_revalidate = xattr_hide_revalidate,
  801. };
  802. int reiserfs_lookup_privroot(struct super_block *s)
  803. {
  804. struct dentry *dentry;
  805. int err = 0;
  806. /* If we don't have the privroot located yet - go find it */
  807. inode_lock(d_inode(s->s_root));
  808. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  809. strlen(PRIVROOT_NAME));
  810. if (!IS_ERR(dentry)) {
  811. REISERFS_SB(s)->priv_root = dentry;
  812. d_set_d_op(dentry, &xattr_lookup_poison_ops);
  813. if (d_really_is_positive(dentry))
  814. d_inode(dentry)->i_flags |= S_PRIVATE;
  815. } else
  816. err = PTR_ERR(dentry);
  817. inode_unlock(d_inode(s->s_root));
  818. return err;
  819. }
  820. /*
  821. * We need to take a copy of the mount flags since things like
  822. * SB_RDONLY don't get set until *after* we're called.
  823. * mount_flags != mount_options
  824. */
  825. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  826. {
  827. int err = 0;
  828. struct dentry *privroot = REISERFS_SB(s)->priv_root;
  829. err = xattr_mount_check(s);
  830. if (err)
  831. goto error;
  832. if (d_really_is_negative(privroot) && !(mount_flags & SB_RDONLY)) {
  833. inode_lock(d_inode(s->s_root));
  834. err = create_privroot(REISERFS_SB(s)->priv_root);
  835. inode_unlock(d_inode(s->s_root));
  836. }
  837. if (d_really_is_positive(privroot)) {
  838. s->s_xattr = reiserfs_xattr_handlers;
  839. inode_lock(d_inode(privroot));
  840. if (!REISERFS_SB(s)->xattr_root) {
  841. struct dentry *dentry;
  842. dentry = lookup_one_len(XAROOT_NAME, privroot,
  843. strlen(XAROOT_NAME));
  844. if (!IS_ERR(dentry))
  845. REISERFS_SB(s)->xattr_root = dentry;
  846. else
  847. err = PTR_ERR(dentry);
  848. }
  849. inode_unlock(d_inode(privroot));
  850. }
  851. error:
  852. if (err) {
  853. clear_bit(REISERFS_XATTRS_USER, &REISERFS_SB(s)->s_mount_opt);
  854. clear_bit(REISERFS_POSIXACL, &REISERFS_SB(s)->s_mount_opt);
  855. }
  856. /* The super_block SB_POSIXACL must mirror the (no)acl mount option. */
  857. if (reiserfs_posixacl(s))
  858. s->s_flags |= SB_POSIXACL;
  859. else
  860. s->s_flags &= ~SB_POSIXACL;
  861. return err;
  862. }