xattr.c 24 KB

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