xattr.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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 <linux/reiserfs_fs.h>
  35. #include <linux/capability.h>
  36. #include <linux/dcache.h>
  37. #include <linux/namei.h>
  38. #include <linux/errno.h>
  39. #include <linux/fs.h>
  40. #include <linux/file.h>
  41. #include <linux/pagemap.h>
  42. #include <linux/xattr.h>
  43. #include <linux/reiserfs_xattr.h>
  44. #include <linux/reiserfs_acl.h>
  45. #include <asm/uaccess.h>
  46. #include <net/checksum.h>
  47. #include <linux/smp_lock.h>
  48. #include <linux/stat.h>
  49. #include <linux/quotaops.h>
  50. #define PRIVROOT_NAME ".reiserfs_priv"
  51. #define XAROOT_NAME "xattrs"
  52. /* Helpers for inode ops. We do this so that we don't have all the VFS
  53. * overhead and also for proper i_mutex annotation.
  54. * dir->i_mutex must be held for all of them. */
  55. static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
  56. {
  57. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  58. DQUOT_INIT(dir);
  59. return dir->i_op->create(dir, dentry, mode, NULL);
  60. }
  61. static int xattr_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  62. {
  63. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  64. DQUOT_INIT(dir);
  65. return dir->i_op->mkdir(dir, dentry, mode);
  66. }
  67. /* We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
  68. * mutation ops aren't called during rename or splace, which are the
  69. * only other users of I_MUTEX_CHILD. It violates the ordering, but that's
  70. * better than allocating another subclass just for this code. */
  71. static int xattr_unlink(struct inode *dir, struct dentry *dentry)
  72. {
  73. int error;
  74. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  75. DQUOT_INIT(dir);
  76. mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
  77. error = dir->i_op->unlink(dir, dentry);
  78. mutex_unlock(&dentry->d_inode->i_mutex);
  79. if (!error)
  80. d_delete(dentry);
  81. return error;
  82. }
  83. static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
  84. {
  85. int error;
  86. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  87. DQUOT_INIT(dir);
  88. mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
  89. dentry_unhash(dentry);
  90. error = dir->i_op->rmdir(dir, dentry);
  91. if (!error)
  92. dentry->d_inode->i_flags |= S_DEAD;
  93. mutex_unlock(&dentry->d_inode->i_mutex);
  94. if (!error)
  95. d_delete(dentry);
  96. dput(dentry);
  97. return error;
  98. }
  99. #define xattr_may_create(flags) (!flags || flags & XATTR_CREATE)
  100. /* Returns and possibly creates the xattr dir. */
  101. static struct dentry *lookup_or_create_dir(struct dentry *parent,
  102. const char *name, int flags)
  103. {
  104. struct dentry *dentry;
  105. BUG_ON(!parent);
  106. dentry = lookup_one_len(name, parent, strlen(name));
  107. if (IS_ERR(dentry))
  108. return dentry;
  109. else if (!dentry->d_inode) {
  110. int err = -ENODATA;
  111. if (xattr_may_create(flags)) {
  112. mutex_lock_nested(&parent->d_inode->i_mutex,
  113. I_MUTEX_XATTR);
  114. err = xattr_mkdir(parent->d_inode, dentry, 0700);
  115. mutex_unlock(&parent->d_inode->i_mutex);
  116. }
  117. if (err) {
  118. dput(dentry);
  119. dentry = ERR_PTR(err);
  120. }
  121. }
  122. return dentry;
  123. }
  124. static struct dentry *open_xa_root(struct super_block *sb, int flags)
  125. {
  126. struct dentry *privroot = REISERFS_SB(sb)->priv_root;
  127. if (!privroot)
  128. return ERR_PTR(-ENODATA);
  129. return lookup_or_create_dir(privroot, XAROOT_NAME, flags);
  130. }
  131. static struct dentry *open_xa_dir(const struct inode *inode, int flags)
  132. {
  133. struct dentry *xaroot, *xadir;
  134. char namebuf[17];
  135. xaroot = open_xa_root(inode->i_sb, flags);
  136. if (IS_ERR(xaroot))
  137. return xaroot;
  138. snprintf(namebuf, sizeof(namebuf), "%X.%X",
  139. le32_to_cpu(INODE_PKEY(inode)->k_objectid),
  140. inode->i_generation);
  141. xadir = lookup_or_create_dir(xaroot, namebuf, flags);
  142. dput(xaroot);
  143. return xadir;
  144. }
  145. /*
  146. * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
  147. * we need to drop the path before calling the filldir struct. That
  148. * would be a big performance hit to the non-xattr case, so I've copied
  149. * the whole thing for now. --clm
  150. *
  151. * the big difference is that I go backwards through the directory,
  152. * and don't mess with f->f_pos, but the idea is the same. Do some
  153. * action on each and every entry in the directory.
  154. *
  155. * we're called with i_mutex held, so there are no worries about the directory
  156. * changing underneath us.
  157. */
  158. static int __xattr_readdir(struct inode *inode, void *dirent, filldir_t filldir)
  159. {
  160. struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
  161. INITIALIZE_PATH(path_to_entry);
  162. struct buffer_head *bh;
  163. int entry_num;
  164. struct item_head *ih, tmp_ih;
  165. int search_res;
  166. char *local_buf;
  167. loff_t next_pos;
  168. char small_buf[32]; /* avoid kmalloc if we can */
  169. struct reiserfs_de_head *deh;
  170. int d_reclen;
  171. char *d_name;
  172. off_t d_off;
  173. ino_t d_ino;
  174. struct reiserfs_dir_entry de;
  175. /* form key for search the next directory entry using f_pos field of
  176. file structure */
  177. next_pos = max_reiserfs_offset(inode);
  178. while (1) {
  179. research:
  180. if (next_pos <= DOT_DOT_OFFSET)
  181. break;
  182. make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
  183. search_res =
  184. search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
  185. &de);
  186. if (search_res == IO_ERROR) {
  187. // FIXME: we could just skip part of directory which could
  188. // not be read
  189. pathrelse(&path_to_entry);
  190. return -EIO;
  191. }
  192. if (search_res == NAME_NOT_FOUND)
  193. de.de_entry_num--;
  194. set_de_name_and_namelen(&de);
  195. entry_num = de.de_entry_num;
  196. deh = &(de.de_deh[entry_num]);
  197. bh = de.de_bh;
  198. ih = de.de_ih;
  199. if (!is_direntry_le_ih(ih)) {
  200. reiserfs_error(inode->i_sb, "jdm-20000",
  201. "not direntry %h", ih);
  202. break;
  203. }
  204. copy_item_head(&tmp_ih, ih);
  205. /* we must have found item, that is item of this directory, */
  206. RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
  207. "vs-9000: found item %h does not match to dir we readdir %K",
  208. ih, &pos_key);
  209. if (deh_offset(deh) <= DOT_DOT_OFFSET) {
  210. break;
  211. }
  212. /* look for the previous entry in the directory */
  213. next_pos = deh_offset(deh) - 1;
  214. if (!de_visible(deh))
  215. /* it is hidden entry */
  216. continue;
  217. d_reclen = entry_length(bh, ih, entry_num);
  218. d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
  219. d_off = deh_offset(deh);
  220. d_ino = deh_objectid(deh);
  221. if (!d_name[d_reclen - 1])
  222. d_reclen = strlen(d_name);
  223. if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
  224. /* too big to send back to VFS */
  225. continue;
  226. }
  227. /* Ignore the .reiserfs_priv entry */
  228. if (reiserfs_xattrs(inode->i_sb) &&
  229. !old_format_only(inode->i_sb) &&
  230. deh_objectid(deh) ==
  231. le32_to_cpu(INODE_PKEY
  232. (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
  233. k_objectid))
  234. continue;
  235. if (d_reclen <= 32) {
  236. local_buf = small_buf;
  237. } else {
  238. local_buf = kmalloc(d_reclen, GFP_NOFS);
  239. if (!local_buf) {
  240. pathrelse(&path_to_entry);
  241. return -ENOMEM;
  242. }
  243. if (item_moved(&tmp_ih, &path_to_entry)) {
  244. kfree(local_buf);
  245. /* sigh, must retry. Do this same offset again */
  246. next_pos = d_off;
  247. goto research;
  248. }
  249. }
  250. // Note, that we copy name to user space via temporary
  251. // buffer (local_buf) because filldir will block if
  252. // user space buffer is swapped out. At that time
  253. // entry can move to somewhere else
  254. memcpy(local_buf, d_name, d_reclen);
  255. /* the filldir function might need to start transactions,
  256. * or do who knows what. Release the path now that we've
  257. * copied all the important stuff out of the deh
  258. */
  259. pathrelse(&path_to_entry);
  260. if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
  261. DT_UNKNOWN) < 0) {
  262. if (local_buf != small_buf) {
  263. kfree(local_buf);
  264. }
  265. goto end;
  266. }
  267. if (local_buf != small_buf) {
  268. kfree(local_buf);
  269. }
  270. } /* while */
  271. end:
  272. pathrelse(&path_to_entry);
  273. return 0;
  274. }
  275. /*
  276. * this could be done with dedicated readdir ops for the xattr files,
  277. * but I want to get something working asap
  278. * this is stolen from vfs_readdir
  279. *
  280. */
  281. static
  282. int xattr_readdir(struct inode *inode, filldir_t filler, void *buf)
  283. {
  284. int res = -ENOENT;
  285. if (!IS_DEADDIR(inode)) {
  286. lock_kernel();
  287. res = __xattr_readdir(inode, buf, filler);
  288. unlock_kernel();
  289. }
  290. return res;
  291. }
  292. /* The following are side effects of other operations that aren't explicitly
  293. * modifying extended attributes. This includes operations such as permissions
  294. * or ownership changes, object deletions, etc. */
  295. static int
  296. reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
  297. loff_t offset, u64 ino, unsigned int d_type)
  298. {
  299. struct dentry *xadir = (struct dentry *)buf;
  300. struct dentry *dentry;
  301. int err = 0;
  302. dentry = lookup_one_len(name, xadir, namelen);
  303. if (IS_ERR(dentry)) {
  304. err = PTR_ERR(dentry);
  305. goto out;
  306. } else if (!dentry->d_inode) {
  307. err = -ENODATA;
  308. goto out_file;
  309. }
  310. /* Skip directories.. */
  311. if (S_ISDIR(dentry->d_inode->i_mode))
  312. goto out_file;
  313. err = xattr_unlink(xadir->d_inode, dentry);
  314. out_file:
  315. dput(dentry);
  316. out:
  317. return err;
  318. }
  319. /* This is called w/ inode->i_mutex downed */
  320. int reiserfs_delete_xattrs(struct inode *inode)
  321. {
  322. int err = -ENODATA;
  323. struct dentry *dir, *root;
  324. struct reiserfs_transaction_handle th;
  325. int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
  326. 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
  327. /* Skip out, an xattr has no xattrs associated with it */
  328. if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
  329. return 0;
  330. dir = open_xa_dir(inode, XATTR_REPLACE);
  331. if (IS_ERR(dir)) {
  332. err = PTR_ERR(dir);
  333. goto out;
  334. } else if (!dir->d_inode) {
  335. dput(dir);
  336. goto out;
  337. }
  338. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
  339. err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir);
  340. mutex_unlock(&dir->d_inode->i_mutex);
  341. if (err) {
  342. dput(dir);
  343. goto out;
  344. }
  345. root = dget(dir->d_parent);
  346. dput(dir);
  347. /* We start a transaction here to avoid a ABBA situation
  348. * between the xattr root's i_mutex and the journal lock.
  349. * Inode creation will inherit an ACL, which requires a
  350. * lookup. The lookup locks the xattr root i_mutex with a
  351. * transaction open. Inode deletion takes teh xattr root
  352. * i_mutex to delete the directory and then starts a
  353. * transaction inside it. Boom. This doesn't incur much
  354. * additional overhead since the reiserfs_rmdir transaction
  355. * will just nest inside the outer transaction. */
  356. err = journal_begin(&th, inode->i_sb, blocks);
  357. if (!err) {
  358. int jerror;
  359. mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_XATTR);
  360. err = xattr_rmdir(root->d_inode, dir);
  361. jerror = journal_end(&th, inode->i_sb, blocks);
  362. mutex_unlock(&root->d_inode->i_mutex);
  363. err = jerror ?: err;
  364. }
  365. dput(root);
  366. out:
  367. if (err)
  368. reiserfs_warning(inode->i_sb, "jdm-20004",
  369. "Couldn't remove all xattrs (%d)\n", err);
  370. return err;
  371. }
  372. struct reiserfs_chown_buf {
  373. struct inode *inode;
  374. struct dentry *xadir;
  375. struct iattr *attrs;
  376. };
  377. /* XXX: If there is a better way to do this, I'd love to hear about it */
  378. static int
  379. reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
  380. loff_t offset, u64 ino, unsigned int d_type)
  381. {
  382. struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
  383. struct dentry *xafile, *xadir = chown_buf->xadir;
  384. struct iattr *attrs = chown_buf->attrs;
  385. int err = 0;
  386. xafile = lookup_one_len(name, xadir, namelen);
  387. if (IS_ERR(xafile))
  388. return PTR_ERR(xafile);
  389. else if (!xafile->d_inode) {
  390. dput(xafile);
  391. return -ENODATA;
  392. }
  393. if (!S_ISDIR(xafile->d_inode->i_mode)) {
  394. mutex_lock_nested(&xafile->d_inode->i_mutex, I_MUTEX_CHILD);
  395. err = reiserfs_setattr(xafile, attrs);
  396. mutex_unlock(&xafile->d_inode->i_mutex);
  397. }
  398. dput(xafile);
  399. return err;
  400. }
  401. int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  402. {
  403. struct dentry *dir;
  404. int err = 0;
  405. struct reiserfs_chown_buf buf;
  406. unsigned int ia_valid = attrs->ia_valid;
  407. /* Skip out, an xattr has no xattrs associated with it */
  408. if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
  409. return 0;
  410. dir = open_xa_dir(inode, XATTR_REPLACE);
  411. if (IS_ERR(dir)) {
  412. if (PTR_ERR(dir) != -ENODATA)
  413. err = PTR_ERR(dir);
  414. goto out;
  415. } else if (!dir->d_inode)
  416. goto out_dir;
  417. attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
  418. buf.xadir = dir;
  419. buf.attrs = attrs;
  420. buf.inode = inode;
  421. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
  422. err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf);
  423. if (!err)
  424. err = reiserfs_setattr(dir, attrs);
  425. mutex_unlock(&dir->d_inode->i_mutex);
  426. attrs->ia_valid = ia_valid;
  427. out_dir:
  428. dput(dir);
  429. out:
  430. if (err)
  431. reiserfs_warning(inode->i_sb, "jdm-20007",
  432. "Couldn't chown all xattrs (%d)\n", err);
  433. return err;
  434. }
  435. #ifdef CONFIG_REISERFS_FS_XATTR
  436. /* Returns a dentry corresponding to a specific extended attribute file
  437. * for the inode. If flags allow, the file is created. Otherwise, a
  438. * valid or negative dentry, or an error is returned. */
  439. static struct dentry *xattr_lookup(struct inode *inode, const char *name,
  440. int flags)
  441. {
  442. struct dentry *xadir, *xafile;
  443. int err = 0;
  444. xadir = open_xa_dir(inode, flags);
  445. if (IS_ERR(xadir))
  446. return ERR_CAST(xadir);
  447. xafile = lookup_one_len(name, xadir, strlen(name));
  448. if (IS_ERR(xafile)) {
  449. err = PTR_ERR(xafile);
  450. goto out;
  451. }
  452. if (xafile->d_inode && (flags & XATTR_CREATE))
  453. err = -EEXIST;
  454. if (!xafile->d_inode) {
  455. err = -ENODATA;
  456. if (xattr_may_create(flags)) {
  457. mutex_lock_nested(&xadir->d_inode->i_mutex,
  458. I_MUTEX_XATTR);
  459. err = xattr_create(xadir->d_inode, xafile,
  460. 0700|S_IFREG);
  461. mutex_unlock(&xadir->d_inode->i_mutex);
  462. }
  463. }
  464. if (err)
  465. dput(xafile);
  466. out:
  467. dput(xadir);
  468. if (err)
  469. return ERR_PTR(err);
  470. return xafile;
  471. }
  472. /* Internal operations on file data */
  473. static inline void reiserfs_put_page(struct page *page)
  474. {
  475. kunmap(page);
  476. page_cache_release(page);
  477. }
  478. static struct page *reiserfs_get_page(struct inode *dir, size_t n)
  479. {
  480. struct address_space *mapping = dir->i_mapping;
  481. struct page *page;
  482. /* We can deadlock if we try to free dentries,
  483. and an unlink/rmdir has just occured - GFP_NOFS avoids this */
  484. mapping_set_gfp_mask(mapping, GFP_NOFS);
  485. page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL);
  486. if (!IS_ERR(page)) {
  487. kmap(page);
  488. if (PageError(page))
  489. goto fail;
  490. }
  491. return page;
  492. fail:
  493. reiserfs_put_page(page);
  494. return ERR_PTR(-EIO);
  495. }
  496. static inline __u32 xattr_hash(const char *msg, int len)
  497. {
  498. return csum_partial(msg, len, 0);
  499. }
  500. int reiserfs_commit_write(struct file *f, struct page *page,
  501. unsigned from, unsigned to);
  502. int reiserfs_prepare_write(struct file *f, struct page *page,
  503. unsigned from, unsigned to);
  504. static void update_ctime(struct inode *inode)
  505. {
  506. struct timespec now = current_fs_time(inode->i_sb);
  507. if (hlist_unhashed(&inode->i_hash) || !inode->i_nlink ||
  508. timespec_equal(&inode->i_ctime, &now))
  509. return;
  510. inode->i_ctime = CURRENT_TIME_SEC;
  511. mark_inode_dirty(inode);
  512. }
  513. static int lookup_and_delete_xattr(struct inode *inode, const char *name)
  514. {
  515. int err = 0;
  516. struct dentry *dentry, *xadir;
  517. xadir = open_xa_dir(inode, XATTR_REPLACE);
  518. if (IS_ERR(xadir))
  519. return PTR_ERR(xadir);
  520. dentry = lookup_one_len(name, xadir, strlen(name));
  521. if (IS_ERR(dentry)) {
  522. err = PTR_ERR(dentry);
  523. goto out_dput;
  524. }
  525. if (dentry->d_inode) {
  526. mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR);
  527. err = xattr_unlink(xadir->d_inode, dentry);
  528. mutex_unlock(&xadir->d_inode->i_mutex);
  529. update_ctime(inode);
  530. }
  531. dput(dentry);
  532. out_dput:
  533. dput(xadir);
  534. return err;
  535. }
  536. /* Generic extended attribute operations that can be used by xa plugins */
  537. /*
  538. * inode->i_mutex: down
  539. */
  540. int
  541. reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
  542. struct inode *inode, const char *name,
  543. const void *buffer, size_t buffer_size, int flags)
  544. {
  545. int err = 0;
  546. struct dentry *dentry;
  547. struct page *page;
  548. char *data;
  549. size_t file_pos = 0;
  550. size_t buffer_pos = 0;
  551. size_t new_size;
  552. __u32 xahash = 0;
  553. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  554. return -EOPNOTSUPP;
  555. if (!buffer)
  556. return lookup_and_delete_xattr(inode, name);
  557. dentry = xattr_lookup(inode, name, flags);
  558. if (IS_ERR(dentry))
  559. return PTR_ERR(dentry);
  560. down_write(&REISERFS_I(inode)->i_xattr_sem);
  561. xahash = xattr_hash(buffer, buffer_size);
  562. while (buffer_pos < buffer_size || buffer_pos == 0) {
  563. size_t chunk;
  564. size_t skip = 0;
  565. size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
  566. if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
  567. chunk = PAGE_CACHE_SIZE;
  568. else
  569. chunk = buffer_size - buffer_pos;
  570. page = reiserfs_get_page(dentry->d_inode, file_pos);
  571. if (IS_ERR(page)) {
  572. err = PTR_ERR(page);
  573. goto out_unlock;
  574. }
  575. lock_page(page);
  576. data = page_address(page);
  577. if (file_pos == 0) {
  578. struct reiserfs_xattr_header *rxh;
  579. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  580. if (chunk + skip > PAGE_CACHE_SIZE)
  581. chunk = PAGE_CACHE_SIZE - skip;
  582. rxh = (struct reiserfs_xattr_header *)data;
  583. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  584. rxh->h_hash = cpu_to_le32(xahash);
  585. }
  586. err = reiserfs_prepare_write(NULL, page, page_offset,
  587. page_offset + chunk + skip);
  588. if (!err) {
  589. if (buffer)
  590. memcpy(data + skip, buffer + buffer_pos, chunk);
  591. err = reiserfs_commit_write(NULL, page, page_offset,
  592. page_offset + chunk +
  593. skip);
  594. }
  595. unlock_page(page);
  596. reiserfs_put_page(page);
  597. buffer_pos += chunk;
  598. file_pos += chunk;
  599. skip = 0;
  600. if (err || buffer_size == 0 || !buffer)
  601. break;
  602. }
  603. new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
  604. if (!err && new_size < i_size_read(dentry->d_inode)) {
  605. struct iattr newattrs = {
  606. .ia_ctime = current_fs_time(inode->i_sb),
  607. .ia_size = buffer_size,
  608. .ia_valid = ATTR_SIZE | ATTR_CTIME,
  609. };
  610. mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
  611. down_write(&dentry->d_inode->i_alloc_sem);
  612. err = reiserfs_setattr(dentry, &newattrs);
  613. up_write(&dentry->d_inode->i_alloc_sem);
  614. mutex_unlock(&dentry->d_inode->i_mutex);
  615. } else
  616. update_ctime(inode);
  617. out_unlock:
  618. up_write(&REISERFS_I(inode)->i_xattr_sem);
  619. dput(dentry);
  620. return err;
  621. }
  622. /* We need to start a transaction to maintain lock ordering */
  623. int reiserfs_xattr_set(struct inode *inode, const char *name,
  624. const void *buffer, size_t buffer_size, int flags)
  625. {
  626. struct reiserfs_transaction_handle th;
  627. int error, error2;
  628. size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
  629. if (!(flags & XATTR_REPLACE))
  630. jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
  631. reiserfs_write_lock(inode->i_sb);
  632. error = journal_begin(&th, inode->i_sb, jbegin_count);
  633. if (error) {
  634. reiserfs_write_unlock(inode->i_sb);
  635. return error;
  636. }
  637. error = reiserfs_xattr_set_handle(&th, inode, name,
  638. buffer, buffer_size, flags);
  639. error2 = journal_end(&th, inode->i_sb, jbegin_count);
  640. if (error == 0)
  641. error = error2;
  642. reiserfs_write_unlock(inode->i_sb);
  643. return error;
  644. }
  645. /*
  646. * inode->i_mutex: down
  647. */
  648. int
  649. reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
  650. size_t buffer_size)
  651. {
  652. ssize_t err = 0;
  653. struct dentry *dentry;
  654. size_t isize;
  655. size_t file_pos = 0;
  656. size_t buffer_pos = 0;
  657. struct page *page;
  658. __u32 hash = 0;
  659. if (name == NULL)
  660. return -EINVAL;
  661. /* We can't have xattrs attached to v1 items since they don't have
  662. * generation numbers */
  663. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  664. return -EOPNOTSUPP;
  665. dentry = xattr_lookup(inode, name, XATTR_REPLACE);
  666. if (IS_ERR(dentry)) {
  667. err = PTR_ERR(dentry);
  668. goto out;
  669. }
  670. down_read(&REISERFS_I(inode)->i_xattr_sem);
  671. isize = i_size_read(dentry->d_inode);
  672. /* Just return the size needed */
  673. if (buffer == NULL) {
  674. err = isize - sizeof(struct reiserfs_xattr_header);
  675. goto out_unlock;
  676. }
  677. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  678. err = -ERANGE;
  679. goto out_unlock;
  680. }
  681. while (file_pos < isize) {
  682. size_t chunk;
  683. char *data;
  684. size_t skip = 0;
  685. if (isize - file_pos > PAGE_CACHE_SIZE)
  686. chunk = PAGE_CACHE_SIZE;
  687. else
  688. chunk = isize - file_pos;
  689. page = reiserfs_get_page(dentry->d_inode, file_pos);
  690. if (IS_ERR(page)) {
  691. err = PTR_ERR(page);
  692. goto out_unlock;
  693. }
  694. lock_page(page);
  695. data = page_address(page);
  696. if (file_pos == 0) {
  697. struct reiserfs_xattr_header *rxh =
  698. (struct reiserfs_xattr_header *)data;
  699. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  700. chunk -= skip;
  701. /* Magic doesn't match up.. */
  702. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  703. unlock_page(page);
  704. reiserfs_put_page(page);
  705. reiserfs_warning(inode->i_sb, "jdm-20001",
  706. "Invalid magic for xattr (%s) "
  707. "associated with %k", name,
  708. INODE_PKEY(inode));
  709. err = -EIO;
  710. goto out_unlock;
  711. }
  712. hash = le32_to_cpu(rxh->h_hash);
  713. }
  714. memcpy(buffer + buffer_pos, data + skip, chunk);
  715. unlock_page(page);
  716. reiserfs_put_page(page);
  717. file_pos += chunk;
  718. buffer_pos += chunk;
  719. skip = 0;
  720. }
  721. err = isize - sizeof(struct reiserfs_xattr_header);
  722. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  723. hash) {
  724. reiserfs_warning(inode->i_sb, "jdm-20002",
  725. "Invalid hash for xattr (%s) associated "
  726. "with %k", name, INODE_PKEY(inode));
  727. err = -EIO;
  728. }
  729. out_unlock:
  730. up_read(&REISERFS_I(inode)->i_xattr_sem);
  731. dput(dentry);
  732. out:
  733. return err;
  734. }
  735. /* Actual operations that are exported to VFS-land */
  736. struct xattr_handler *reiserfs_xattr_handlers[] = {
  737. &reiserfs_xattr_user_handler,
  738. &reiserfs_xattr_trusted_handler,
  739. #ifdef CONFIG_REISERFS_FS_SECURITY
  740. &reiserfs_xattr_security_handler,
  741. #endif
  742. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  743. &reiserfs_posix_acl_access_handler,
  744. &reiserfs_posix_acl_default_handler,
  745. #endif
  746. NULL
  747. };
  748. /*
  749. * In order to implement different sets of xattr operations for each xattr
  750. * prefix with the generic xattr API, a filesystem should create a
  751. * null-terminated array of struct xattr_handler (one for each prefix) and
  752. * hang a pointer to it off of the s_xattr field of the superblock.
  753. *
  754. * The generic_fooxattr() functions will use this list to dispatch xattr
  755. * operations to the correct xattr_handler.
  756. */
  757. #define for_each_xattr_handler(handlers, handler) \
  758. for ((handler) = *(handlers)++; \
  759. (handler) != NULL; \
  760. (handler) = *(handlers)++)
  761. /* This is the implementation for the xattr plugin infrastructure */
  762. static inline struct xattr_handler *
  763. find_xattr_handler_prefix(struct xattr_handler **handlers,
  764. const char *name)
  765. {
  766. struct xattr_handler *xah;
  767. if (!handlers)
  768. return NULL;
  769. for_each_xattr_handler(handlers, xah) {
  770. if (strncmp(xah->prefix, name, strlen(xah->prefix)) == 0)
  771. break;
  772. }
  773. return xah;
  774. }
  775. /*
  776. * Inode operation getxattr()
  777. */
  778. ssize_t
  779. reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
  780. size_t size)
  781. {
  782. struct inode *inode = dentry->d_inode;
  783. struct xattr_handler *handler;
  784. handler = find_xattr_handler_prefix(inode->i_sb->s_xattr, name);
  785. if (!handler || get_inode_sd_version(inode) == STAT_DATA_V1)
  786. return -EOPNOTSUPP;
  787. return handler->get(inode, name, buffer, size);
  788. }
  789. /*
  790. * Inode operation setxattr()
  791. *
  792. * dentry->d_inode->i_mutex down
  793. */
  794. int
  795. reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  796. size_t size, int flags)
  797. {
  798. struct inode *inode = dentry->d_inode;
  799. struct xattr_handler *handler;
  800. handler = find_xattr_handler_prefix(inode->i_sb->s_xattr, name);
  801. if (!handler || get_inode_sd_version(inode) == STAT_DATA_V1)
  802. return -EOPNOTSUPP;
  803. return handler->set(inode, name, value, size, flags);
  804. }
  805. /*
  806. * Inode operation removexattr()
  807. *
  808. * dentry->d_inode->i_mutex down
  809. */
  810. int reiserfs_removexattr(struct dentry *dentry, const char *name)
  811. {
  812. struct inode *inode = dentry->d_inode;
  813. struct xattr_handler *handler;
  814. handler = find_xattr_handler_prefix(inode->i_sb->s_xattr, name);
  815. if (!handler || get_inode_sd_version(inode) == STAT_DATA_V1)
  816. return -EOPNOTSUPP;
  817. return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
  818. }
  819. struct listxattr_buf {
  820. size_t size;
  821. size_t pos;
  822. char *buf;
  823. struct inode *inode;
  824. };
  825. static int listxattr_filler(void *buf, const char *name, int namelen,
  826. loff_t offset, u64 ino, unsigned int d_type)
  827. {
  828. struct listxattr_buf *b = (struct listxattr_buf *)buf;
  829. size_t size;
  830. if (name[0] != '.' ||
  831. (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  832. struct xattr_handler *handler;
  833. handler = find_xattr_handler_prefix(b->inode->i_sb->s_xattr,
  834. name);
  835. if (!handler) /* Unsupported xattr name */
  836. return 0;
  837. if (b->buf) {
  838. size = handler->list(b->inode, b->buf + b->pos,
  839. b->size, name, namelen);
  840. if (size > b->size)
  841. return -ERANGE;
  842. } else {
  843. size = handler->list(b->inode, NULL, 0, name, namelen);
  844. }
  845. b->pos += size;
  846. }
  847. return 0;
  848. }
  849. /*
  850. * Inode operation listxattr()
  851. *
  852. * We totally ignore the generic listxattr here because it would be stupid
  853. * not to. Since the xattrs are organized in a directory, we can just
  854. * readdir to find them.
  855. */
  856. ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
  857. {
  858. struct dentry *dir;
  859. int err = 0;
  860. struct listxattr_buf buf = {
  861. .inode = dentry->d_inode,
  862. .buf = buffer,
  863. .size = buffer ? size : 0,
  864. };
  865. if (!dentry->d_inode)
  866. return -EINVAL;
  867. if (!reiserfs_xattrs(dentry->d_sb) ||
  868. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  869. return -EOPNOTSUPP;
  870. dir = open_xa_dir(dentry->d_inode, XATTR_REPLACE);
  871. if (IS_ERR(dir)) {
  872. err = PTR_ERR(dir);
  873. if (err == -ENODATA)
  874. err = 0; /* Not an error if there aren't any xattrs */
  875. goto out;
  876. }
  877. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
  878. err = xattr_readdir(dir->d_inode, listxattr_filler, &buf);
  879. mutex_unlock(&dir->d_inode->i_mutex);
  880. if (!err)
  881. err = buf.pos;
  882. dput(dir);
  883. out:
  884. return err;
  885. }
  886. static int reiserfs_check_acl(struct inode *inode, int mask)
  887. {
  888. struct posix_acl *acl;
  889. int error = -EAGAIN; /* do regular unix permission checks by default */
  890. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  891. if (acl) {
  892. if (!IS_ERR(acl)) {
  893. error = posix_acl_permission(inode, acl, mask);
  894. posix_acl_release(acl);
  895. } else if (PTR_ERR(acl) != -ENODATA)
  896. error = PTR_ERR(acl);
  897. }
  898. return error;
  899. }
  900. int reiserfs_permission(struct inode *inode, int mask)
  901. {
  902. /*
  903. * We don't do permission checks on the internal objects.
  904. * Permissions are determined by the "owning" object.
  905. */
  906. if (IS_PRIVATE(inode))
  907. return 0;
  908. /*
  909. * Stat data v1 doesn't support ACLs.
  910. */
  911. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  912. return generic_permission(inode, mask, NULL);
  913. else
  914. return generic_permission(inode, mask, reiserfs_check_acl);
  915. }
  916. static int create_privroot(struct dentry *dentry)
  917. {
  918. int err;
  919. struct inode *inode = dentry->d_parent->d_inode;
  920. mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
  921. err = xattr_mkdir(inode, dentry, 0700);
  922. mutex_unlock(&inode->i_mutex);
  923. if (err) {
  924. dput(dentry);
  925. dentry = NULL;
  926. }
  927. if (dentry && dentry->d_inode)
  928. reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
  929. "storage.\n", PRIVROOT_NAME);
  930. return err;
  931. }
  932. static int xattr_mount_check(struct super_block *s)
  933. {
  934. /* We need generation numbers to ensure that the oid mapping is correct
  935. * v3.5 filesystems don't have them. */
  936. if (old_format_only(s)) {
  937. if (reiserfs_xattrs_optional(s)) {
  938. /* Old format filesystem, but optional xattrs have
  939. * been enabled. Error out. */
  940. reiserfs_warning(s, "jdm-2005",
  941. "xattrs/ACLs not supported "
  942. "on pre-v3.6 format filesystems. "
  943. "Failing mount.");
  944. return -EOPNOTSUPP;
  945. }
  946. }
  947. return 0;
  948. }
  949. #else
  950. int __init reiserfs_xattr_register_handlers(void) { return 0; }
  951. void reiserfs_xattr_unregister_handlers(void) {}
  952. #endif
  953. /* This will catch lookups from the fs root to .reiserfs_priv */
  954. static int
  955. xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
  956. {
  957. struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
  958. if (name->len == priv_root->d_name.len &&
  959. name->hash == priv_root->d_name.hash &&
  960. !memcmp(name->name, priv_root->d_name.name, name->len)) {
  961. return -ENOENT;
  962. } else if (q1->len == name->len &&
  963. !memcmp(q1->name, name->name, name->len))
  964. return 0;
  965. return 1;
  966. }
  967. static struct dentry_operations xattr_lookup_poison_ops = {
  968. .d_compare = xattr_lookup_poison,
  969. };
  970. /* We need to take a copy of the mount flags since things like
  971. * MS_RDONLY don't get set until *after* we're called.
  972. * mount_flags != mount_options */
  973. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  974. {
  975. int err = 0;
  976. #ifdef CONFIG_REISERFS_FS_XATTR
  977. err = xattr_mount_check(s);
  978. if (err)
  979. goto error;
  980. #endif
  981. /* If we don't have the privroot located yet - go find it */
  982. if (!REISERFS_SB(s)->priv_root) {
  983. struct dentry *dentry;
  984. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  985. strlen(PRIVROOT_NAME));
  986. if (!IS_ERR(dentry)) {
  987. #ifdef CONFIG_REISERFS_FS_XATTR
  988. if (!(mount_flags & MS_RDONLY) && !dentry->d_inode)
  989. err = create_privroot(dentry);
  990. #endif
  991. if (!dentry->d_inode) {
  992. dput(dentry);
  993. dentry = NULL;
  994. }
  995. } else
  996. err = PTR_ERR(dentry);
  997. if (!err && dentry) {
  998. s->s_root->d_op = &xattr_lookup_poison_ops;
  999. dentry->d_inode->i_flags |= S_PRIVATE;
  1000. REISERFS_SB(s)->priv_root = dentry;
  1001. #ifdef CONFIG_REISERFS_FS_XATTR
  1002. /* xattrs are unavailable */
  1003. } else if (!(mount_flags & MS_RDONLY)) {
  1004. /* If we're read-only it just means that the dir
  1005. * hasn't been created. Not an error -- just no
  1006. * xattrs on the fs. We'll check again if we
  1007. * go read-write */
  1008. reiserfs_warning(s, "jdm-20006",
  1009. "xattrs/ACLs enabled and couldn't "
  1010. "find/create .reiserfs_priv. "
  1011. "Failing mount.");
  1012. err = -EOPNOTSUPP;
  1013. #endif
  1014. }
  1015. }
  1016. #ifdef CONFIG_REISERFS_FS_XATTR
  1017. if (!err)
  1018. s->s_xattr = reiserfs_xattr_handlers;
  1019. error:
  1020. if (err) {
  1021. clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
  1022. clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
  1023. }
  1024. #endif
  1025. /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
  1026. s->s_flags = s->s_flags & ~MS_POSIXACL;
  1027. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  1028. if (reiserfs_posixacl(s))
  1029. s->s_flags |= MS_POSIXACL;
  1030. #endif
  1031. return err;
  1032. }