xfs_ioctl.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_format.h"
  21. #include "xfs_log.h"
  22. #include "xfs_trans.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_alloc.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_bmap_btree.h"
  28. #include "xfs_dinode.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_ioctl.h"
  31. #include "xfs_rtalloc.h"
  32. #include "xfs_itable.h"
  33. #include "xfs_error.h"
  34. #include "xfs_attr.h"
  35. #include "xfs_bmap.h"
  36. #include "xfs_bmap_util.h"
  37. #include "xfs_buf_item.h"
  38. #include "xfs_utils.h"
  39. #include "xfs_dfrag.h"
  40. #include "xfs_fsops.h"
  41. #include "xfs_vnodeops.h"
  42. #include "xfs_discard.h"
  43. #include "xfs_quota.h"
  44. #include "xfs_inode_item.h"
  45. #include "xfs_export.h"
  46. #include "xfs_trace.h"
  47. #include "xfs_icache.h"
  48. #include <linux/capability.h>
  49. #include <linux/dcache.h>
  50. #include <linux/mount.h>
  51. #include <linux/namei.h>
  52. #include <linux/pagemap.h>
  53. #include <linux/slab.h>
  54. #include <linux/exportfs.h>
  55. /*
  56. * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
  57. * a file or fs handle.
  58. *
  59. * XFS_IOC_PATH_TO_FSHANDLE
  60. * returns fs handle for a mount point or path within that mount point
  61. * XFS_IOC_FD_TO_HANDLE
  62. * returns full handle for a FD opened in user space
  63. * XFS_IOC_PATH_TO_HANDLE
  64. * returns full handle for a path
  65. */
  66. int
  67. xfs_find_handle(
  68. unsigned int cmd,
  69. xfs_fsop_handlereq_t *hreq)
  70. {
  71. int hsize;
  72. xfs_handle_t handle;
  73. struct inode *inode;
  74. struct fd f = {0};
  75. struct path path;
  76. int error;
  77. struct xfs_inode *ip;
  78. if (cmd == XFS_IOC_FD_TO_HANDLE) {
  79. f = fdget(hreq->fd);
  80. if (!f.file)
  81. return -EBADF;
  82. inode = file_inode(f.file);
  83. } else {
  84. error = user_lpath((const char __user *)hreq->path, &path);
  85. if (error)
  86. return error;
  87. inode = path.dentry->d_inode;
  88. }
  89. ip = XFS_I(inode);
  90. /*
  91. * We can only generate handles for inodes residing on a XFS filesystem,
  92. * and only for regular files, directories or symbolic links.
  93. */
  94. error = -EINVAL;
  95. if (inode->i_sb->s_magic != XFS_SB_MAGIC)
  96. goto out_put;
  97. error = -EBADF;
  98. if (!S_ISREG(inode->i_mode) &&
  99. !S_ISDIR(inode->i_mode) &&
  100. !S_ISLNK(inode->i_mode))
  101. goto out_put;
  102. memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
  103. if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
  104. /*
  105. * This handle only contains an fsid, zero the rest.
  106. */
  107. memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
  108. hsize = sizeof(xfs_fsid_t);
  109. } else {
  110. int lock_mode;
  111. lock_mode = xfs_ilock_map_shared(ip);
  112. handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
  113. sizeof(handle.ha_fid.fid_len);
  114. handle.ha_fid.fid_pad = 0;
  115. handle.ha_fid.fid_gen = ip->i_d.di_gen;
  116. handle.ha_fid.fid_ino = ip->i_ino;
  117. xfs_iunlock_map_shared(ip, lock_mode);
  118. hsize = XFS_HSIZE(handle);
  119. }
  120. error = -EFAULT;
  121. if (copy_to_user(hreq->ohandle, &handle, hsize) ||
  122. copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
  123. goto out_put;
  124. error = 0;
  125. out_put:
  126. if (cmd == XFS_IOC_FD_TO_HANDLE)
  127. fdput(f);
  128. else
  129. path_put(&path);
  130. return error;
  131. }
  132. /*
  133. * No need to do permission checks on the various pathname components
  134. * as the handle operations are privileged.
  135. */
  136. STATIC int
  137. xfs_handle_acceptable(
  138. void *context,
  139. struct dentry *dentry)
  140. {
  141. return 1;
  142. }
  143. /*
  144. * Convert userspace handle data into a dentry.
  145. */
  146. struct dentry *
  147. xfs_handle_to_dentry(
  148. struct file *parfilp,
  149. void __user *uhandle,
  150. u32 hlen)
  151. {
  152. xfs_handle_t handle;
  153. struct xfs_fid64 fid;
  154. /*
  155. * Only allow handle opens under a directory.
  156. */
  157. if (!S_ISDIR(file_inode(parfilp)->i_mode))
  158. return ERR_PTR(-ENOTDIR);
  159. if (hlen != sizeof(xfs_handle_t))
  160. return ERR_PTR(-EINVAL);
  161. if (copy_from_user(&handle, uhandle, hlen))
  162. return ERR_PTR(-EFAULT);
  163. if (handle.ha_fid.fid_len !=
  164. sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
  165. return ERR_PTR(-EINVAL);
  166. memset(&fid, 0, sizeof(struct fid));
  167. fid.ino = handle.ha_fid.fid_ino;
  168. fid.gen = handle.ha_fid.fid_gen;
  169. return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
  170. FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
  171. xfs_handle_acceptable, NULL);
  172. }
  173. STATIC struct dentry *
  174. xfs_handlereq_to_dentry(
  175. struct file *parfilp,
  176. xfs_fsop_handlereq_t *hreq)
  177. {
  178. return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
  179. }
  180. int
  181. xfs_open_by_handle(
  182. struct file *parfilp,
  183. xfs_fsop_handlereq_t *hreq)
  184. {
  185. const struct cred *cred = current_cred();
  186. int error;
  187. int fd;
  188. int permflag;
  189. struct file *filp;
  190. struct inode *inode;
  191. struct dentry *dentry;
  192. fmode_t fmode;
  193. struct path path;
  194. if (!capable(CAP_SYS_ADMIN))
  195. return -XFS_ERROR(EPERM);
  196. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  197. if (IS_ERR(dentry))
  198. return PTR_ERR(dentry);
  199. inode = dentry->d_inode;
  200. /* Restrict xfs_open_by_handle to directories & regular files. */
  201. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  202. error = -XFS_ERROR(EPERM);
  203. goto out_dput;
  204. }
  205. #if BITS_PER_LONG != 32
  206. hreq->oflags |= O_LARGEFILE;
  207. #endif
  208. permflag = hreq->oflags;
  209. fmode = OPEN_FMODE(permflag);
  210. if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
  211. (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
  212. error = -XFS_ERROR(EPERM);
  213. goto out_dput;
  214. }
  215. if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
  216. error = -XFS_ERROR(EACCES);
  217. goto out_dput;
  218. }
  219. /* Can't write directories. */
  220. if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
  221. error = -XFS_ERROR(EISDIR);
  222. goto out_dput;
  223. }
  224. fd = get_unused_fd_flags(0);
  225. if (fd < 0) {
  226. error = fd;
  227. goto out_dput;
  228. }
  229. path.mnt = parfilp->f_path.mnt;
  230. path.dentry = dentry;
  231. filp = dentry_open(&path, hreq->oflags, cred);
  232. dput(dentry);
  233. if (IS_ERR(filp)) {
  234. put_unused_fd(fd);
  235. return PTR_ERR(filp);
  236. }
  237. if (S_ISREG(inode->i_mode)) {
  238. filp->f_flags |= O_NOATIME;
  239. filp->f_mode |= FMODE_NOCMTIME;
  240. }
  241. fd_install(fd, filp);
  242. return fd;
  243. out_dput:
  244. dput(dentry);
  245. return error;
  246. }
  247. /*
  248. * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
  249. * unused first argument.
  250. */
  251. STATIC int
  252. do_readlink(
  253. char __user *buffer,
  254. int buflen,
  255. const char *link)
  256. {
  257. int len;
  258. len = PTR_ERR(link);
  259. if (IS_ERR(link))
  260. goto out;
  261. len = strlen(link);
  262. if (len > (unsigned) buflen)
  263. len = buflen;
  264. if (copy_to_user(buffer, link, len))
  265. len = -EFAULT;
  266. out:
  267. return len;
  268. }
  269. int
  270. xfs_readlink_by_handle(
  271. struct file *parfilp,
  272. xfs_fsop_handlereq_t *hreq)
  273. {
  274. struct dentry *dentry;
  275. __u32 olen;
  276. void *link;
  277. int error;
  278. if (!capable(CAP_SYS_ADMIN))
  279. return -XFS_ERROR(EPERM);
  280. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  281. if (IS_ERR(dentry))
  282. return PTR_ERR(dentry);
  283. /* Restrict this handle operation to symlinks only. */
  284. if (!S_ISLNK(dentry->d_inode->i_mode)) {
  285. error = -XFS_ERROR(EINVAL);
  286. goto out_dput;
  287. }
  288. if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
  289. error = -XFS_ERROR(EFAULT);
  290. goto out_dput;
  291. }
  292. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  293. if (!link) {
  294. error = -XFS_ERROR(ENOMEM);
  295. goto out_dput;
  296. }
  297. error = -xfs_readlink(XFS_I(dentry->d_inode), link);
  298. if (error)
  299. goto out_kfree;
  300. error = do_readlink(hreq->ohandle, olen, link);
  301. if (error)
  302. goto out_kfree;
  303. out_kfree:
  304. kfree(link);
  305. out_dput:
  306. dput(dentry);
  307. return error;
  308. }
  309. STATIC int
  310. xfs_fssetdm_by_handle(
  311. struct file *parfilp,
  312. void __user *arg)
  313. {
  314. int error;
  315. struct fsdmidata fsd;
  316. xfs_fsop_setdm_handlereq_t dmhreq;
  317. struct dentry *dentry;
  318. if (!capable(CAP_MKNOD))
  319. return -XFS_ERROR(EPERM);
  320. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  321. return -XFS_ERROR(EFAULT);
  322. error = mnt_want_write_file(parfilp);
  323. if (error)
  324. return error;
  325. dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
  326. if (IS_ERR(dentry)) {
  327. mnt_drop_write_file(parfilp);
  328. return PTR_ERR(dentry);
  329. }
  330. if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
  331. error = -XFS_ERROR(EPERM);
  332. goto out;
  333. }
  334. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  335. error = -XFS_ERROR(EFAULT);
  336. goto out;
  337. }
  338. error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
  339. fsd.fsd_dmstate);
  340. out:
  341. mnt_drop_write_file(parfilp);
  342. dput(dentry);
  343. return error;
  344. }
  345. STATIC int
  346. xfs_attrlist_by_handle(
  347. struct file *parfilp,
  348. void __user *arg)
  349. {
  350. int error = -ENOMEM;
  351. attrlist_cursor_kern_t *cursor;
  352. xfs_fsop_attrlist_handlereq_t al_hreq;
  353. struct dentry *dentry;
  354. char *kbuf;
  355. if (!capable(CAP_SYS_ADMIN))
  356. return -XFS_ERROR(EPERM);
  357. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  358. return -XFS_ERROR(EFAULT);
  359. if (al_hreq.buflen > XATTR_LIST_MAX)
  360. return -XFS_ERROR(EINVAL);
  361. /*
  362. * Reject flags, only allow namespaces.
  363. */
  364. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  365. return -XFS_ERROR(EINVAL);
  366. dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  367. if (IS_ERR(dentry))
  368. return PTR_ERR(dentry);
  369. kbuf = kmem_zalloc(al_hreq.buflen, KM_SLEEP | KM_MAYFAIL);
  370. if (!kbuf) {
  371. kbuf = kmem_zalloc_large(al_hreq.buflen);
  372. if (!kbuf)
  373. goto out_dput;
  374. }
  375. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  376. error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
  377. al_hreq.flags, cursor);
  378. if (error)
  379. goto out_kfree;
  380. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  381. error = -EFAULT;
  382. out_kfree:
  383. if (is_vmalloc_addr(kbuf))
  384. kmem_free_large(kbuf);
  385. else
  386. kmem_free(kbuf);
  387. out_dput:
  388. dput(dentry);
  389. return error;
  390. }
  391. int
  392. xfs_attrmulti_attr_get(
  393. struct inode *inode,
  394. unsigned char *name,
  395. unsigned char __user *ubuf,
  396. __uint32_t *len,
  397. __uint32_t flags)
  398. {
  399. unsigned char *kbuf;
  400. int error = EFAULT;
  401. if (*len > XATTR_SIZE_MAX)
  402. return EINVAL;
  403. kbuf = kmem_zalloc(*len, KM_SLEEP | KM_MAYFAIL);
  404. if (!kbuf) {
  405. kbuf = kmem_zalloc_large(*len);
  406. if (!kbuf)
  407. return ENOMEM;
  408. }
  409. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
  410. if (error)
  411. goto out_kfree;
  412. if (copy_to_user(ubuf, kbuf, *len))
  413. error = EFAULT;
  414. out_kfree:
  415. if (is_vmalloc_addr(kbuf))
  416. kmem_free_large(kbuf);
  417. else
  418. kmem_free(kbuf);
  419. return error;
  420. }
  421. int
  422. xfs_attrmulti_attr_set(
  423. struct inode *inode,
  424. unsigned char *name,
  425. const unsigned char __user *ubuf,
  426. __uint32_t len,
  427. __uint32_t flags)
  428. {
  429. unsigned char *kbuf;
  430. int error = EFAULT;
  431. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  432. return EPERM;
  433. if (len > XATTR_SIZE_MAX)
  434. return EINVAL;
  435. kbuf = memdup_user(ubuf, len);
  436. if (IS_ERR(kbuf))
  437. return PTR_ERR(kbuf);
  438. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  439. return error;
  440. }
  441. int
  442. xfs_attrmulti_attr_remove(
  443. struct inode *inode,
  444. unsigned char *name,
  445. __uint32_t flags)
  446. {
  447. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  448. return EPERM;
  449. return xfs_attr_remove(XFS_I(inode), name, flags);
  450. }
  451. STATIC int
  452. xfs_attrmulti_by_handle(
  453. struct file *parfilp,
  454. void __user *arg)
  455. {
  456. int error;
  457. xfs_attr_multiop_t *ops;
  458. xfs_fsop_attrmulti_handlereq_t am_hreq;
  459. struct dentry *dentry;
  460. unsigned int i, size;
  461. unsigned char *attr_name;
  462. if (!capable(CAP_SYS_ADMIN))
  463. return -XFS_ERROR(EPERM);
  464. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  465. return -XFS_ERROR(EFAULT);
  466. /* overflow check */
  467. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  468. return -E2BIG;
  469. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  470. if (IS_ERR(dentry))
  471. return PTR_ERR(dentry);
  472. error = E2BIG;
  473. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  474. if (!size || size > 16 * PAGE_SIZE)
  475. goto out_dput;
  476. ops = memdup_user(am_hreq.ops, size);
  477. if (IS_ERR(ops)) {
  478. error = PTR_ERR(ops);
  479. goto out_dput;
  480. }
  481. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  482. if (!attr_name)
  483. goto out_kfree_ops;
  484. error = 0;
  485. for (i = 0; i < am_hreq.opcount; i++) {
  486. ops[i].am_error = strncpy_from_user((char *)attr_name,
  487. ops[i].am_attrname, MAXNAMELEN);
  488. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  489. error = -ERANGE;
  490. if (ops[i].am_error < 0)
  491. break;
  492. switch (ops[i].am_opcode) {
  493. case ATTR_OP_GET:
  494. ops[i].am_error = xfs_attrmulti_attr_get(
  495. dentry->d_inode, attr_name,
  496. ops[i].am_attrvalue, &ops[i].am_length,
  497. ops[i].am_flags);
  498. break;
  499. case ATTR_OP_SET:
  500. ops[i].am_error = mnt_want_write_file(parfilp);
  501. if (ops[i].am_error)
  502. break;
  503. ops[i].am_error = xfs_attrmulti_attr_set(
  504. dentry->d_inode, attr_name,
  505. ops[i].am_attrvalue, ops[i].am_length,
  506. ops[i].am_flags);
  507. mnt_drop_write_file(parfilp);
  508. break;
  509. case ATTR_OP_REMOVE:
  510. ops[i].am_error = mnt_want_write_file(parfilp);
  511. if (ops[i].am_error)
  512. break;
  513. ops[i].am_error = xfs_attrmulti_attr_remove(
  514. dentry->d_inode, attr_name,
  515. ops[i].am_flags);
  516. mnt_drop_write_file(parfilp);
  517. break;
  518. default:
  519. ops[i].am_error = EINVAL;
  520. }
  521. }
  522. if (copy_to_user(am_hreq.ops, ops, size))
  523. error = XFS_ERROR(EFAULT);
  524. kfree(attr_name);
  525. out_kfree_ops:
  526. kfree(ops);
  527. out_dput:
  528. dput(dentry);
  529. return -error;
  530. }
  531. int
  532. xfs_ioc_space(
  533. struct xfs_inode *ip,
  534. struct inode *inode,
  535. struct file *filp,
  536. int ioflags,
  537. unsigned int cmd,
  538. xfs_flock64_t *bf)
  539. {
  540. int attr_flags = 0;
  541. int error;
  542. /*
  543. * Only allow the sys admin to reserve space unless
  544. * unwritten extents are enabled.
  545. */
  546. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  547. !capable(CAP_SYS_ADMIN))
  548. return -XFS_ERROR(EPERM);
  549. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  550. return -XFS_ERROR(EPERM);
  551. if (!(filp->f_mode & FMODE_WRITE))
  552. return -XFS_ERROR(EBADF);
  553. if (!S_ISREG(inode->i_mode))
  554. return -XFS_ERROR(EINVAL);
  555. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  556. attr_flags |= XFS_ATTR_NONBLOCK;
  557. if (filp->f_flags & O_DSYNC)
  558. attr_flags |= XFS_ATTR_SYNC;
  559. if (ioflags & IO_INVIS)
  560. attr_flags |= XFS_ATTR_DMI;
  561. error = mnt_want_write_file(filp);
  562. if (error)
  563. return error;
  564. error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
  565. mnt_drop_write_file(filp);
  566. return -error;
  567. }
  568. STATIC int
  569. xfs_ioc_bulkstat(
  570. xfs_mount_t *mp,
  571. unsigned int cmd,
  572. void __user *arg)
  573. {
  574. xfs_fsop_bulkreq_t bulkreq;
  575. int count; /* # of records returned */
  576. xfs_ino_t inlast; /* last inode number */
  577. int done;
  578. int error;
  579. /* done = 1 if there are more stats to get and if bulkstat */
  580. /* should be called again (unused here, but used in dmapi) */
  581. if (!capable(CAP_SYS_ADMIN))
  582. return -EPERM;
  583. if (XFS_FORCED_SHUTDOWN(mp))
  584. return -XFS_ERROR(EIO);
  585. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  586. return -XFS_ERROR(EFAULT);
  587. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  588. return -XFS_ERROR(EFAULT);
  589. if ((count = bulkreq.icount) <= 0)
  590. return -XFS_ERROR(EINVAL);
  591. if (bulkreq.ubuffer == NULL)
  592. return -XFS_ERROR(EINVAL);
  593. if (cmd == XFS_IOC_FSINUMBERS)
  594. error = xfs_inumbers(mp, &inlast, &count,
  595. bulkreq.ubuffer, xfs_inumbers_fmt);
  596. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  597. error = xfs_bulkstat_single(mp, &inlast,
  598. bulkreq.ubuffer, &done);
  599. else /* XFS_IOC_FSBULKSTAT */
  600. error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
  601. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  602. &done);
  603. if (error)
  604. return -error;
  605. if (bulkreq.ocount != NULL) {
  606. if (copy_to_user(bulkreq.lastip, &inlast,
  607. sizeof(xfs_ino_t)))
  608. return -XFS_ERROR(EFAULT);
  609. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  610. return -XFS_ERROR(EFAULT);
  611. }
  612. return 0;
  613. }
  614. STATIC int
  615. xfs_ioc_fsgeometry_v1(
  616. xfs_mount_t *mp,
  617. void __user *arg)
  618. {
  619. xfs_fsop_geom_t fsgeo;
  620. int error;
  621. error = xfs_fs_geometry(mp, &fsgeo, 3);
  622. if (error)
  623. return -error;
  624. /*
  625. * Caller should have passed an argument of type
  626. * xfs_fsop_geom_v1_t. This is a proper subset of the
  627. * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
  628. */
  629. if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
  630. return -XFS_ERROR(EFAULT);
  631. return 0;
  632. }
  633. STATIC int
  634. xfs_ioc_fsgeometry(
  635. xfs_mount_t *mp,
  636. void __user *arg)
  637. {
  638. xfs_fsop_geom_t fsgeo;
  639. int error;
  640. error = xfs_fs_geometry(mp, &fsgeo, 4);
  641. if (error)
  642. return -error;
  643. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  644. return -XFS_ERROR(EFAULT);
  645. return 0;
  646. }
  647. /*
  648. * Linux extended inode flags interface.
  649. */
  650. STATIC unsigned int
  651. xfs_merge_ioc_xflags(
  652. unsigned int flags,
  653. unsigned int start)
  654. {
  655. unsigned int xflags = start;
  656. if (flags & FS_IMMUTABLE_FL)
  657. xflags |= XFS_XFLAG_IMMUTABLE;
  658. else
  659. xflags &= ~XFS_XFLAG_IMMUTABLE;
  660. if (flags & FS_APPEND_FL)
  661. xflags |= XFS_XFLAG_APPEND;
  662. else
  663. xflags &= ~XFS_XFLAG_APPEND;
  664. if (flags & FS_SYNC_FL)
  665. xflags |= XFS_XFLAG_SYNC;
  666. else
  667. xflags &= ~XFS_XFLAG_SYNC;
  668. if (flags & FS_NOATIME_FL)
  669. xflags |= XFS_XFLAG_NOATIME;
  670. else
  671. xflags &= ~XFS_XFLAG_NOATIME;
  672. if (flags & FS_NODUMP_FL)
  673. xflags |= XFS_XFLAG_NODUMP;
  674. else
  675. xflags &= ~XFS_XFLAG_NODUMP;
  676. return xflags;
  677. }
  678. STATIC unsigned int
  679. xfs_di2lxflags(
  680. __uint16_t di_flags)
  681. {
  682. unsigned int flags = 0;
  683. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  684. flags |= FS_IMMUTABLE_FL;
  685. if (di_flags & XFS_DIFLAG_APPEND)
  686. flags |= FS_APPEND_FL;
  687. if (di_flags & XFS_DIFLAG_SYNC)
  688. flags |= FS_SYNC_FL;
  689. if (di_flags & XFS_DIFLAG_NOATIME)
  690. flags |= FS_NOATIME_FL;
  691. if (di_flags & XFS_DIFLAG_NODUMP)
  692. flags |= FS_NODUMP_FL;
  693. return flags;
  694. }
  695. STATIC int
  696. xfs_ioc_fsgetxattr(
  697. xfs_inode_t *ip,
  698. int attr,
  699. void __user *arg)
  700. {
  701. struct fsxattr fa;
  702. memset(&fa, 0, sizeof(struct fsxattr));
  703. xfs_ilock(ip, XFS_ILOCK_SHARED);
  704. fa.fsx_xflags = xfs_ip2xflags(ip);
  705. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  706. fa.fsx_projid = xfs_get_projid(ip);
  707. if (attr) {
  708. if (ip->i_afp) {
  709. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  710. fa.fsx_nextents = ip->i_afp->if_bytes /
  711. sizeof(xfs_bmbt_rec_t);
  712. else
  713. fa.fsx_nextents = ip->i_d.di_anextents;
  714. } else
  715. fa.fsx_nextents = 0;
  716. } else {
  717. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  718. fa.fsx_nextents = ip->i_df.if_bytes /
  719. sizeof(xfs_bmbt_rec_t);
  720. else
  721. fa.fsx_nextents = ip->i_d.di_nextents;
  722. }
  723. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  724. if (copy_to_user(arg, &fa, sizeof(fa)))
  725. return -EFAULT;
  726. return 0;
  727. }
  728. STATIC void
  729. xfs_set_diflags(
  730. struct xfs_inode *ip,
  731. unsigned int xflags)
  732. {
  733. unsigned int di_flags;
  734. /* can't set PREALLOC this way, just preserve it */
  735. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  736. if (xflags & XFS_XFLAG_IMMUTABLE)
  737. di_flags |= XFS_DIFLAG_IMMUTABLE;
  738. if (xflags & XFS_XFLAG_APPEND)
  739. di_flags |= XFS_DIFLAG_APPEND;
  740. if (xflags & XFS_XFLAG_SYNC)
  741. di_flags |= XFS_DIFLAG_SYNC;
  742. if (xflags & XFS_XFLAG_NOATIME)
  743. di_flags |= XFS_DIFLAG_NOATIME;
  744. if (xflags & XFS_XFLAG_NODUMP)
  745. di_flags |= XFS_DIFLAG_NODUMP;
  746. if (xflags & XFS_XFLAG_PROJINHERIT)
  747. di_flags |= XFS_DIFLAG_PROJINHERIT;
  748. if (xflags & XFS_XFLAG_NODEFRAG)
  749. di_flags |= XFS_DIFLAG_NODEFRAG;
  750. if (xflags & XFS_XFLAG_FILESTREAM)
  751. di_flags |= XFS_DIFLAG_FILESTREAM;
  752. if (S_ISDIR(ip->i_d.di_mode)) {
  753. if (xflags & XFS_XFLAG_RTINHERIT)
  754. di_flags |= XFS_DIFLAG_RTINHERIT;
  755. if (xflags & XFS_XFLAG_NOSYMLINKS)
  756. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  757. if (xflags & XFS_XFLAG_EXTSZINHERIT)
  758. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  759. } else if (S_ISREG(ip->i_d.di_mode)) {
  760. if (xflags & XFS_XFLAG_REALTIME)
  761. di_flags |= XFS_DIFLAG_REALTIME;
  762. if (xflags & XFS_XFLAG_EXTSIZE)
  763. di_flags |= XFS_DIFLAG_EXTSIZE;
  764. }
  765. ip->i_d.di_flags = di_flags;
  766. }
  767. STATIC void
  768. xfs_diflags_to_linux(
  769. struct xfs_inode *ip)
  770. {
  771. struct inode *inode = VFS_I(ip);
  772. unsigned int xflags = xfs_ip2xflags(ip);
  773. if (xflags & XFS_XFLAG_IMMUTABLE)
  774. inode->i_flags |= S_IMMUTABLE;
  775. else
  776. inode->i_flags &= ~S_IMMUTABLE;
  777. if (xflags & XFS_XFLAG_APPEND)
  778. inode->i_flags |= S_APPEND;
  779. else
  780. inode->i_flags &= ~S_APPEND;
  781. if (xflags & XFS_XFLAG_SYNC)
  782. inode->i_flags |= S_SYNC;
  783. else
  784. inode->i_flags &= ~S_SYNC;
  785. if (xflags & XFS_XFLAG_NOATIME)
  786. inode->i_flags |= S_NOATIME;
  787. else
  788. inode->i_flags &= ~S_NOATIME;
  789. }
  790. #define FSX_PROJID 1
  791. #define FSX_EXTSIZE 2
  792. #define FSX_XFLAGS 4
  793. #define FSX_NONBLOCK 8
  794. STATIC int
  795. xfs_ioctl_setattr(
  796. xfs_inode_t *ip,
  797. struct fsxattr *fa,
  798. int mask)
  799. {
  800. struct xfs_mount *mp = ip->i_mount;
  801. struct xfs_trans *tp;
  802. unsigned int lock_flags = 0;
  803. struct xfs_dquot *udqp = NULL;
  804. struct xfs_dquot *pdqp = NULL;
  805. struct xfs_dquot *olddquot = NULL;
  806. int code;
  807. trace_xfs_ioctl_setattr(ip);
  808. if (mp->m_flags & XFS_MOUNT_RDONLY)
  809. return XFS_ERROR(EROFS);
  810. if (XFS_FORCED_SHUTDOWN(mp))
  811. return XFS_ERROR(EIO);
  812. /*
  813. * Disallow 32bit project ids when projid32bit feature is not enabled.
  814. */
  815. if ((mask & FSX_PROJID) && (fa->fsx_projid > (__uint16_t)-1) &&
  816. !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
  817. return XFS_ERROR(EINVAL);
  818. /*
  819. * If disk quotas is on, we make sure that the dquots do exist on disk,
  820. * before we start any other transactions. Trying to do this later
  821. * is messy. We don't care to take a readlock to look at the ids
  822. * in inode here, because we can't hold it across the trans_reserve.
  823. * If the IDs do change before we take the ilock, we're covered
  824. * because the i_*dquot fields will get updated anyway.
  825. */
  826. if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
  827. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  828. ip->i_d.di_gid, fa->fsx_projid,
  829. XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
  830. if (code)
  831. return code;
  832. }
  833. /*
  834. * For the other attributes, we acquire the inode lock and
  835. * first do an error checking pass.
  836. */
  837. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  838. code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  839. if (code)
  840. goto error_return;
  841. lock_flags = XFS_ILOCK_EXCL;
  842. xfs_ilock(ip, lock_flags);
  843. /*
  844. * CAP_FOWNER overrides the following restrictions:
  845. *
  846. * The user ID of the calling process must be equal
  847. * to the file owner ID, except in cases where the
  848. * CAP_FSETID capability is applicable.
  849. */
  850. if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
  851. code = XFS_ERROR(EPERM);
  852. goto error_return;
  853. }
  854. /*
  855. * Do a quota reservation only if projid is actually going to change.
  856. */
  857. if (mask & FSX_PROJID) {
  858. if (XFS_IS_QUOTA_RUNNING(mp) &&
  859. XFS_IS_PQUOTA_ON(mp) &&
  860. xfs_get_projid(ip) != fa->fsx_projid) {
  861. ASSERT(tp);
  862. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL,
  863. pdqp, capable(CAP_FOWNER) ?
  864. XFS_QMOPT_FORCE_RES : 0);
  865. if (code) /* out of quota */
  866. goto error_return;
  867. }
  868. }
  869. if (mask & FSX_EXTSIZE) {
  870. /*
  871. * Can't change extent size if any extents are allocated.
  872. */
  873. if (ip->i_d.di_nextents &&
  874. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
  875. fa->fsx_extsize)) {
  876. code = XFS_ERROR(EINVAL); /* EFBIG? */
  877. goto error_return;
  878. }
  879. /*
  880. * Extent size must be a multiple of the appropriate block
  881. * size, if set at all. It must also be smaller than the
  882. * maximum extent size supported by the filesystem.
  883. *
  884. * Also, for non-realtime files, limit the extent size hint to
  885. * half the size of the AGs in the filesystem so alignment
  886. * doesn't result in extents larger than an AG.
  887. */
  888. if (fa->fsx_extsize != 0) {
  889. xfs_extlen_t size;
  890. xfs_fsblock_t extsize_fsb;
  891. extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
  892. if (extsize_fsb > MAXEXTLEN) {
  893. code = XFS_ERROR(EINVAL);
  894. goto error_return;
  895. }
  896. if (XFS_IS_REALTIME_INODE(ip) ||
  897. ((mask & FSX_XFLAGS) &&
  898. (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
  899. size = mp->m_sb.sb_rextsize <<
  900. mp->m_sb.sb_blocklog;
  901. } else {
  902. size = mp->m_sb.sb_blocksize;
  903. if (extsize_fsb > mp->m_sb.sb_agblocks / 2) {
  904. code = XFS_ERROR(EINVAL);
  905. goto error_return;
  906. }
  907. }
  908. if (fa->fsx_extsize % size) {
  909. code = XFS_ERROR(EINVAL);
  910. goto error_return;
  911. }
  912. }
  913. }
  914. if (mask & FSX_XFLAGS) {
  915. /*
  916. * Can't change realtime flag if any extents are allocated.
  917. */
  918. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  919. (XFS_IS_REALTIME_INODE(ip)) !=
  920. (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  921. code = XFS_ERROR(EINVAL); /* EFBIG? */
  922. goto error_return;
  923. }
  924. /*
  925. * If realtime flag is set then must have realtime data.
  926. */
  927. if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  928. if ((mp->m_sb.sb_rblocks == 0) ||
  929. (mp->m_sb.sb_rextsize == 0) ||
  930. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
  931. code = XFS_ERROR(EINVAL);
  932. goto error_return;
  933. }
  934. }
  935. /*
  936. * Can't modify an immutable/append-only file unless
  937. * we have appropriate permission.
  938. */
  939. if ((ip->i_d.di_flags &
  940. (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
  941. (fa->fsx_xflags &
  942. (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  943. !capable(CAP_LINUX_IMMUTABLE)) {
  944. code = XFS_ERROR(EPERM);
  945. goto error_return;
  946. }
  947. }
  948. xfs_trans_ijoin(tp, ip, 0);
  949. /*
  950. * Change file ownership. Must be the owner or privileged.
  951. */
  952. if (mask & FSX_PROJID) {
  953. /*
  954. * CAP_FSETID overrides the following restrictions:
  955. *
  956. * The set-user-ID and set-group-ID bits of a file will be
  957. * cleared upon successful return from chown()
  958. */
  959. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  960. !capable(CAP_FSETID))
  961. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  962. /*
  963. * Change the ownerships and register quota modifications
  964. * in the transaction.
  965. */
  966. if (xfs_get_projid(ip) != fa->fsx_projid) {
  967. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  968. olddquot = xfs_qm_vop_chown(tp, ip,
  969. &ip->i_pdquot, pdqp);
  970. }
  971. xfs_set_projid(ip, fa->fsx_projid);
  972. /*
  973. * We may have to rev the inode as well as
  974. * the superblock version number since projids didn't
  975. * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
  976. */
  977. if (ip->i_d.di_version == 1)
  978. xfs_bump_ino_vers2(tp, ip);
  979. }
  980. }
  981. if (mask & FSX_EXTSIZE)
  982. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  983. if (mask & FSX_XFLAGS) {
  984. xfs_set_diflags(ip, fa->fsx_xflags);
  985. xfs_diflags_to_linux(ip);
  986. }
  987. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  988. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  989. XFS_STATS_INC(xs_ig_attrchg);
  990. /*
  991. * If this is a synchronous mount, make sure that the
  992. * transaction goes to disk before returning to the user.
  993. * This is slightly sub-optimal in that truncates require
  994. * two sync transactions instead of one for wsync filesystems.
  995. * One for the truncate and one for the timestamps since we
  996. * don't want to change the timestamps unless we're sure the
  997. * truncate worked. Truncates are less than 1% of the laddis
  998. * mix so this probably isn't worth the trouble to optimize.
  999. */
  1000. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1001. xfs_trans_set_sync(tp);
  1002. code = xfs_trans_commit(tp, 0);
  1003. xfs_iunlock(ip, lock_flags);
  1004. /*
  1005. * Release any dquot(s) the inode had kept before chown.
  1006. */
  1007. xfs_qm_dqrele(olddquot);
  1008. xfs_qm_dqrele(udqp);
  1009. xfs_qm_dqrele(pdqp);
  1010. return code;
  1011. error_return:
  1012. xfs_qm_dqrele(udqp);
  1013. xfs_qm_dqrele(pdqp);
  1014. xfs_trans_cancel(tp, 0);
  1015. if (lock_flags)
  1016. xfs_iunlock(ip, lock_flags);
  1017. return code;
  1018. }
  1019. STATIC int
  1020. xfs_ioc_fssetxattr(
  1021. xfs_inode_t *ip,
  1022. struct file *filp,
  1023. void __user *arg)
  1024. {
  1025. struct fsxattr fa;
  1026. unsigned int mask;
  1027. int error;
  1028. if (copy_from_user(&fa, arg, sizeof(fa)))
  1029. return -EFAULT;
  1030. mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
  1031. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1032. mask |= FSX_NONBLOCK;
  1033. error = mnt_want_write_file(filp);
  1034. if (error)
  1035. return error;
  1036. error = xfs_ioctl_setattr(ip, &fa, mask);
  1037. mnt_drop_write_file(filp);
  1038. return -error;
  1039. }
  1040. STATIC int
  1041. xfs_ioc_getxflags(
  1042. xfs_inode_t *ip,
  1043. void __user *arg)
  1044. {
  1045. unsigned int flags;
  1046. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1047. if (copy_to_user(arg, &flags, sizeof(flags)))
  1048. return -EFAULT;
  1049. return 0;
  1050. }
  1051. STATIC int
  1052. xfs_ioc_setxflags(
  1053. xfs_inode_t *ip,
  1054. struct file *filp,
  1055. void __user *arg)
  1056. {
  1057. struct fsxattr fa;
  1058. unsigned int flags;
  1059. unsigned int mask;
  1060. int error;
  1061. if (copy_from_user(&flags, arg, sizeof(flags)))
  1062. return -EFAULT;
  1063. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1064. FS_NOATIME_FL | FS_NODUMP_FL | \
  1065. FS_SYNC_FL))
  1066. return -EOPNOTSUPP;
  1067. mask = FSX_XFLAGS;
  1068. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1069. mask |= FSX_NONBLOCK;
  1070. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1071. error = mnt_want_write_file(filp);
  1072. if (error)
  1073. return error;
  1074. error = xfs_ioctl_setattr(ip, &fa, mask);
  1075. mnt_drop_write_file(filp);
  1076. return -error;
  1077. }
  1078. STATIC int
  1079. xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
  1080. {
  1081. struct getbmap __user *base = *ap;
  1082. /* copy only getbmap portion (not getbmapx) */
  1083. if (copy_to_user(base, bmv, sizeof(struct getbmap)))
  1084. return XFS_ERROR(EFAULT);
  1085. *ap += sizeof(struct getbmap);
  1086. return 0;
  1087. }
  1088. STATIC int
  1089. xfs_ioc_getbmap(
  1090. struct xfs_inode *ip,
  1091. int ioflags,
  1092. unsigned int cmd,
  1093. void __user *arg)
  1094. {
  1095. struct getbmapx bmx;
  1096. int error;
  1097. if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
  1098. return -XFS_ERROR(EFAULT);
  1099. if (bmx.bmv_count < 2)
  1100. return -XFS_ERROR(EINVAL);
  1101. bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1102. if (ioflags & IO_INVIS)
  1103. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1104. error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
  1105. (struct getbmap *)arg+1);
  1106. if (error)
  1107. return -error;
  1108. /* copy back header - only size of getbmap */
  1109. if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
  1110. return -XFS_ERROR(EFAULT);
  1111. return 0;
  1112. }
  1113. STATIC int
  1114. xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
  1115. {
  1116. struct getbmapx __user *base = *ap;
  1117. if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
  1118. return XFS_ERROR(EFAULT);
  1119. *ap += sizeof(struct getbmapx);
  1120. return 0;
  1121. }
  1122. STATIC int
  1123. xfs_ioc_getbmapx(
  1124. struct xfs_inode *ip,
  1125. void __user *arg)
  1126. {
  1127. struct getbmapx bmx;
  1128. int error;
  1129. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1130. return -XFS_ERROR(EFAULT);
  1131. if (bmx.bmv_count < 2)
  1132. return -XFS_ERROR(EINVAL);
  1133. if (bmx.bmv_iflags & (~BMV_IF_VALID))
  1134. return -XFS_ERROR(EINVAL);
  1135. error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
  1136. (struct getbmapx *)arg+1);
  1137. if (error)
  1138. return -error;
  1139. /* copy back header */
  1140. if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
  1141. return -XFS_ERROR(EFAULT);
  1142. return 0;
  1143. }
  1144. /*
  1145. * Note: some of the ioctl's return positive numbers as a
  1146. * byte count indicating success, such as readlink_by_handle.
  1147. * So we don't "sign flip" like most other routines. This means
  1148. * true errors need to be returned as a negative value.
  1149. */
  1150. long
  1151. xfs_file_ioctl(
  1152. struct file *filp,
  1153. unsigned int cmd,
  1154. unsigned long p)
  1155. {
  1156. struct inode *inode = file_inode(filp);
  1157. struct xfs_inode *ip = XFS_I(inode);
  1158. struct xfs_mount *mp = ip->i_mount;
  1159. void __user *arg = (void __user *)p;
  1160. int ioflags = 0;
  1161. int error;
  1162. if (filp->f_mode & FMODE_NOCMTIME)
  1163. ioflags |= IO_INVIS;
  1164. trace_xfs_file_ioctl(ip);
  1165. switch (cmd) {
  1166. case FITRIM:
  1167. return xfs_ioc_trim(mp, arg);
  1168. case XFS_IOC_ALLOCSP:
  1169. case XFS_IOC_FREESP:
  1170. case XFS_IOC_RESVSP:
  1171. case XFS_IOC_UNRESVSP:
  1172. case XFS_IOC_ALLOCSP64:
  1173. case XFS_IOC_FREESP64:
  1174. case XFS_IOC_RESVSP64:
  1175. case XFS_IOC_UNRESVSP64:
  1176. case XFS_IOC_ZERO_RANGE: {
  1177. xfs_flock64_t bf;
  1178. if (copy_from_user(&bf, arg, sizeof(bf)))
  1179. return -XFS_ERROR(EFAULT);
  1180. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
  1181. }
  1182. case XFS_IOC_DIOINFO: {
  1183. struct dioattr da;
  1184. xfs_buftarg_t *target =
  1185. XFS_IS_REALTIME_INODE(ip) ?
  1186. mp->m_rtdev_targp : mp->m_ddev_targp;
  1187. da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
  1188. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1189. if (copy_to_user(arg, &da, sizeof(da)))
  1190. return -XFS_ERROR(EFAULT);
  1191. return 0;
  1192. }
  1193. case XFS_IOC_FSBULKSTAT_SINGLE:
  1194. case XFS_IOC_FSBULKSTAT:
  1195. case XFS_IOC_FSINUMBERS:
  1196. return xfs_ioc_bulkstat(mp, cmd, arg);
  1197. case XFS_IOC_FSGEOMETRY_V1:
  1198. return xfs_ioc_fsgeometry_v1(mp, arg);
  1199. case XFS_IOC_FSGEOMETRY:
  1200. return xfs_ioc_fsgeometry(mp, arg);
  1201. case XFS_IOC_GETVERSION:
  1202. return put_user(inode->i_generation, (int __user *)arg);
  1203. case XFS_IOC_FSGETXATTR:
  1204. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1205. case XFS_IOC_FSGETXATTRA:
  1206. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1207. case XFS_IOC_FSSETXATTR:
  1208. return xfs_ioc_fssetxattr(ip, filp, arg);
  1209. case XFS_IOC_GETXFLAGS:
  1210. return xfs_ioc_getxflags(ip, arg);
  1211. case XFS_IOC_SETXFLAGS:
  1212. return xfs_ioc_setxflags(ip, filp, arg);
  1213. case XFS_IOC_FSSETDM: {
  1214. struct fsdmidata dmi;
  1215. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1216. return -XFS_ERROR(EFAULT);
  1217. error = mnt_want_write_file(filp);
  1218. if (error)
  1219. return error;
  1220. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1221. dmi.fsd_dmstate);
  1222. mnt_drop_write_file(filp);
  1223. return -error;
  1224. }
  1225. case XFS_IOC_GETBMAP:
  1226. case XFS_IOC_GETBMAPA:
  1227. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  1228. case XFS_IOC_GETBMAPX:
  1229. return xfs_ioc_getbmapx(ip, arg);
  1230. case XFS_IOC_FD_TO_HANDLE:
  1231. case XFS_IOC_PATH_TO_HANDLE:
  1232. case XFS_IOC_PATH_TO_FSHANDLE: {
  1233. xfs_fsop_handlereq_t hreq;
  1234. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1235. return -XFS_ERROR(EFAULT);
  1236. return xfs_find_handle(cmd, &hreq);
  1237. }
  1238. case XFS_IOC_OPEN_BY_HANDLE: {
  1239. xfs_fsop_handlereq_t hreq;
  1240. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1241. return -XFS_ERROR(EFAULT);
  1242. return xfs_open_by_handle(filp, &hreq);
  1243. }
  1244. case XFS_IOC_FSSETDM_BY_HANDLE:
  1245. return xfs_fssetdm_by_handle(filp, arg);
  1246. case XFS_IOC_READLINK_BY_HANDLE: {
  1247. xfs_fsop_handlereq_t hreq;
  1248. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1249. return -XFS_ERROR(EFAULT);
  1250. return xfs_readlink_by_handle(filp, &hreq);
  1251. }
  1252. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1253. return xfs_attrlist_by_handle(filp, arg);
  1254. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1255. return xfs_attrmulti_by_handle(filp, arg);
  1256. case XFS_IOC_SWAPEXT: {
  1257. struct xfs_swapext sxp;
  1258. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1259. return -XFS_ERROR(EFAULT);
  1260. error = mnt_want_write_file(filp);
  1261. if (error)
  1262. return error;
  1263. error = xfs_swapext(&sxp);
  1264. mnt_drop_write_file(filp);
  1265. return -error;
  1266. }
  1267. case XFS_IOC_FSCOUNTS: {
  1268. xfs_fsop_counts_t out;
  1269. error = xfs_fs_counts(mp, &out);
  1270. if (error)
  1271. return -error;
  1272. if (copy_to_user(arg, &out, sizeof(out)))
  1273. return -XFS_ERROR(EFAULT);
  1274. return 0;
  1275. }
  1276. case XFS_IOC_SET_RESBLKS: {
  1277. xfs_fsop_resblks_t inout;
  1278. __uint64_t in;
  1279. if (!capable(CAP_SYS_ADMIN))
  1280. return -EPERM;
  1281. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1282. return -XFS_ERROR(EROFS);
  1283. if (copy_from_user(&inout, arg, sizeof(inout)))
  1284. return -XFS_ERROR(EFAULT);
  1285. error = mnt_want_write_file(filp);
  1286. if (error)
  1287. return error;
  1288. /* input parameter is passed in resblks field of structure */
  1289. in = inout.resblks;
  1290. error = xfs_reserve_blocks(mp, &in, &inout);
  1291. mnt_drop_write_file(filp);
  1292. if (error)
  1293. return -error;
  1294. if (copy_to_user(arg, &inout, sizeof(inout)))
  1295. return -XFS_ERROR(EFAULT);
  1296. return 0;
  1297. }
  1298. case XFS_IOC_GET_RESBLKS: {
  1299. xfs_fsop_resblks_t out;
  1300. if (!capable(CAP_SYS_ADMIN))
  1301. return -EPERM;
  1302. error = xfs_reserve_blocks(mp, NULL, &out);
  1303. if (error)
  1304. return -error;
  1305. if (copy_to_user(arg, &out, sizeof(out)))
  1306. return -XFS_ERROR(EFAULT);
  1307. return 0;
  1308. }
  1309. case XFS_IOC_FSGROWFSDATA: {
  1310. xfs_growfs_data_t in;
  1311. if (copy_from_user(&in, arg, sizeof(in)))
  1312. return -XFS_ERROR(EFAULT);
  1313. error = mnt_want_write_file(filp);
  1314. if (error)
  1315. return error;
  1316. error = xfs_growfs_data(mp, &in);
  1317. mnt_drop_write_file(filp);
  1318. return -error;
  1319. }
  1320. case XFS_IOC_FSGROWFSLOG: {
  1321. xfs_growfs_log_t in;
  1322. if (copy_from_user(&in, arg, sizeof(in)))
  1323. return -XFS_ERROR(EFAULT);
  1324. error = mnt_want_write_file(filp);
  1325. if (error)
  1326. return error;
  1327. error = xfs_growfs_log(mp, &in);
  1328. mnt_drop_write_file(filp);
  1329. return -error;
  1330. }
  1331. case XFS_IOC_FSGROWFSRT: {
  1332. xfs_growfs_rt_t in;
  1333. if (copy_from_user(&in, arg, sizeof(in)))
  1334. return -XFS_ERROR(EFAULT);
  1335. error = mnt_want_write_file(filp);
  1336. if (error)
  1337. return error;
  1338. error = xfs_growfs_rt(mp, &in);
  1339. mnt_drop_write_file(filp);
  1340. return -error;
  1341. }
  1342. case XFS_IOC_GOINGDOWN: {
  1343. __uint32_t in;
  1344. if (!capable(CAP_SYS_ADMIN))
  1345. return -EPERM;
  1346. if (get_user(in, (__uint32_t __user *)arg))
  1347. return -XFS_ERROR(EFAULT);
  1348. error = xfs_fs_goingdown(mp, in);
  1349. return -error;
  1350. }
  1351. case XFS_IOC_ERROR_INJECTION: {
  1352. xfs_error_injection_t in;
  1353. if (!capable(CAP_SYS_ADMIN))
  1354. return -EPERM;
  1355. if (copy_from_user(&in, arg, sizeof(in)))
  1356. return -XFS_ERROR(EFAULT);
  1357. error = xfs_errortag_add(in.errtag, mp);
  1358. return -error;
  1359. }
  1360. case XFS_IOC_ERROR_CLEARALL:
  1361. if (!capable(CAP_SYS_ADMIN))
  1362. return -EPERM;
  1363. error = xfs_errortag_clearall(mp, 1);
  1364. return -error;
  1365. case XFS_IOC_FREE_EOFBLOCKS: {
  1366. struct xfs_eofblocks eofb;
  1367. if (copy_from_user(&eofb, arg, sizeof(eofb)))
  1368. return -XFS_ERROR(EFAULT);
  1369. if (eofb.eof_version != XFS_EOFBLOCKS_VERSION)
  1370. return -XFS_ERROR(EINVAL);
  1371. if (eofb.eof_flags & ~XFS_EOF_FLAGS_VALID)
  1372. return -XFS_ERROR(EINVAL);
  1373. if (memchr_inv(&eofb.pad32, 0, sizeof(eofb.pad32)) ||
  1374. memchr_inv(eofb.pad64, 0, sizeof(eofb.pad64)))
  1375. return -XFS_ERROR(EINVAL);
  1376. error = xfs_icache_free_eofblocks(mp, &eofb);
  1377. return -error;
  1378. }
  1379. default:
  1380. return -ENOTTY;
  1381. }
  1382. }