xfs_ioctl.c 35 KB

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