xattr.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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_warning(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, unsigned long 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, 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. struct address_space *mapping;
  373. size_t file_pos = 0;
  374. size_t buffer_pos = 0;
  375. struct inode *xinode;
  376. struct iattr newattrs;
  377. __u32 xahash = 0;
  378. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  379. return -EOPNOTSUPP;
  380. /* Empty xattrs are ok, they're just empty files, no hash */
  381. if (buffer && buffer_size)
  382. xahash = xattr_hash(buffer, buffer_size);
  383. open_file:
  384. dentry = get_xa_file_dentry(inode, name, flags);
  385. if (IS_ERR(dentry)) {
  386. err = PTR_ERR(dentry);
  387. goto out;
  388. }
  389. xinode = dentry->d_inode;
  390. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  391. /* we need to copy it off.. */
  392. if (xinode->i_nlink > 1) {
  393. dput(dentry);
  394. err = reiserfs_xattr_del(inode, name);
  395. if (err < 0)
  396. goto out;
  397. /* We just killed the old one, we're not replacing anymore */
  398. if (flags & XATTR_REPLACE)
  399. flags &= ~XATTR_REPLACE;
  400. goto open_file;
  401. }
  402. /* Resize it so we're ok to write there */
  403. newattrs.ia_size = buffer_size;
  404. newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
  405. mutex_lock_nested(&xinode->i_mutex, I_MUTEX_XATTR);
  406. err = notify_change(dentry, &newattrs);
  407. if (err)
  408. goto out_filp;
  409. mapping = xinode->i_mapping;
  410. while (buffer_pos < buffer_size || buffer_pos == 0) {
  411. size_t chunk;
  412. size_t skip = 0;
  413. size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
  414. if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
  415. chunk = PAGE_CACHE_SIZE;
  416. else
  417. chunk = buffer_size - buffer_pos;
  418. page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
  419. if (IS_ERR(page)) {
  420. err = PTR_ERR(page);
  421. goto out_filp;
  422. }
  423. lock_page(page);
  424. data = page_address(page);
  425. if (file_pos == 0) {
  426. struct reiserfs_xattr_header *rxh;
  427. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  428. if (chunk + skip > PAGE_CACHE_SIZE)
  429. chunk = PAGE_CACHE_SIZE - skip;
  430. rxh = (struct reiserfs_xattr_header *)data;
  431. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  432. rxh->h_hash = cpu_to_le32(xahash);
  433. }
  434. err = reiserfs_prepare_write(NULL, page, page_offset,
  435. page_offset + chunk + skip);
  436. if (!err) {
  437. if (buffer)
  438. memcpy(data + skip, buffer + buffer_pos, chunk);
  439. err = reiserfs_commit_write(NULL, page, page_offset,
  440. page_offset + chunk +
  441. skip);
  442. }
  443. unlock_page(page);
  444. reiserfs_put_page(page);
  445. buffer_pos += chunk;
  446. file_pos += chunk;
  447. skip = 0;
  448. if (err || buffer_size == 0 || !buffer)
  449. break;
  450. }
  451. /* We can't mark the inode dirty if it's not hashed. This is the case
  452. * when we're inheriting the default ACL. If we dirty it, the inode
  453. * gets marked dirty, but won't (ever) make it onto the dirty list until
  454. * it's synced explicitly to clear I_DIRTY. This is bad. */
  455. if (!hlist_unhashed(&inode->i_hash)) {
  456. inode->i_ctime = CURRENT_TIME_SEC;
  457. mark_inode_dirty(inode);
  458. }
  459. out_filp:
  460. mutex_unlock(&xinode->i_mutex);
  461. dput(dentry);
  462. out:
  463. return err;
  464. }
  465. /*
  466. * inode->i_mutex: down
  467. */
  468. int
  469. reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
  470. size_t buffer_size)
  471. {
  472. ssize_t err = 0;
  473. struct dentry *dentry;
  474. size_t isize;
  475. size_t file_pos = 0;
  476. size_t buffer_pos = 0;
  477. struct page *page;
  478. struct inode *xinode;
  479. __u32 hash = 0;
  480. if (name == NULL)
  481. return -EINVAL;
  482. /* We can't have xattrs attached to v1 items since they don't have
  483. * generation numbers */
  484. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  485. return -EOPNOTSUPP;
  486. dentry = get_xa_file_dentry(inode, name, FL_READONLY);
  487. if (IS_ERR(dentry)) {
  488. err = PTR_ERR(dentry);
  489. goto out;
  490. }
  491. xinode = dentry->d_inode;
  492. isize = xinode->i_size;
  493. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  494. /* Just return the size needed */
  495. if (buffer == NULL) {
  496. err = isize - sizeof(struct reiserfs_xattr_header);
  497. goto out_dput;
  498. }
  499. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  500. err = -ERANGE;
  501. goto out_dput;
  502. }
  503. while (file_pos < isize) {
  504. size_t chunk;
  505. char *data;
  506. size_t skip = 0;
  507. if (isize - file_pos > PAGE_CACHE_SIZE)
  508. chunk = PAGE_CACHE_SIZE;
  509. else
  510. chunk = isize - file_pos;
  511. page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
  512. if (IS_ERR(page)) {
  513. err = PTR_ERR(page);
  514. goto out_dput;
  515. }
  516. lock_page(page);
  517. data = page_address(page);
  518. if (file_pos == 0) {
  519. struct reiserfs_xattr_header *rxh =
  520. (struct reiserfs_xattr_header *)data;
  521. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  522. chunk -= skip;
  523. /* Magic doesn't match up.. */
  524. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  525. unlock_page(page);
  526. reiserfs_put_page(page);
  527. reiserfs_warning(inode->i_sb, "jdm-20001",
  528. "Invalid magic for xattr (%s) "
  529. "associated with %k", name,
  530. INODE_PKEY(inode));
  531. err = -EIO;
  532. goto out_dput;
  533. }
  534. hash = le32_to_cpu(rxh->h_hash);
  535. }
  536. memcpy(buffer + buffer_pos, data + skip, chunk);
  537. unlock_page(page);
  538. reiserfs_put_page(page);
  539. file_pos += chunk;
  540. buffer_pos += chunk;
  541. skip = 0;
  542. }
  543. err = isize - sizeof(struct reiserfs_xattr_header);
  544. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  545. hash) {
  546. reiserfs_warning(inode->i_sb, "jdm-20002",
  547. "Invalid hash for xattr (%s) associated "
  548. "with %k", name, INODE_PKEY(inode));
  549. err = -EIO;
  550. }
  551. out_dput:
  552. dput(dentry);
  553. out:
  554. return err;
  555. }
  556. static int
  557. __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
  558. {
  559. struct dentry *dentry;
  560. struct inode *dir = xadir->d_inode;
  561. int err = 0;
  562. dentry = lookup_one_len(name, xadir, namelen);
  563. if (IS_ERR(dentry)) {
  564. err = PTR_ERR(dentry);
  565. goto out;
  566. } else if (!dentry->d_inode) {
  567. err = -ENODATA;
  568. goto out_file;
  569. }
  570. /* Skip directories.. */
  571. if (S_ISDIR(dentry->d_inode->i_mode))
  572. goto out_file;
  573. if (!is_reiserfs_priv_object(dentry->d_inode)) {
  574. reiserfs_warning(dir->i_sb, "jdm-20003",
  575. "OID %08x [%.*s/%.*s] doesn't have "
  576. "priv flag set [parent is %sset].",
  577. le32_to_cpu(INODE_PKEY(dentry->d_inode)->
  578. k_objectid), xadir->d_name.len,
  579. xadir->d_name.name, namelen, name,
  580. is_reiserfs_priv_object(xadir->
  581. d_inode) ? "" :
  582. "not ");
  583. dput(dentry);
  584. return -EIO;
  585. }
  586. err = dir->i_op->unlink(dir, dentry);
  587. if (!err)
  588. d_delete(dentry);
  589. out_file:
  590. dput(dentry);
  591. out:
  592. return err;
  593. }
  594. int reiserfs_xattr_del(struct inode *inode, const char *name)
  595. {
  596. struct dentry *dir;
  597. int err;
  598. dir = open_xa_dir(inode, FL_READONLY);
  599. if (IS_ERR(dir)) {
  600. err = PTR_ERR(dir);
  601. goto out;
  602. }
  603. err = __reiserfs_xattr_del(dir, name, strlen(name));
  604. dput(dir);
  605. if (!err) {
  606. inode->i_ctime = CURRENT_TIME_SEC;
  607. mark_inode_dirty(inode);
  608. }
  609. out:
  610. return err;
  611. }
  612. /* The following are side effects of other operations that aren't explicitly
  613. * modifying extended attributes. This includes operations such as permissions
  614. * or ownership changes, object deletions, etc. */
  615. static int
  616. reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
  617. loff_t offset, u64 ino, unsigned int d_type)
  618. {
  619. struct dentry *xadir = (struct dentry *)buf;
  620. return __reiserfs_xattr_del(xadir, name, namelen);
  621. }
  622. /* This is called w/ inode->i_mutex downed */
  623. int reiserfs_delete_xattrs(struct inode *inode)
  624. {
  625. struct dentry *dir, *root;
  626. int err = 0;
  627. /* Skip out, an xattr has no xattrs associated with it */
  628. if (is_reiserfs_priv_object(inode) ||
  629. get_inode_sd_version(inode) == STAT_DATA_V1 ||
  630. !reiserfs_xattrs(inode->i_sb)) {
  631. return 0;
  632. }
  633. reiserfs_read_lock_xattrs(inode->i_sb);
  634. dir = open_xa_dir(inode, FL_READONLY);
  635. reiserfs_read_unlock_xattrs(inode->i_sb);
  636. if (IS_ERR(dir)) {
  637. err = PTR_ERR(dir);
  638. goto out;
  639. } else if (!dir->d_inode) {
  640. dput(dir);
  641. return 0;
  642. }
  643. lock_kernel();
  644. err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir);
  645. if (err) {
  646. unlock_kernel();
  647. goto out_dir;
  648. }
  649. /* Leftovers besides . and .. -- that's not good. */
  650. if (dir->d_inode->i_nlink <= 2) {
  651. root = get_xa_root(inode->i_sb, XATTR_REPLACE);
  652. reiserfs_write_lock_xattrs(inode->i_sb);
  653. err = vfs_rmdir(root->d_inode, dir);
  654. reiserfs_write_unlock_xattrs(inode->i_sb);
  655. dput(root);
  656. } else {
  657. reiserfs_warning(inode->i_sb, "jdm-20006",
  658. "Couldn't remove all entries in directory");
  659. }
  660. unlock_kernel();
  661. out_dir:
  662. dput(dir);
  663. out:
  664. if (!err)
  665. REISERFS_I(inode)->i_flags =
  666. REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
  667. return err;
  668. }
  669. struct reiserfs_chown_buf {
  670. struct inode *inode;
  671. struct dentry *xadir;
  672. struct iattr *attrs;
  673. };
  674. /* XXX: If there is a better way to do this, I'd love to hear about it */
  675. static int
  676. reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
  677. loff_t offset, u64 ino, unsigned int d_type)
  678. {
  679. struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
  680. struct dentry *xafile, *xadir = chown_buf->xadir;
  681. struct iattr *attrs = chown_buf->attrs;
  682. int err = 0;
  683. xafile = lookup_one_len(name, xadir, namelen);
  684. if (IS_ERR(xafile))
  685. return PTR_ERR(xafile);
  686. else if (!xafile->d_inode) {
  687. dput(xafile);
  688. return -ENODATA;
  689. }
  690. if (!S_ISDIR(xafile->d_inode->i_mode))
  691. err = notify_change(xafile, attrs);
  692. dput(xafile);
  693. return err;
  694. }
  695. int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  696. {
  697. struct dentry *dir;
  698. int err = 0;
  699. struct reiserfs_chown_buf buf;
  700. unsigned int ia_valid = attrs->ia_valid;
  701. /* Skip out, an xattr has no xattrs associated with it */
  702. if (is_reiserfs_priv_object(inode) ||
  703. get_inode_sd_version(inode) == STAT_DATA_V1 ||
  704. !reiserfs_xattrs(inode->i_sb)) {
  705. return 0;
  706. }
  707. reiserfs_read_lock_xattrs(inode->i_sb);
  708. dir = open_xa_dir(inode, FL_READONLY);
  709. reiserfs_read_unlock_xattrs(inode->i_sb);
  710. if (IS_ERR(dir)) {
  711. if (PTR_ERR(dir) != -ENODATA)
  712. err = PTR_ERR(dir);
  713. goto out;
  714. } else if (!dir->d_inode) {
  715. dput(dir);
  716. goto out;
  717. }
  718. lock_kernel();
  719. attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
  720. buf.xadir = dir;
  721. buf.attrs = attrs;
  722. buf.inode = inode;
  723. err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf);
  724. if (err) {
  725. unlock_kernel();
  726. goto out_dir;
  727. }
  728. err = notify_change(dir, attrs);
  729. unlock_kernel();
  730. out_dir:
  731. dput(dir);
  732. out:
  733. attrs->ia_valid = ia_valid;
  734. return err;
  735. }
  736. /* Actual operations that are exported to VFS-land */
  737. /*
  738. * Inode operation getxattr()
  739. * Preliminary locking: we down dentry->d_inode->i_mutex
  740. */
  741. ssize_t
  742. reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
  743. size_t size)
  744. {
  745. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  746. int err;
  747. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  748. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  749. return -EOPNOTSUPP;
  750. reiserfs_read_lock_xattr_i(dentry->d_inode);
  751. reiserfs_read_lock_xattrs(dentry->d_sb);
  752. err = xah->get(dentry->d_inode, name, buffer, size);
  753. reiserfs_read_unlock_xattrs(dentry->d_sb);
  754. reiserfs_read_unlock_xattr_i(dentry->d_inode);
  755. return err;
  756. }
  757. /*
  758. * Inode operation setxattr()
  759. *
  760. * dentry->d_inode->i_mutex down
  761. */
  762. int
  763. reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  764. size_t size, int flags)
  765. {
  766. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  767. int err;
  768. int lock;
  769. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  770. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  771. return -EOPNOTSUPP;
  772. reiserfs_write_lock_xattr_i(dentry->d_inode);
  773. lock = !has_xattr_dir(dentry->d_inode);
  774. if (lock)
  775. reiserfs_write_lock_xattrs(dentry->d_sb);
  776. else
  777. reiserfs_read_lock_xattrs(dentry->d_sb);
  778. err = xah->set(dentry->d_inode, name, value, size, flags);
  779. if (lock)
  780. reiserfs_write_unlock_xattrs(dentry->d_sb);
  781. else
  782. reiserfs_read_unlock_xattrs(dentry->d_sb);
  783. reiserfs_write_unlock_xattr_i(dentry->d_inode);
  784. return err;
  785. }
  786. /*
  787. * Inode operation removexattr()
  788. *
  789. * dentry->d_inode->i_mutex down
  790. */
  791. int reiserfs_removexattr(struct dentry *dentry, const char *name)
  792. {
  793. int err;
  794. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  795. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  796. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  797. return -EOPNOTSUPP;
  798. reiserfs_write_lock_xattr_i(dentry->d_inode);
  799. reiserfs_read_lock_xattrs(dentry->d_sb);
  800. /* Deletion pre-operation */
  801. if (xah->del) {
  802. err = xah->del(dentry->d_inode, name);
  803. if (err)
  804. goto out;
  805. }
  806. err = reiserfs_xattr_del(dentry->d_inode, name);
  807. dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
  808. mark_inode_dirty(dentry->d_inode);
  809. out:
  810. reiserfs_read_unlock_xattrs(dentry->d_sb);
  811. reiserfs_write_unlock_xattr_i(dentry->d_inode);
  812. return err;
  813. }
  814. /* This is what filldir will use:
  815. * r_pos will always contain the amount of space required for the entire
  816. * list. If r_pos becomes larger than r_size, we need more space and we
  817. * return an error indicating this. If r_pos is less than r_size, then we've
  818. * filled the buffer successfully and we return success */
  819. struct reiserfs_listxattr_buf {
  820. int r_pos;
  821. int r_size;
  822. char *r_buf;
  823. struct inode *r_inode;
  824. };
  825. static int
  826. reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
  827. loff_t offset, u64 ino, unsigned int d_type)
  828. {
  829. struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
  830. int len = 0;
  831. if (name[0] != '.'
  832. || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  833. struct reiserfs_xattr_handler *xah =
  834. find_xattr_handler_prefix(name);
  835. if (!xah)
  836. return 0; /* Unsupported xattr name, skip it */
  837. /* We call ->list() twice because the operation isn't required to just
  838. * return the name back - we want to make sure we have enough space */
  839. len += xah->list(b->r_inode, name, namelen, NULL);
  840. if (len) {
  841. if (b->r_pos + len + 1 <= b->r_size) {
  842. char *p = b->r_buf + b->r_pos;
  843. p += xah->list(b->r_inode, name, namelen, p);
  844. *p++ = '\0';
  845. }
  846. b->r_pos += len + 1;
  847. }
  848. }
  849. return 0;
  850. }
  851. /*
  852. * Inode operation listxattr()
  853. *
  854. * Preliminary locking: we down dentry->d_inode->i_mutex
  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 reiserfs_listxattr_buf buf;
  861. if (!dentry->d_inode)
  862. return -EINVAL;
  863. if (!reiserfs_xattrs(dentry->d_sb) ||
  864. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  865. return -EOPNOTSUPP;
  866. reiserfs_read_lock_xattr_i(dentry->d_inode);
  867. reiserfs_read_lock_xattrs(dentry->d_sb);
  868. dir = open_xa_dir(dentry->d_inode, FL_READONLY);
  869. reiserfs_read_unlock_xattrs(dentry->d_sb);
  870. if (IS_ERR(dir)) {
  871. err = PTR_ERR(dir);
  872. if (err == -ENODATA)
  873. err = 0; /* Not an error if there aren't any xattrs */
  874. goto out;
  875. }
  876. buf.r_buf = buffer;
  877. buf.r_size = buffer ? size : 0;
  878. buf.r_pos = 0;
  879. buf.r_inode = dentry->d_inode;
  880. REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
  881. err = xattr_readdir(dir->d_inode, reiserfs_listxattr_filler, &buf);
  882. if (err)
  883. goto out_dir;
  884. if (buf.r_pos > buf.r_size && buffer != NULL)
  885. err = -ERANGE;
  886. else
  887. err = buf.r_pos;
  888. out_dir:
  889. dput(dir);
  890. out:
  891. reiserfs_read_unlock_xattr_i(dentry->d_inode);
  892. return err;
  893. }
  894. /* This is the implementation for the xattr plugin infrastructure */
  895. static LIST_HEAD(xattr_handlers);
  896. static DEFINE_RWLOCK(handler_lock);
  897. static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
  898. *prefix)
  899. {
  900. struct reiserfs_xattr_handler *xah = NULL;
  901. struct list_head *p;
  902. read_lock(&handler_lock);
  903. list_for_each(p, &xattr_handlers) {
  904. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  905. if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
  906. break;
  907. xah = NULL;
  908. }
  909. read_unlock(&handler_lock);
  910. return xah;
  911. }
  912. static void __unregister_handlers(void)
  913. {
  914. struct reiserfs_xattr_handler *xah;
  915. struct list_head *p, *tmp;
  916. list_for_each_safe(p, tmp, &xattr_handlers) {
  917. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  918. if (xah->exit)
  919. xah->exit();
  920. list_del_init(p);
  921. }
  922. INIT_LIST_HEAD(&xattr_handlers);
  923. }
  924. int __init reiserfs_xattr_register_handlers(void)
  925. {
  926. int err = 0;
  927. struct reiserfs_xattr_handler *xah;
  928. struct list_head *p;
  929. write_lock(&handler_lock);
  930. /* If we're already initialized, nothing to do */
  931. if (!list_empty(&xattr_handlers)) {
  932. write_unlock(&handler_lock);
  933. return 0;
  934. }
  935. /* Add the handlers */
  936. list_add_tail(&user_handler.handlers, &xattr_handlers);
  937. list_add_tail(&trusted_handler.handlers, &xattr_handlers);
  938. #ifdef CONFIG_REISERFS_FS_SECURITY
  939. list_add_tail(&security_handler.handlers, &xattr_handlers);
  940. #endif
  941. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  942. list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
  943. list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
  944. #endif
  945. /* Run initializers, if available */
  946. list_for_each(p, &xattr_handlers) {
  947. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  948. if (xah->init) {
  949. err = xah->init();
  950. if (err) {
  951. list_del_init(p);
  952. break;
  953. }
  954. }
  955. }
  956. /* Clean up other handlers, if any failed */
  957. if (err)
  958. __unregister_handlers();
  959. write_unlock(&handler_lock);
  960. return err;
  961. }
  962. void reiserfs_xattr_unregister_handlers(void)
  963. {
  964. write_lock(&handler_lock);
  965. __unregister_handlers();
  966. write_unlock(&handler_lock);
  967. }
  968. /* This will catch lookups from the fs root to .reiserfs_priv */
  969. static int
  970. xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
  971. {
  972. struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
  973. if (name->len == priv_root->d_name.len &&
  974. name->hash == priv_root->d_name.hash &&
  975. !memcmp(name->name, priv_root->d_name.name, name->len)) {
  976. return -ENOENT;
  977. } else if (q1->len == name->len &&
  978. !memcmp(q1->name, name->name, name->len))
  979. return 0;
  980. return 1;
  981. }
  982. static struct dentry_operations xattr_lookup_poison_ops = {
  983. .d_compare = xattr_lookup_poison,
  984. };
  985. /* We need to take a copy of the mount flags since things like
  986. * MS_RDONLY don't get set until *after* we're called.
  987. * mount_flags != mount_options */
  988. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  989. {
  990. int err = 0;
  991. /* We need generation numbers to ensure that the oid mapping is correct
  992. * v3.5 filesystems don't have them. */
  993. if (!old_format_only(s)) {
  994. set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  995. } else if (reiserfs_xattrs_optional(s)) {
  996. /* Old format filesystem, but optional xattrs have been enabled
  997. * at mount time. Error out. */
  998. reiserfs_warning(s, "jdm-20005",
  999. "xattrs/ACLs not supported on pre v3.6 "
  1000. "format filesystem. Failing mount.");
  1001. err = -EOPNOTSUPP;
  1002. goto error;
  1003. } else {
  1004. /* Old format filesystem, but no optional xattrs have been enabled. This
  1005. * means we silently disable xattrs on the filesystem. */
  1006. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1007. }
  1008. /* If we don't have the privroot located yet - go find it */
  1009. if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
  1010. struct dentry *dentry;
  1011. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  1012. strlen(PRIVROOT_NAME));
  1013. if (!IS_ERR(dentry)) {
  1014. if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
  1015. struct inode *inode = dentry->d_parent->d_inode;
  1016. mutex_lock_nested(&inode->i_mutex,
  1017. I_MUTEX_XATTR);
  1018. err = inode->i_op->mkdir(inode, dentry, 0700);
  1019. mutex_unlock(&inode->i_mutex);
  1020. if (err) {
  1021. dput(dentry);
  1022. dentry = NULL;
  1023. }
  1024. if (dentry && dentry->d_inode)
  1025. reiserfs_info(s, "Created %s - "
  1026. "reserved for xattr "
  1027. "storage.\n",
  1028. PRIVROOT_NAME);
  1029. } else if (!dentry->d_inode) {
  1030. dput(dentry);
  1031. dentry = NULL;
  1032. }
  1033. } else
  1034. err = PTR_ERR(dentry);
  1035. if (!err && dentry) {
  1036. s->s_root->d_op = &xattr_lookup_poison_ops;
  1037. reiserfs_mark_inode_private(dentry->d_inode);
  1038. REISERFS_SB(s)->priv_root = dentry;
  1039. } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
  1040. /* If we're read-only it just means that the dir hasn't been
  1041. * created. Not an error -- just no xattrs on the fs. We'll
  1042. * check again if we go read-write */
  1043. reiserfs_warning(s, "jdm-20006",
  1044. "xattrs/ACLs enabled and couldn't "
  1045. "find/create .reiserfs_priv. "
  1046. "Failing mount.");
  1047. err = -EOPNOTSUPP;
  1048. }
  1049. }
  1050. error:
  1051. /* This is only nonzero if there was an error initializing the xattr
  1052. * directory or if there is a condition where we don't support them. */
  1053. if (err) {
  1054. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1055. clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
  1056. clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
  1057. }
  1058. /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
  1059. s->s_flags = s->s_flags & ~MS_POSIXACL;
  1060. if (reiserfs_posixacl(s))
  1061. s->s_flags |= MS_POSIXACL;
  1062. return err;
  1063. }
  1064. static int reiserfs_check_acl(struct inode *inode, int mask)
  1065. {
  1066. struct posix_acl *acl;
  1067. int error = -EAGAIN; /* do regular unix permission checks by default */
  1068. reiserfs_read_lock_xattr_i(inode);
  1069. reiserfs_read_lock_xattrs(inode->i_sb);
  1070. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  1071. reiserfs_read_unlock_xattrs(inode->i_sb);
  1072. reiserfs_read_unlock_xattr_i(inode);
  1073. if (acl) {
  1074. if (!IS_ERR(acl)) {
  1075. error = posix_acl_permission(inode, acl, mask);
  1076. posix_acl_release(acl);
  1077. } else if (PTR_ERR(acl) != -ENODATA)
  1078. error = PTR_ERR(acl);
  1079. }
  1080. return error;
  1081. }
  1082. int reiserfs_permission(struct inode *inode, int mask)
  1083. {
  1084. /*
  1085. * We don't do permission checks on the internal objects.
  1086. * Permissions are determined by the "owning" object.
  1087. */
  1088. if (is_reiserfs_priv_object(inode))
  1089. return 0;
  1090. /*
  1091. * Stat data v1 doesn't support ACLs.
  1092. */
  1093. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  1094. return generic_permission(inode, mask, NULL);
  1095. else
  1096. return generic_permission(inode, mask, reiserfs_check_acl);
  1097. }