xfs_ioctl.c 38 KB

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