xattr.c 26 KB

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