xattr.c 32 KB

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