xattr.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. struct dentry *dentries[8];
  160. };
  161. static int
  162. fill_with_dentries(struct dir_context *ctx, const char *name, int namelen,
  163. loff_t offset, u64 ino, unsigned int d_type)
  164. {
  165. struct reiserfs_dentry_buf *dbuf =
  166. container_of(ctx, struct reiserfs_dentry_buf, ctx);
  167. struct dentry *dentry;
  168. WARN_ON_ONCE(!inode_is_locked(d_inode(dbuf->xadir)));
  169. if (dbuf->count == ARRAY_SIZE(dbuf->dentries))
  170. return -ENOSPC;
  171. if (name[0] == '.' && (namelen < 2 ||
  172. (namelen == 2 && name[1] == '.')))
  173. return 0;
  174. dentry = lookup_one_len(name, dbuf->xadir, namelen);
  175. if (IS_ERR(dentry)) {
  176. return PTR_ERR(dentry);
  177. } else if (d_really_is_negative(dentry)) {
  178. /* A directory entry exists, but no file? */
  179. reiserfs_error(dentry->d_sb, "xattr-20003",
  180. "Corrupted directory: xattr %pd listed but "
  181. "not found for file %pd.\n",
  182. dentry, dbuf->xadir);
  183. dput(dentry);
  184. return -EIO;
  185. }
  186. dbuf->dentries[dbuf->count++] = dentry;
  187. return 0;
  188. }
  189. static void
  190. cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
  191. {
  192. int i;
  193. for (i = 0; i < buf->count; i++)
  194. if (buf->dentries[i])
  195. dput(buf->dentries[i]);
  196. }
  197. static int reiserfs_for_each_xattr(struct inode *inode,
  198. int (*action)(struct dentry *, void *),
  199. void *data)
  200. {
  201. struct dentry *dir;
  202. int i, err = 0;
  203. struct reiserfs_dentry_buf buf = {
  204. .ctx.actor = fill_with_dentries,
  205. };
  206. /* Skip out, an xattr has no xattrs associated with it */
  207. if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
  208. return 0;
  209. dir = open_xa_dir(inode, XATTR_REPLACE);
  210. if (IS_ERR(dir)) {
  211. err = PTR_ERR(dir);
  212. goto out;
  213. } else if (d_really_is_negative(dir)) {
  214. err = 0;
  215. goto out_dir;
  216. }
  217. inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
  218. buf.xadir = dir;
  219. while (1) {
  220. err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
  221. if (err)
  222. break;
  223. if (!buf.count)
  224. break;
  225. for (i = 0; !err && i < buf.count && buf.dentries[i]; i++) {
  226. struct dentry *dentry = buf.dentries[i];
  227. if (!d_is_dir(dentry))
  228. err = action(dentry, data);
  229. dput(dentry);
  230. buf.dentries[i] = NULL;
  231. }
  232. if (err)
  233. break;
  234. buf.count = 0;
  235. }
  236. inode_unlock(d_inode(dir));
  237. cleanup_dentry_buf(&buf);
  238. if (!err) {
  239. /*
  240. * We start a transaction here to avoid a ABBA situation
  241. * between the xattr root's i_mutex and the journal lock.
  242. * This doesn't incur much additional overhead since the
  243. * new transaction will just nest inside the
  244. * outer transaction.
  245. */
  246. int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
  247. 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
  248. struct reiserfs_transaction_handle th;
  249. reiserfs_write_lock(inode->i_sb);
  250. err = journal_begin(&th, inode->i_sb, blocks);
  251. reiserfs_write_unlock(inode->i_sb);
  252. if (!err) {
  253. int jerror;
  254. inode_lock_nested(d_inode(dir->d_parent),
  255. I_MUTEX_XATTR);
  256. err = action(dir, data);
  257. reiserfs_write_lock(inode->i_sb);
  258. jerror = journal_end(&th);
  259. reiserfs_write_unlock(inode->i_sb);
  260. inode_unlock(d_inode(dir->d_parent));
  261. err = jerror ?: err;
  262. }
  263. }
  264. out_dir:
  265. dput(dir);
  266. out:
  267. /* -ENODATA isn't an error */
  268. if (err == -ENODATA)
  269. err = 0;
  270. return err;
  271. }
  272. static int delete_one_xattr(struct dentry *dentry, void *data)
  273. {
  274. struct inode *dir = d_inode(dentry->d_parent);
  275. /* This is the xattr dir, handle specially. */
  276. if (d_is_dir(dentry))
  277. return xattr_rmdir(dir, dentry);
  278. return xattr_unlink(dir, dentry);
  279. }
  280. static int chown_one_xattr(struct dentry *dentry, void *data)
  281. {
  282. struct iattr *attrs = data;
  283. int ia_valid = attrs->ia_valid;
  284. int err;
  285. /*
  286. * We only want the ownership bits. Otherwise, we'll do
  287. * things like change a directory to a regular file if
  288. * ATTR_MODE is set.
  289. */
  290. attrs->ia_valid &= (ATTR_UID|ATTR_GID);
  291. err = reiserfs_setattr(dentry, attrs);
  292. attrs->ia_valid = ia_valid;
  293. return err;
  294. }
  295. /* No i_mutex, but the inode is unconnected. */
  296. int reiserfs_delete_xattrs(struct inode *inode)
  297. {
  298. int err = reiserfs_for_each_xattr(inode, delete_one_xattr, NULL);
  299. if (err)
  300. reiserfs_warning(inode->i_sb, "jdm-20004",
  301. "Couldn't delete all xattrs (%d)\n", err);
  302. return err;
  303. }
  304. /* inode->i_mutex: down */
  305. int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  306. {
  307. int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
  308. if (err)
  309. reiserfs_warning(inode->i_sb, "jdm-20007",
  310. "Couldn't chown all xattrs (%d)\n", err);
  311. return err;
  312. }
  313. #ifdef CONFIG_REISERFS_FS_XATTR
  314. /*
  315. * Returns a dentry corresponding to a specific extended attribute file
  316. * for the inode. If flags allow, the file is created. Otherwise, a
  317. * valid or negative dentry, or an error is returned.
  318. */
  319. static struct dentry *xattr_lookup(struct inode *inode, const char *name,
  320. int flags)
  321. {
  322. struct dentry *xadir, *xafile;
  323. int err = 0;
  324. xadir = open_xa_dir(inode, flags);
  325. if (IS_ERR(xadir))
  326. return ERR_CAST(xadir);
  327. inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
  328. xafile = lookup_one_len(name, xadir, strlen(name));
  329. if (IS_ERR(xafile)) {
  330. err = PTR_ERR(xafile);
  331. goto out;
  332. }
  333. if (d_really_is_positive(xafile) && (flags & XATTR_CREATE))
  334. err = -EEXIST;
  335. if (d_really_is_negative(xafile)) {
  336. err = -ENODATA;
  337. if (xattr_may_create(flags))
  338. err = xattr_create(d_inode(xadir), xafile,
  339. 0700|S_IFREG);
  340. }
  341. if (err)
  342. dput(xafile);
  343. out:
  344. inode_unlock(d_inode(xadir));
  345. dput(xadir);
  346. if (err)
  347. return ERR_PTR(err);
  348. return xafile;
  349. }
  350. /* Internal operations on file data */
  351. static inline void reiserfs_put_page(struct page *page)
  352. {
  353. kunmap(page);
  354. put_page(page);
  355. }
  356. static struct page *reiserfs_get_page(struct inode *dir, size_t n)
  357. {
  358. struct address_space *mapping = dir->i_mapping;
  359. struct page *page;
  360. /*
  361. * We can deadlock if we try to free dentries,
  362. * and an unlink/rmdir has just occurred - GFP_NOFS avoids this
  363. */
  364. mapping_set_gfp_mask(mapping, GFP_NOFS);
  365. page = read_mapping_page(mapping, n >> PAGE_SHIFT, NULL);
  366. if (!IS_ERR(page)) {
  367. kmap(page);
  368. if (PageError(page))
  369. goto fail;
  370. }
  371. return page;
  372. fail:
  373. reiserfs_put_page(page);
  374. return ERR_PTR(-EIO);
  375. }
  376. static inline __u32 xattr_hash(const char *msg, int len)
  377. {
  378. return csum_partial(msg, len, 0);
  379. }
  380. int reiserfs_commit_write(struct file *f, struct page *page,
  381. unsigned from, unsigned to);
  382. static void update_ctime(struct inode *inode)
  383. {
  384. struct timespec now = current_time(inode);
  385. if (inode_unhashed(inode) || !inode->i_nlink ||
  386. timespec_equal(&inode->i_ctime, &now))
  387. return;
  388. inode->i_ctime = current_time(inode);
  389. mark_inode_dirty(inode);
  390. }
  391. static int lookup_and_delete_xattr(struct inode *inode, const char *name)
  392. {
  393. int err = 0;
  394. struct dentry *dentry, *xadir;
  395. xadir = open_xa_dir(inode, XATTR_REPLACE);
  396. if (IS_ERR(xadir))
  397. return PTR_ERR(xadir);
  398. inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
  399. dentry = lookup_one_len(name, xadir, strlen(name));
  400. if (IS_ERR(dentry)) {
  401. err = PTR_ERR(dentry);
  402. goto out_dput;
  403. }
  404. if (d_really_is_positive(dentry)) {
  405. err = xattr_unlink(d_inode(xadir), dentry);
  406. update_ctime(inode);
  407. }
  408. dput(dentry);
  409. out_dput:
  410. inode_unlock(d_inode(xadir));
  411. dput(xadir);
  412. return err;
  413. }
  414. /* Generic extended attribute operations that can be used by xa plugins */
  415. /*
  416. * inode->i_mutex: down
  417. */
  418. int
  419. reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
  420. struct inode *inode, const char *name,
  421. const void *buffer, size_t buffer_size, int flags)
  422. {
  423. int err = 0;
  424. struct dentry *dentry;
  425. struct page *page;
  426. char *data;
  427. size_t file_pos = 0;
  428. size_t buffer_pos = 0;
  429. size_t new_size;
  430. __u32 xahash = 0;
  431. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  432. return -EOPNOTSUPP;
  433. if (!buffer) {
  434. err = lookup_and_delete_xattr(inode, name);
  435. return err;
  436. }
  437. dentry = xattr_lookup(inode, name, flags);
  438. if (IS_ERR(dentry))
  439. return PTR_ERR(dentry);
  440. down_write(&REISERFS_I(inode)->i_xattr_sem);
  441. xahash = xattr_hash(buffer, buffer_size);
  442. while (buffer_pos < buffer_size || buffer_pos == 0) {
  443. size_t chunk;
  444. size_t skip = 0;
  445. size_t page_offset = (file_pos & (PAGE_SIZE - 1));
  446. if (buffer_size - buffer_pos > PAGE_SIZE)
  447. chunk = PAGE_SIZE;
  448. else
  449. chunk = buffer_size - buffer_pos;
  450. page = reiserfs_get_page(d_inode(dentry), file_pos);
  451. if (IS_ERR(page)) {
  452. err = PTR_ERR(page);
  453. goto out_unlock;
  454. }
  455. lock_page(page);
  456. data = page_address(page);
  457. if (file_pos == 0) {
  458. struct reiserfs_xattr_header *rxh;
  459. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  460. if (chunk + skip > PAGE_SIZE)
  461. chunk = PAGE_SIZE - skip;
  462. rxh = (struct reiserfs_xattr_header *)data;
  463. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  464. rxh->h_hash = cpu_to_le32(xahash);
  465. }
  466. reiserfs_write_lock(inode->i_sb);
  467. err = __reiserfs_write_begin(page, page_offset, chunk + skip);
  468. if (!err) {
  469. if (buffer)
  470. memcpy(data + skip, buffer + buffer_pos, chunk);
  471. err = reiserfs_commit_write(NULL, page, page_offset,
  472. page_offset + chunk +
  473. skip);
  474. }
  475. reiserfs_write_unlock(inode->i_sb);
  476. unlock_page(page);
  477. reiserfs_put_page(page);
  478. buffer_pos += chunk;
  479. file_pos += chunk;
  480. skip = 0;
  481. if (err || buffer_size == 0 || !buffer)
  482. break;
  483. }
  484. new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
  485. if (!err && new_size < i_size_read(d_inode(dentry))) {
  486. struct iattr newattrs = {
  487. .ia_ctime = current_time(inode),
  488. .ia_size = new_size,
  489. .ia_valid = ATTR_SIZE | ATTR_CTIME,
  490. };
  491. inode_lock_nested(d_inode(dentry), I_MUTEX_XATTR);
  492. inode_dio_wait(d_inode(dentry));
  493. err = reiserfs_setattr(dentry, &newattrs);
  494. inode_unlock(d_inode(dentry));
  495. } else
  496. update_ctime(inode);
  497. out_unlock:
  498. up_write(&REISERFS_I(inode)->i_xattr_sem);
  499. dput(dentry);
  500. return err;
  501. }
  502. /* We need to start a transaction to maintain lock ordering */
  503. int reiserfs_xattr_set(struct inode *inode, const char *name,
  504. const void *buffer, size_t buffer_size, int flags)
  505. {
  506. struct reiserfs_transaction_handle th;
  507. int error, error2;
  508. size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
  509. if (!(flags & XATTR_REPLACE))
  510. jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
  511. reiserfs_write_lock(inode->i_sb);
  512. error = journal_begin(&th, inode->i_sb, jbegin_count);
  513. reiserfs_write_unlock(inode->i_sb);
  514. if (error) {
  515. return error;
  516. }
  517. error = reiserfs_xattr_set_handle(&th, inode, name,
  518. buffer, buffer_size, flags);
  519. reiserfs_write_lock(inode->i_sb);
  520. error2 = journal_end(&th);
  521. reiserfs_write_unlock(inode->i_sb);
  522. if (error == 0)
  523. error = error2;
  524. return error;
  525. }
  526. /*
  527. * inode->i_mutex: down
  528. */
  529. int
  530. reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
  531. size_t buffer_size)
  532. {
  533. ssize_t err = 0;
  534. struct dentry *dentry;
  535. size_t isize;
  536. size_t file_pos = 0;
  537. size_t buffer_pos = 0;
  538. struct page *page;
  539. __u32 hash = 0;
  540. if (name == NULL)
  541. return -EINVAL;
  542. /*
  543. * We can't have xattrs attached to v1 items since they don't have
  544. * generation numbers
  545. */
  546. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  547. return -EOPNOTSUPP;
  548. dentry = xattr_lookup(inode, name, XATTR_REPLACE);
  549. if (IS_ERR(dentry)) {
  550. err = PTR_ERR(dentry);
  551. goto out;
  552. }
  553. down_read(&REISERFS_I(inode)->i_xattr_sem);
  554. isize = i_size_read(d_inode(dentry));
  555. /* Just return the size needed */
  556. if (buffer == NULL) {
  557. err = isize - sizeof(struct reiserfs_xattr_header);
  558. goto out_unlock;
  559. }
  560. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  561. err = -ERANGE;
  562. goto out_unlock;
  563. }
  564. while (file_pos < isize) {
  565. size_t chunk;
  566. char *data;
  567. size_t skip = 0;
  568. if (isize - file_pos > PAGE_SIZE)
  569. chunk = PAGE_SIZE;
  570. else
  571. chunk = isize - file_pos;
  572. page = reiserfs_get_page(d_inode(dentry), file_pos);
  573. if (IS_ERR(page)) {
  574. err = PTR_ERR(page);
  575. goto out_unlock;
  576. }
  577. lock_page(page);
  578. data = page_address(page);
  579. if (file_pos == 0) {
  580. struct reiserfs_xattr_header *rxh =
  581. (struct reiserfs_xattr_header *)data;
  582. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  583. chunk -= skip;
  584. /* Magic doesn't match up.. */
  585. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  586. unlock_page(page);
  587. reiserfs_put_page(page);
  588. reiserfs_warning(inode->i_sb, "jdm-20001",
  589. "Invalid magic for xattr (%s) "
  590. "associated with %k", name,
  591. INODE_PKEY(inode));
  592. err = -EIO;
  593. goto out_unlock;
  594. }
  595. hash = le32_to_cpu(rxh->h_hash);
  596. }
  597. memcpy(buffer + buffer_pos, data + skip, chunk);
  598. unlock_page(page);
  599. reiserfs_put_page(page);
  600. file_pos += chunk;
  601. buffer_pos += chunk;
  602. skip = 0;
  603. }
  604. err = isize - sizeof(struct reiserfs_xattr_header);
  605. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  606. hash) {
  607. reiserfs_warning(inode->i_sb, "jdm-20002",
  608. "Invalid hash for xattr (%s) associated "
  609. "with %k", name, INODE_PKEY(inode));
  610. err = -EIO;
  611. }
  612. out_unlock:
  613. up_read(&REISERFS_I(inode)->i_xattr_sem);
  614. dput(dentry);
  615. out:
  616. return err;
  617. }
  618. /*
  619. * In order to implement different sets of xattr operations for each xattr
  620. * prefix with the generic xattr API, a filesystem should create a
  621. * null-terminated array of struct xattr_handler (one for each prefix) and
  622. * hang a pointer to it off of the s_xattr field of the superblock.
  623. *
  624. * The generic_fooxattr() functions will use this list to dispatch xattr
  625. * operations to the correct xattr_handler.
  626. */
  627. #define for_each_xattr_handler(handlers, handler) \
  628. for ((handler) = *(handlers)++; \
  629. (handler) != NULL; \
  630. (handler) = *(handlers)++)
  631. /* This is the implementation for the xattr plugin infrastructure */
  632. static inline const struct xattr_handler *
  633. find_xattr_handler_prefix(const struct xattr_handler **handlers,
  634. const char *name)
  635. {
  636. const struct xattr_handler *xah;
  637. if (!handlers)
  638. return NULL;
  639. for_each_xattr_handler(handlers, xah) {
  640. const char *prefix = xattr_prefix(xah);
  641. if (strncmp(prefix, name, strlen(prefix)) == 0)
  642. break;
  643. }
  644. return xah;
  645. }
  646. struct listxattr_buf {
  647. struct dir_context ctx;
  648. size_t size;
  649. size_t pos;
  650. char *buf;
  651. struct dentry *dentry;
  652. };
  653. static int listxattr_filler(struct dir_context *ctx, const char *name,
  654. int namelen, loff_t offset, u64 ino,
  655. unsigned int d_type)
  656. {
  657. struct listxattr_buf *b =
  658. container_of(ctx, struct listxattr_buf, ctx);
  659. size_t size;
  660. if (name[0] != '.' ||
  661. (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  662. const struct xattr_handler *handler;
  663. handler = find_xattr_handler_prefix(b->dentry->d_sb->s_xattr,
  664. name);
  665. if (!handler /* Unsupported xattr name */ ||
  666. (handler->list && !handler->list(b->dentry)))
  667. return 0;
  668. size = namelen + 1;
  669. if (b->buf) {
  670. if (size > b->size)
  671. return -ERANGE;
  672. memcpy(b->buf + b->pos, name, namelen);
  673. b->buf[b->pos + namelen] = 0;
  674. }
  675. b->pos += size;
  676. }
  677. return 0;
  678. }
  679. /*
  680. * Inode operation listxattr()
  681. *
  682. * We totally ignore the generic listxattr here because it would be stupid
  683. * not to. Since the xattrs are organized in a directory, we can just
  684. * readdir to find them.
  685. */
  686. ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
  687. {
  688. struct dentry *dir;
  689. int err = 0;
  690. struct listxattr_buf buf = {
  691. .ctx.actor = listxattr_filler,
  692. .dentry = dentry,
  693. .buf = buffer,
  694. .size = buffer ? size : 0,
  695. };
  696. if (d_really_is_negative(dentry))
  697. return -EINVAL;
  698. if (!dentry->d_sb->s_xattr ||
  699. get_inode_sd_version(d_inode(dentry)) == STAT_DATA_V1)
  700. return -EOPNOTSUPP;
  701. dir = open_xa_dir(d_inode(dentry), XATTR_REPLACE);
  702. if (IS_ERR(dir)) {
  703. err = PTR_ERR(dir);
  704. if (err == -ENODATA)
  705. err = 0; /* Not an error if there aren't any xattrs */
  706. goto out;
  707. }
  708. inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
  709. err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
  710. inode_unlock(d_inode(dir));
  711. if (!err)
  712. err = buf.pos;
  713. dput(dir);
  714. out:
  715. return err;
  716. }
  717. static int create_privroot(struct dentry *dentry)
  718. {
  719. int err;
  720. struct inode *inode = d_inode(dentry->d_parent);
  721. WARN_ON_ONCE(!inode_is_locked(inode));
  722. err = xattr_mkdir(inode, dentry, 0700);
  723. if (err || d_really_is_negative(dentry)) {
  724. reiserfs_warning(dentry->d_sb, "jdm-20006",
  725. "xattrs/ACLs enabled and couldn't "
  726. "find/create .reiserfs_priv. "
  727. "Failing mount.");
  728. return -EOPNOTSUPP;
  729. }
  730. d_inode(dentry)->i_flags |= S_PRIVATE;
  731. reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
  732. "storage.\n", PRIVROOT_NAME);
  733. return 0;
  734. }
  735. #else
  736. int __init reiserfs_xattr_register_handlers(void) { return 0; }
  737. void reiserfs_xattr_unregister_handlers(void) {}
  738. static int create_privroot(struct dentry *dentry) { return 0; }
  739. #endif
  740. /* Actual operations that are exported to VFS-land */
  741. static const struct xattr_handler *reiserfs_xattr_handlers[] = {
  742. #ifdef CONFIG_REISERFS_FS_XATTR
  743. &reiserfs_xattr_user_handler,
  744. &reiserfs_xattr_trusted_handler,
  745. #endif
  746. #ifdef CONFIG_REISERFS_FS_SECURITY
  747. &reiserfs_xattr_security_handler,
  748. #endif
  749. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  750. &posix_acl_access_xattr_handler,
  751. &posix_acl_default_xattr_handler,
  752. #endif
  753. NULL
  754. };
  755. static int xattr_mount_check(struct super_block *s)
  756. {
  757. /*
  758. * We need generation numbers to ensure that the oid mapping is correct
  759. * v3.5 filesystems don't have them.
  760. */
  761. if (old_format_only(s)) {
  762. if (reiserfs_xattrs_optional(s)) {
  763. /*
  764. * Old format filesystem, but optional xattrs have
  765. * been enabled. Error out.
  766. */
  767. reiserfs_warning(s, "jdm-2005",
  768. "xattrs/ACLs not supported "
  769. "on pre-v3.6 format filesystems. "
  770. "Failing mount.");
  771. return -EOPNOTSUPP;
  772. }
  773. }
  774. return 0;
  775. }
  776. int reiserfs_permission(struct inode *inode, int mask)
  777. {
  778. /*
  779. * We don't do permission checks on the internal objects.
  780. * Permissions are determined by the "owning" object.
  781. */
  782. if (IS_PRIVATE(inode))
  783. return 0;
  784. return generic_permission(inode, mask);
  785. }
  786. static int xattr_hide_revalidate(struct dentry *dentry, unsigned int flags)
  787. {
  788. return -EPERM;
  789. }
  790. static const struct dentry_operations xattr_lookup_poison_ops = {
  791. .d_revalidate = xattr_hide_revalidate,
  792. };
  793. int reiserfs_lookup_privroot(struct super_block *s)
  794. {
  795. struct dentry *dentry;
  796. int err = 0;
  797. /* If we don't have the privroot located yet - go find it */
  798. inode_lock(d_inode(s->s_root));
  799. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  800. strlen(PRIVROOT_NAME));
  801. if (!IS_ERR(dentry)) {
  802. REISERFS_SB(s)->priv_root = dentry;
  803. d_set_d_op(dentry, &xattr_lookup_poison_ops);
  804. if (d_really_is_positive(dentry))
  805. d_inode(dentry)->i_flags |= S_PRIVATE;
  806. } else
  807. err = PTR_ERR(dentry);
  808. inode_unlock(d_inode(s->s_root));
  809. return err;
  810. }
  811. /*
  812. * We need to take a copy of the mount flags since things like
  813. * SB_RDONLY don't get set until *after* we're called.
  814. * mount_flags != mount_options
  815. */
  816. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  817. {
  818. int err = 0;
  819. struct dentry *privroot = REISERFS_SB(s)->priv_root;
  820. err = xattr_mount_check(s);
  821. if (err)
  822. goto error;
  823. if (d_really_is_negative(privroot) && !(mount_flags & SB_RDONLY)) {
  824. inode_lock(d_inode(s->s_root));
  825. err = create_privroot(REISERFS_SB(s)->priv_root);
  826. inode_unlock(d_inode(s->s_root));
  827. }
  828. if (d_really_is_positive(privroot)) {
  829. s->s_xattr = reiserfs_xattr_handlers;
  830. inode_lock(d_inode(privroot));
  831. if (!REISERFS_SB(s)->xattr_root) {
  832. struct dentry *dentry;
  833. dentry = lookup_one_len(XAROOT_NAME, privroot,
  834. strlen(XAROOT_NAME));
  835. if (!IS_ERR(dentry))
  836. REISERFS_SB(s)->xattr_root = dentry;
  837. else
  838. err = PTR_ERR(dentry);
  839. }
  840. inode_unlock(d_inode(privroot));
  841. }
  842. error:
  843. if (err) {
  844. clear_bit(REISERFS_XATTRS_USER, &REISERFS_SB(s)->s_mount_opt);
  845. clear_bit(REISERFS_POSIXACL, &REISERFS_SB(s)->s_mount_opt);
  846. }
  847. /* The super_block SB_POSIXACL must mirror the (no)acl mount option. */
  848. if (reiserfs_posixacl(s))
  849. s->s_flags |= SB_POSIXACL;
  850. else
  851. s->s_flags &= ~SB_POSIXACL;
  852. return err;
  853. }