ioctl.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. /*
  2. * ioctl.c - NILFS ioctl operations.
  3. *
  4. * Copyright (C) 2007, 2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Written by Koji Sato.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/wait.h>
  20. #include <linux/slab.h>
  21. #include <linux/capability.h> /* capable() */
  22. #include <linux/uaccess.h> /* copy_from_user(), copy_to_user() */
  23. #include <linux/vmalloc.h>
  24. #include <linux/compat.h> /* compat_ptr() */
  25. #include <linux/mount.h> /* mnt_want_write_file(), mnt_drop_write_file() */
  26. #include <linux/buffer_head.h>
  27. #include <linux/nilfs2_fs.h>
  28. #include "nilfs.h"
  29. #include "segment.h"
  30. #include "bmap.h"
  31. #include "cpfile.h"
  32. #include "sufile.h"
  33. #include "dat.h"
  34. /**
  35. * nilfs_ioctl_wrap_copy - wrapping function of get/set metadata info
  36. * @nilfs: nilfs object
  37. * @argv: vector of arguments from userspace
  38. * @dir: set of direction flags
  39. * @dofunc: concrete function of get/set metadata info
  40. *
  41. * Description: nilfs_ioctl_wrap_copy() gets/sets metadata info by means of
  42. * calling dofunc() function on the basis of @argv argument.
  43. *
  44. * Return Value: On success, 0 is returned and requested metadata info
  45. * is copied into userspace. On error, one of the following
  46. * negative error codes is returned.
  47. *
  48. * %-EINVAL - Invalid arguments from userspace.
  49. *
  50. * %-ENOMEM - Insufficient amount of memory available.
  51. *
  52. * %-EFAULT - Failure during execution of requested operation.
  53. */
  54. static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
  55. struct nilfs_argv *argv, int dir,
  56. ssize_t (*dofunc)(struct the_nilfs *,
  57. __u64 *, int,
  58. void *, size_t, size_t))
  59. {
  60. void *buf;
  61. void __user *base = (void __user *)(unsigned long)argv->v_base;
  62. size_t maxmembs, total, n;
  63. ssize_t nr;
  64. int ret, i;
  65. __u64 pos, ppos;
  66. if (argv->v_nmembs == 0)
  67. return 0;
  68. if (argv->v_size > PAGE_SIZE)
  69. return -EINVAL;
  70. /*
  71. * Reject pairs of a start item position (argv->v_index) and a
  72. * total count (argv->v_nmembs) which leads position 'pos' to
  73. * overflow by the increment at the end of the loop.
  74. */
  75. if (argv->v_index > ~(__u64)0 - argv->v_nmembs)
  76. return -EINVAL;
  77. buf = (void *)__get_free_pages(GFP_NOFS, 0);
  78. if (unlikely(!buf))
  79. return -ENOMEM;
  80. maxmembs = PAGE_SIZE / argv->v_size;
  81. ret = 0;
  82. total = 0;
  83. pos = argv->v_index;
  84. for (i = 0; i < argv->v_nmembs; i += n) {
  85. n = (argv->v_nmembs - i < maxmembs) ?
  86. argv->v_nmembs - i : maxmembs;
  87. if ((dir & _IOC_WRITE) &&
  88. copy_from_user(buf, base + argv->v_size * i,
  89. argv->v_size * n)) {
  90. ret = -EFAULT;
  91. break;
  92. }
  93. ppos = pos;
  94. nr = dofunc(nilfs, &pos, argv->v_flags, buf, argv->v_size,
  95. n);
  96. if (nr < 0) {
  97. ret = nr;
  98. break;
  99. }
  100. if ((dir & _IOC_READ) &&
  101. copy_to_user(base + argv->v_size * i, buf,
  102. argv->v_size * nr)) {
  103. ret = -EFAULT;
  104. break;
  105. }
  106. total += nr;
  107. if ((size_t)nr < n)
  108. break;
  109. if (pos == ppos)
  110. pos += n;
  111. }
  112. argv->v_nmembs = total;
  113. free_pages((unsigned long)buf, 0);
  114. return ret;
  115. }
  116. /**
  117. * nilfs_ioctl_getflags - ioctl to support lsattr
  118. */
  119. static int nilfs_ioctl_getflags(struct inode *inode, void __user *argp)
  120. {
  121. unsigned int flags = NILFS_I(inode)->i_flags & FS_FL_USER_VISIBLE;
  122. return put_user(flags, (int __user *)argp);
  123. }
  124. /**
  125. * nilfs_ioctl_setflags - ioctl to support chattr
  126. */
  127. static int nilfs_ioctl_setflags(struct inode *inode, struct file *filp,
  128. void __user *argp)
  129. {
  130. struct nilfs_transaction_info ti;
  131. unsigned int flags, oldflags;
  132. int ret;
  133. if (!inode_owner_or_capable(inode))
  134. return -EACCES;
  135. if (get_user(flags, (int __user *)argp))
  136. return -EFAULT;
  137. ret = mnt_want_write_file(filp);
  138. if (ret)
  139. return ret;
  140. flags = nilfs_mask_flags(inode->i_mode, flags);
  141. inode_lock(inode);
  142. oldflags = NILFS_I(inode)->i_flags;
  143. /*
  144. * The IMMUTABLE and APPEND_ONLY flags can only be changed by the
  145. * relevant capability.
  146. */
  147. ret = -EPERM;
  148. if (((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) &&
  149. !capable(CAP_LINUX_IMMUTABLE))
  150. goto out;
  151. ret = nilfs_transaction_begin(inode->i_sb, &ti, 0);
  152. if (ret)
  153. goto out;
  154. NILFS_I(inode)->i_flags = (oldflags & ~FS_FL_USER_MODIFIABLE) |
  155. (flags & FS_FL_USER_MODIFIABLE);
  156. nilfs_set_inode_flags(inode);
  157. inode->i_ctime = CURRENT_TIME;
  158. if (IS_SYNC(inode))
  159. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  160. nilfs_mark_inode_dirty(inode);
  161. ret = nilfs_transaction_commit(inode->i_sb);
  162. out:
  163. inode_unlock(inode);
  164. mnt_drop_write_file(filp);
  165. return ret;
  166. }
  167. /**
  168. * nilfs_ioctl_getversion - get info about a file's version (generation number)
  169. */
  170. static int nilfs_ioctl_getversion(struct inode *inode, void __user *argp)
  171. {
  172. return put_user(inode->i_generation, (int __user *)argp);
  173. }
  174. /**
  175. * nilfs_ioctl_change_cpmode - change checkpoint mode (checkpoint/snapshot)
  176. * @inode: inode object
  177. * @filp: file object
  178. * @cmd: ioctl's request code
  179. * @argp: pointer on argument from userspace
  180. *
  181. * Description: nilfs_ioctl_change_cpmode() function changes mode of
  182. * given checkpoint between checkpoint and snapshot state. This ioctl
  183. * is used in chcp and mkcp utilities.
  184. *
  185. * Return Value: On success, 0 is returned and mode of a checkpoint is
  186. * changed. On error, one of the following negative error codes
  187. * is returned.
  188. *
  189. * %-EPERM - Operation not permitted.
  190. *
  191. * %-EFAULT - Failure during checkpoint mode changing.
  192. */
  193. static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
  194. unsigned int cmd, void __user *argp)
  195. {
  196. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  197. struct nilfs_transaction_info ti;
  198. struct nilfs_cpmode cpmode;
  199. int ret;
  200. if (!capable(CAP_SYS_ADMIN))
  201. return -EPERM;
  202. ret = mnt_want_write_file(filp);
  203. if (ret)
  204. return ret;
  205. ret = -EFAULT;
  206. if (copy_from_user(&cpmode, argp, sizeof(cpmode)))
  207. goto out;
  208. mutex_lock(&nilfs->ns_snapshot_mount_mutex);
  209. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  210. ret = nilfs_cpfile_change_cpmode(
  211. nilfs->ns_cpfile, cpmode.cm_cno, cpmode.cm_mode);
  212. if (unlikely(ret < 0))
  213. nilfs_transaction_abort(inode->i_sb);
  214. else
  215. nilfs_transaction_commit(inode->i_sb); /* never fails */
  216. mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
  217. out:
  218. mnt_drop_write_file(filp);
  219. return ret;
  220. }
  221. /**
  222. * nilfs_ioctl_delete_checkpoint - remove checkpoint
  223. * @inode: inode object
  224. * @filp: file object
  225. * @cmd: ioctl's request code
  226. * @argp: pointer on argument from userspace
  227. *
  228. * Description: nilfs_ioctl_delete_checkpoint() function removes
  229. * checkpoint from NILFS2 file system. This ioctl is used in rmcp
  230. * utility.
  231. *
  232. * Return Value: On success, 0 is returned and a checkpoint is
  233. * removed. On error, one of the following negative error codes
  234. * is returned.
  235. *
  236. * %-EPERM - Operation not permitted.
  237. *
  238. * %-EFAULT - Failure during checkpoint removing.
  239. */
  240. static int
  241. nilfs_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
  242. unsigned int cmd, void __user *argp)
  243. {
  244. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  245. struct nilfs_transaction_info ti;
  246. __u64 cno;
  247. int ret;
  248. if (!capable(CAP_SYS_ADMIN))
  249. return -EPERM;
  250. ret = mnt_want_write_file(filp);
  251. if (ret)
  252. return ret;
  253. ret = -EFAULT;
  254. if (copy_from_user(&cno, argp, sizeof(cno)))
  255. goto out;
  256. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  257. ret = nilfs_cpfile_delete_checkpoint(nilfs->ns_cpfile, cno);
  258. if (unlikely(ret < 0))
  259. nilfs_transaction_abort(inode->i_sb);
  260. else
  261. nilfs_transaction_commit(inode->i_sb); /* never fails */
  262. out:
  263. mnt_drop_write_file(filp);
  264. return ret;
  265. }
  266. /**
  267. * nilfs_ioctl_do_get_cpinfo - callback method getting info about checkpoints
  268. * @nilfs: nilfs object
  269. * @posp: pointer on array of checkpoint's numbers
  270. * @flags: checkpoint mode (checkpoint or snapshot)
  271. * @buf: buffer for storing checkponts' info
  272. * @size: size in bytes of one checkpoint info item in array
  273. * @nmembs: number of checkpoints in array (numbers and infos)
  274. *
  275. * Description: nilfs_ioctl_do_get_cpinfo() function returns info about
  276. * requested checkpoints. The NILFS_IOCTL_GET_CPINFO ioctl is used in
  277. * lscp utility and by nilfs_cleanerd daemon.
  278. *
  279. * Return value: count of nilfs_cpinfo structures in output buffer.
  280. */
  281. static ssize_t
  282. nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
  283. void *buf, size_t size, size_t nmembs)
  284. {
  285. int ret;
  286. down_read(&nilfs->ns_segctor_sem);
  287. ret = nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
  288. size, nmembs);
  289. up_read(&nilfs->ns_segctor_sem);
  290. return ret;
  291. }
  292. /**
  293. * nilfs_ioctl_get_cpstat - get checkpoints statistics
  294. * @inode: inode object
  295. * @filp: file object
  296. * @cmd: ioctl's request code
  297. * @argp: pointer on argument from userspace
  298. *
  299. * Description: nilfs_ioctl_get_cpstat() returns information about checkpoints.
  300. * The NILFS_IOCTL_GET_CPSTAT ioctl is used by lscp, rmcp utilities
  301. * and by nilfs_cleanerd daemon.
  302. *
  303. * Return Value: On success, 0 is returned, and checkpoints information is
  304. * copied into userspace pointer @argp. On error, one of the following
  305. * negative error codes is returned.
  306. *
  307. * %-EIO - I/O error.
  308. *
  309. * %-ENOMEM - Insufficient amount of memory available.
  310. *
  311. * %-EFAULT - Failure during getting checkpoints statistics.
  312. */
  313. static int nilfs_ioctl_get_cpstat(struct inode *inode, struct file *filp,
  314. unsigned int cmd, void __user *argp)
  315. {
  316. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  317. struct nilfs_cpstat cpstat;
  318. int ret;
  319. down_read(&nilfs->ns_segctor_sem);
  320. ret = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
  321. up_read(&nilfs->ns_segctor_sem);
  322. if (ret < 0)
  323. return ret;
  324. if (copy_to_user(argp, &cpstat, sizeof(cpstat)))
  325. ret = -EFAULT;
  326. return ret;
  327. }
  328. /**
  329. * nilfs_ioctl_do_get_suinfo - callback method getting segment usage info
  330. * @nilfs: nilfs object
  331. * @posp: pointer on array of segment numbers
  332. * @flags: *not used*
  333. * @buf: buffer for storing suinfo array
  334. * @size: size in bytes of one suinfo item in array
  335. * @nmembs: count of segment numbers and suinfos in array
  336. *
  337. * Description: nilfs_ioctl_do_get_suinfo() function returns segment usage
  338. * info about requested segments. The NILFS_IOCTL_GET_SUINFO ioctl is used
  339. * in lssu, nilfs_resize utilities and by nilfs_cleanerd daemon.
  340. *
  341. * Return value: count of nilfs_suinfo structures in output buffer.
  342. */
  343. static ssize_t
  344. nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
  345. void *buf, size_t size, size_t nmembs)
  346. {
  347. int ret;
  348. down_read(&nilfs->ns_segctor_sem);
  349. ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, size,
  350. nmembs);
  351. up_read(&nilfs->ns_segctor_sem);
  352. return ret;
  353. }
  354. /**
  355. * nilfs_ioctl_get_sustat - get segment usage statistics
  356. * @inode: inode object
  357. * @filp: file object
  358. * @cmd: ioctl's request code
  359. * @argp: pointer on argument from userspace
  360. *
  361. * Description: nilfs_ioctl_get_sustat() returns segment usage statistics.
  362. * The NILFS_IOCTL_GET_SUSTAT ioctl is used in lssu, nilfs_resize utilities
  363. * and by nilfs_cleanerd daemon.
  364. *
  365. * Return Value: On success, 0 is returned, and segment usage information is
  366. * copied into userspace pointer @argp. On error, one of the following
  367. * negative error codes is returned.
  368. *
  369. * %-EIO - I/O error.
  370. *
  371. * %-ENOMEM - Insufficient amount of memory available.
  372. *
  373. * %-EFAULT - Failure during getting segment usage statistics.
  374. */
  375. static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp,
  376. unsigned int cmd, void __user *argp)
  377. {
  378. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  379. struct nilfs_sustat sustat;
  380. int ret;
  381. down_read(&nilfs->ns_segctor_sem);
  382. ret = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
  383. up_read(&nilfs->ns_segctor_sem);
  384. if (ret < 0)
  385. return ret;
  386. if (copy_to_user(argp, &sustat, sizeof(sustat)))
  387. ret = -EFAULT;
  388. return ret;
  389. }
  390. /**
  391. * nilfs_ioctl_do_get_vinfo - callback method getting virtual blocks info
  392. * @nilfs: nilfs object
  393. * @posp: *not used*
  394. * @flags: *not used*
  395. * @buf: buffer for storing array of nilfs_vinfo structures
  396. * @size: size in bytes of one vinfo item in array
  397. * @nmembs: count of vinfos in array
  398. *
  399. * Description: nilfs_ioctl_do_get_vinfo() function returns information
  400. * on virtual block addresses. The NILFS_IOCTL_GET_VINFO ioctl is used
  401. * by nilfs_cleanerd daemon.
  402. *
  403. * Return value: count of nilfs_vinfo structures in output buffer.
  404. */
  405. static ssize_t
  406. nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
  407. void *buf, size_t size, size_t nmembs)
  408. {
  409. int ret;
  410. down_read(&nilfs->ns_segctor_sem);
  411. ret = nilfs_dat_get_vinfo(nilfs->ns_dat, buf, size, nmembs);
  412. up_read(&nilfs->ns_segctor_sem);
  413. return ret;
  414. }
  415. /**
  416. * nilfs_ioctl_do_get_bdescs - callback method getting disk block descriptors
  417. * @nilfs: nilfs object
  418. * @posp: *not used*
  419. * @flags: *not used*
  420. * @buf: buffer for storing array of nilfs_bdesc structures
  421. * @size: size in bytes of one bdesc item in array
  422. * @nmembs: count of bdescs in array
  423. *
  424. * Description: nilfs_ioctl_do_get_bdescs() function returns information
  425. * about descriptors of disk block numbers. The NILFS_IOCTL_GET_BDESCS ioctl
  426. * is used by nilfs_cleanerd daemon.
  427. *
  428. * Return value: count of nilfs_bdescs structures in output buffer.
  429. */
  430. static ssize_t
  431. nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, __u64 *posp, int flags,
  432. void *buf, size_t size, size_t nmembs)
  433. {
  434. struct nilfs_bmap *bmap = NILFS_I(nilfs->ns_dat)->i_bmap;
  435. struct nilfs_bdesc *bdescs = buf;
  436. int ret, i;
  437. down_read(&nilfs->ns_segctor_sem);
  438. for (i = 0; i < nmembs; i++) {
  439. ret = nilfs_bmap_lookup_at_level(bmap,
  440. bdescs[i].bd_offset,
  441. bdescs[i].bd_level + 1,
  442. &bdescs[i].bd_blocknr);
  443. if (ret < 0) {
  444. if (ret != -ENOENT) {
  445. up_read(&nilfs->ns_segctor_sem);
  446. return ret;
  447. }
  448. bdescs[i].bd_blocknr = 0;
  449. }
  450. }
  451. up_read(&nilfs->ns_segctor_sem);
  452. return nmembs;
  453. }
  454. /**
  455. * nilfs_ioctl_get_bdescs - get disk block descriptors
  456. * @inode: inode object
  457. * @filp: file object
  458. * @cmd: ioctl's request code
  459. * @argp: pointer on argument from userspace
  460. *
  461. * Description: nilfs_ioctl_do_get_bdescs() function returns information
  462. * about descriptors of disk block numbers. The NILFS_IOCTL_GET_BDESCS ioctl
  463. * is used by nilfs_cleanerd daemon.
  464. *
  465. * Return Value: On success, 0 is returned, and disk block descriptors are
  466. * copied into userspace pointer @argp. On error, one of the following
  467. * negative error codes is returned.
  468. *
  469. * %-EINVAL - Invalid arguments from userspace.
  470. *
  471. * %-EIO - I/O error.
  472. *
  473. * %-ENOMEM - Insufficient amount of memory available.
  474. *
  475. * %-EFAULT - Failure during getting disk block descriptors.
  476. */
  477. static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
  478. unsigned int cmd, void __user *argp)
  479. {
  480. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  481. struct nilfs_argv argv;
  482. int ret;
  483. if (copy_from_user(&argv, argp, sizeof(argv)))
  484. return -EFAULT;
  485. if (argv.v_size != sizeof(struct nilfs_bdesc))
  486. return -EINVAL;
  487. ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
  488. nilfs_ioctl_do_get_bdescs);
  489. if (ret < 0)
  490. return ret;
  491. if (copy_to_user(argp, &argv, sizeof(argv)))
  492. ret = -EFAULT;
  493. return ret;
  494. }
  495. /**
  496. * nilfs_ioctl_move_inode_block - prepare data/node block for moving by GC
  497. * @inode: inode object
  498. * @vdesc: descriptor of virtual block number
  499. * @buffers: list of moving buffers
  500. *
  501. * Description: nilfs_ioctl_move_inode_block() function registers data/node
  502. * buffer in the GC pagecache and submit read request.
  503. *
  504. * Return Value: On success, 0 is returned. On error, one of the following
  505. * negative error codes is returned.
  506. *
  507. * %-EIO - I/O error.
  508. *
  509. * %-ENOMEM - Insufficient amount of memory available.
  510. *
  511. * %-ENOENT - Requested block doesn't exist.
  512. *
  513. * %-EEXIST - Blocks conflict is detected.
  514. */
  515. static int nilfs_ioctl_move_inode_block(struct inode *inode,
  516. struct nilfs_vdesc *vdesc,
  517. struct list_head *buffers)
  518. {
  519. struct buffer_head *bh;
  520. int ret;
  521. if (vdesc->vd_flags == 0)
  522. ret = nilfs_gccache_submit_read_data(
  523. inode, vdesc->vd_offset, vdesc->vd_blocknr,
  524. vdesc->vd_vblocknr, &bh);
  525. else
  526. ret = nilfs_gccache_submit_read_node(
  527. inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
  528. if (unlikely(ret < 0)) {
  529. if (ret == -ENOENT)
  530. printk(KERN_CRIT
  531. "%s: invalid virtual block address (%s): "
  532. "ino=%llu, cno=%llu, offset=%llu, "
  533. "blocknr=%llu, vblocknr=%llu\n",
  534. __func__, vdesc->vd_flags ? "node" : "data",
  535. (unsigned long long)vdesc->vd_ino,
  536. (unsigned long long)vdesc->vd_cno,
  537. (unsigned long long)vdesc->vd_offset,
  538. (unsigned long long)vdesc->vd_blocknr,
  539. (unsigned long long)vdesc->vd_vblocknr);
  540. return ret;
  541. }
  542. if (unlikely(!list_empty(&bh->b_assoc_buffers))) {
  543. printk(KERN_CRIT "%s: conflicting %s buffer: ino=%llu, "
  544. "cno=%llu, offset=%llu, blocknr=%llu, vblocknr=%llu\n",
  545. __func__, vdesc->vd_flags ? "node" : "data",
  546. (unsigned long long)vdesc->vd_ino,
  547. (unsigned long long)vdesc->vd_cno,
  548. (unsigned long long)vdesc->vd_offset,
  549. (unsigned long long)vdesc->vd_blocknr,
  550. (unsigned long long)vdesc->vd_vblocknr);
  551. brelse(bh);
  552. return -EEXIST;
  553. }
  554. list_add_tail(&bh->b_assoc_buffers, buffers);
  555. return 0;
  556. }
  557. /**
  558. * nilfs_ioctl_move_blocks - move valid inode's blocks during garbage collection
  559. * @sb: superblock object
  560. * @argv: vector of arguments from userspace
  561. * @buf: array of nilfs_vdesc structures
  562. *
  563. * Description: nilfs_ioctl_move_blocks() function reads valid data/node
  564. * blocks that garbage collector specified with the array of nilfs_vdesc
  565. * structures and stores them into page caches of GC inodes.
  566. *
  567. * Return Value: Number of processed nilfs_vdesc structures or
  568. * error code, otherwise.
  569. */
  570. static int nilfs_ioctl_move_blocks(struct super_block *sb,
  571. struct nilfs_argv *argv, void *buf)
  572. {
  573. size_t nmembs = argv->v_nmembs;
  574. struct the_nilfs *nilfs = sb->s_fs_info;
  575. struct inode *inode;
  576. struct nilfs_vdesc *vdesc;
  577. struct buffer_head *bh, *n;
  578. LIST_HEAD(buffers);
  579. ino_t ino;
  580. __u64 cno;
  581. int i, ret;
  582. for (i = 0, vdesc = buf; i < nmembs; ) {
  583. ino = vdesc->vd_ino;
  584. cno = vdesc->vd_cno;
  585. inode = nilfs_iget_for_gc(sb, ino, cno);
  586. if (IS_ERR(inode)) {
  587. ret = PTR_ERR(inode);
  588. goto failed;
  589. }
  590. if (list_empty(&NILFS_I(inode)->i_dirty)) {
  591. /*
  592. * Add the inode to GC inode list. Garbage Collection
  593. * is serialized and no two processes manipulate the
  594. * list simultaneously.
  595. */
  596. igrab(inode);
  597. list_add(&NILFS_I(inode)->i_dirty,
  598. &nilfs->ns_gc_inodes);
  599. }
  600. do {
  601. ret = nilfs_ioctl_move_inode_block(inode, vdesc,
  602. &buffers);
  603. if (unlikely(ret < 0)) {
  604. iput(inode);
  605. goto failed;
  606. }
  607. vdesc++;
  608. } while (++i < nmembs &&
  609. vdesc->vd_ino == ino && vdesc->vd_cno == cno);
  610. iput(inode); /* The inode still remains in GC inode list */
  611. }
  612. list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
  613. ret = nilfs_gccache_wait_and_mark_dirty(bh);
  614. if (unlikely(ret < 0)) {
  615. WARN_ON(ret == -EEXIST);
  616. goto failed;
  617. }
  618. list_del_init(&bh->b_assoc_buffers);
  619. brelse(bh);
  620. }
  621. return nmembs;
  622. failed:
  623. list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
  624. list_del_init(&bh->b_assoc_buffers);
  625. brelse(bh);
  626. }
  627. return ret;
  628. }
  629. /**
  630. * nilfs_ioctl_delete_checkpoints - delete checkpoints
  631. * @nilfs: nilfs object
  632. * @argv: vector of arguments from userspace
  633. * @buf: array of periods of checkpoints numbers
  634. *
  635. * Description: nilfs_ioctl_delete_checkpoints() function deletes checkpoints
  636. * in the period from p_start to p_end, excluding p_end itself. The checkpoints
  637. * which have been already deleted are ignored.
  638. *
  639. * Return Value: Number of processed nilfs_period structures or
  640. * error code, otherwise.
  641. *
  642. * %-EIO - I/O error.
  643. *
  644. * %-ENOMEM - Insufficient amount of memory available.
  645. *
  646. * %-EINVAL - invalid checkpoints.
  647. */
  648. static int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
  649. struct nilfs_argv *argv, void *buf)
  650. {
  651. size_t nmembs = argv->v_nmembs;
  652. struct inode *cpfile = nilfs->ns_cpfile;
  653. struct nilfs_period *periods = buf;
  654. int ret, i;
  655. for (i = 0; i < nmembs; i++) {
  656. ret = nilfs_cpfile_delete_checkpoints(
  657. cpfile, periods[i].p_start, periods[i].p_end);
  658. if (ret < 0)
  659. return ret;
  660. }
  661. return nmembs;
  662. }
  663. /**
  664. * nilfs_ioctl_free_vblocknrs - free virtual block numbers
  665. * @nilfs: nilfs object
  666. * @argv: vector of arguments from userspace
  667. * @buf: array of virtual block numbers
  668. *
  669. * Description: nilfs_ioctl_free_vblocknrs() function frees
  670. * the virtual block numbers specified by @buf and @argv->v_nmembs.
  671. *
  672. * Return Value: Number of processed virtual block numbers or
  673. * error code, otherwise.
  674. *
  675. * %-EIO - I/O error.
  676. *
  677. * %-ENOMEM - Insufficient amount of memory available.
  678. *
  679. * %-ENOENT - The virtual block number have not been allocated.
  680. */
  681. static int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
  682. struct nilfs_argv *argv, void *buf)
  683. {
  684. size_t nmembs = argv->v_nmembs;
  685. int ret;
  686. ret = nilfs_dat_freev(nilfs->ns_dat, buf, nmembs);
  687. return (ret < 0) ? ret : nmembs;
  688. }
  689. /**
  690. * nilfs_ioctl_mark_blocks_dirty - mark blocks dirty
  691. * @nilfs: nilfs object
  692. * @argv: vector of arguments from userspace
  693. * @buf: array of block descriptors
  694. *
  695. * Description: nilfs_ioctl_mark_blocks_dirty() function marks
  696. * metadata file or data blocks as dirty.
  697. *
  698. * Return Value: Number of processed block descriptors or
  699. * error code, otherwise.
  700. *
  701. * %-ENOMEM - Insufficient memory available.
  702. *
  703. * %-EIO - I/O error
  704. *
  705. * %-ENOENT - the specified block does not exist (hole block)
  706. */
  707. static int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
  708. struct nilfs_argv *argv, void *buf)
  709. {
  710. size_t nmembs = argv->v_nmembs;
  711. struct nilfs_bmap *bmap = NILFS_I(nilfs->ns_dat)->i_bmap;
  712. struct nilfs_bdesc *bdescs = buf;
  713. struct buffer_head *bh;
  714. int ret, i;
  715. for (i = 0; i < nmembs; i++) {
  716. /* XXX: use macro or inline func to check liveness */
  717. ret = nilfs_bmap_lookup_at_level(bmap,
  718. bdescs[i].bd_offset,
  719. bdescs[i].bd_level + 1,
  720. &bdescs[i].bd_blocknr);
  721. if (ret < 0) {
  722. if (ret != -ENOENT)
  723. return ret;
  724. bdescs[i].bd_blocknr = 0;
  725. }
  726. if (bdescs[i].bd_blocknr != bdescs[i].bd_oblocknr)
  727. /* skip dead block */
  728. continue;
  729. if (bdescs[i].bd_level == 0) {
  730. ret = nilfs_mdt_get_block(nilfs->ns_dat,
  731. bdescs[i].bd_offset,
  732. false, NULL, &bh);
  733. if (unlikely(ret)) {
  734. WARN_ON(ret == -ENOENT);
  735. return ret;
  736. }
  737. mark_buffer_dirty(bh);
  738. nilfs_mdt_mark_dirty(nilfs->ns_dat);
  739. put_bh(bh);
  740. } else {
  741. ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
  742. bdescs[i].bd_level);
  743. if (ret < 0) {
  744. WARN_ON(ret == -ENOENT);
  745. return ret;
  746. }
  747. }
  748. }
  749. return nmembs;
  750. }
  751. int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
  752. struct nilfs_argv *argv, void **kbufs)
  753. {
  754. const char *msg;
  755. int ret;
  756. ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], kbufs[1]);
  757. if (ret < 0) {
  758. /*
  759. * can safely abort because checkpoints can be removed
  760. * independently.
  761. */
  762. msg = "cannot delete checkpoints";
  763. goto failed;
  764. }
  765. ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], kbufs[2]);
  766. if (ret < 0) {
  767. /*
  768. * can safely abort because DAT file is updated atomically
  769. * using a copy-on-write technique.
  770. */
  771. msg = "cannot delete virtual blocks from DAT file";
  772. goto failed;
  773. }
  774. ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], kbufs[3]);
  775. if (ret < 0) {
  776. /*
  777. * can safely abort because the operation is nondestructive.
  778. */
  779. msg = "cannot mark copying blocks dirty";
  780. goto failed;
  781. }
  782. return 0;
  783. failed:
  784. printk(KERN_ERR "NILFS: GC failed during preparation: %s: err=%d\n",
  785. msg, ret);
  786. return ret;
  787. }
  788. /**
  789. * nilfs_ioctl_clean_segments - clean segments
  790. * @inode: inode object
  791. * @filp: file object
  792. * @cmd: ioctl's request code
  793. * @argp: pointer on argument from userspace
  794. *
  795. * Description: nilfs_ioctl_clean_segments() function makes garbage
  796. * collection operation in the environment of requested parameters
  797. * from userspace. The NILFS_IOCTL_CLEAN_SEGMENTS ioctl is used by
  798. * nilfs_cleanerd daemon.
  799. *
  800. * Return Value: On success, 0 is returned or error code, otherwise.
  801. */
  802. static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
  803. unsigned int cmd, void __user *argp)
  804. {
  805. struct nilfs_argv argv[5];
  806. static const size_t argsz[5] = {
  807. sizeof(struct nilfs_vdesc),
  808. sizeof(struct nilfs_period),
  809. sizeof(__u64),
  810. sizeof(struct nilfs_bdesc),
  811. sizeof(__u64),
  812. };
  813. void __user *base;
  814. void *kbufs[5];
  815. struct the_nilfs *nilfs;
  816. size_t len, nsegs;
  817. int n, ret;
  818. if (!capable(CAP_SYS_ADMIN))
  819. return -EPERM;
  820. ret = mnt_want_write_file(filp);
  821. if (ret)
  822. return ret;
  823. ret = -EFAULT;
  824. if (copy_from_user(argv, argp, sizeof(argv)))
  825. goto out;
  826. ret = -EINVAL;
  827. nsegs = argv[4].v_nmembs;
  828. if (argv[4].v_size != argsz[4])
  829. goto out;
  830. if (nsegs > UINT_MAX / sizeof(__u64))
  831. goto out;
  832. /*
  833. * argv[4] points to segment numbers this ioctl cleans. We
  834. * use kmalloc() for its buffer because memory used for the
  835. * segment numbers is enough small.
  836. */
  837. kbufs[4] = memdup_user((void __user *)(unsigned long)argv[4].v_base,
  838. nsegs * sizeof(__u64));
  839. if (IS_ERR(kbufs[4])) {
  840. ret = PTR_ERR(kbufs[4]);
  841. goto out;
  842. }
  843. nilfs = inode->i_sb->s_fs_info;
  844. for (n = 0; n < 4; n++) {
  845. ret = -EINVAL;
  846. if (argv[n].v_size != argsz[n])
  847. goto out_free;
  848. if (argv[n].v_nmembs > nsegs * nilfs->ns_blocks_per_segment)
  849. goto out_free;
  850. if (argv[n].v_nmembs >= UINT_MAX / argv[n].v_size)
  851. goto out_free;
  852. len = argv[n].v_size * argv[n].v_nmembs;
  853. base = (void __user *)(unsigned long)argv[n].v_base;
  854. if (len == 0) {
  855. kbufs[n] = NULL;
  856. continue;
  857. }
  858. kbufs[n] = vmalloc(len);
  859. if (!kbufs[n]) {
  860. ret = -ENOMEM;
  861. goto out_free;
  862. }
  863. if (copy_from_user(kbufs[n], base, len)) {
  864. ret = -EFAULT;
  865. vfree(kbufs[n]);
  866. goto out_free;
  867. }
  868. }
  869. /*
  870. * nilfs_ioctl_move_blocks() will call nilfs_iget_for_gc(),
  871. * which will operates an inode list without blocking.
  872. * To protect the list from concurrent operations,
  873. * nilfs_ioctl_move_blocks should be atomic operation.
  874. */
  875. if (test_and_set_bit(THE_NILFS_GC_RUNNING, &nilfs->ns_flags)) {
  876. ret = -EBUSY;
  877. goto out_free;
  878. }
  879. ret = nilfs_ioctl_move_blocks(inode->i_sb, &argv[0], kbufs[0]);
  880. if (ret < 0)
  881. printk(KERN_ERR "NILFS: GC failed during preparation: "
  882. "cannot read source blocks: err=%d\n", ret);
  883. else {
  884. if (nilfs_sb_need_update(nilfs))
  885. set_nilfs_discontinued(nilfs);
  886. ret = nilfs_clean_segments(inode->i_sb, argv, kbufs);
  887. }
  888. nilfs_remove_all_gcinodes(nilfs);
  889. clear_nilfs_gc_running(nilfs);
  890. out_free:
  891. while (--n >= 0)
  892. vfree(kbufs[n]);
  893. kfree(kbufs[4]);
  894. out:
  895. mnt_drop_write_file(filp);
  896. return ret;
  897. }
  898. /**
  899. * nilfs_ioctl_sync - make a checkpoint
  900. * @inode: inode object
  901. * @filp: file object
  902. * @cmd: ioctl's request code
  903. * @argp: pointer on argument from userspace
  904. *
  905. * Description: nilfs_ioctl_sync() function constructs a logical segment
  906. * for checkpointing. This function guarantees that all modified data
  907. * and metadata are written out to the device when it successfully
  908. * returned.
  909. *
  910. * Return Value: On success, 0 is retured. On errors, one of the following
  911. * negative error code is returned.
  912. *
  913. * %-EROFS - Read only filesystem.
  914. *
  915. * %-EIO - I/O error
  916. *
  917. * %-ENOSPC - No space left on device (only in a panic state).
  918. *
  919. * %-ERESTARTSYS - Interrupted.
  920. *
  921. * %-ENOMEM - Insufficient memory available.
  922. *
  923. * %-EFAULT - Failure during execution of requested operation.
  924. */
  925. static int nilfs_ioctl_sync(struct inode *inode, struct file *filp,
  926. unsigned int cmd, void __user *argp)
  927. {
  928. __u64 cno;
  929. int ret;
  930. struct the_nilfs *nilfs;
  931. ret = nilfs_construct_segment(inode->i_sb);
  932. if (ret < 0)
  933. return ret;
  934. nilfs = inode->i_sb->s_fs_info;
  935. ret = nilfs_flush_device(nilfs);
  936. if (ret < 0)
  937. return ret;
  938. if (argp != NULL) {
  939. down_read(&nilfs->ns_segctor_sem);
  940. cno = nilfs->ns_cno - 1;
  941. up_read(&nilfs->ns_segctor_sem);
  942. if (copy_to_user(argp, &cno, sizeof(cno)))
  943. return -EFAULT;
  944. }
  945. return 0;
  946. }
  947. /**
  948. * nilfs_ioctl_resize - resize NILFS2 volume
  949. * @inode: inode object
  950. * @filp: file object
  951. * @argp: pointer on argument from userspace
  952. *
  953. * Return Value: On success, 0 is returned or error code, otherwise.
  954. */
  955. static int nilfs_ioctl_resize(struct inode *inode, struct file *filp,
  956. void __user *argp)
  957. {
  958. __u64 newsize;
  959. int ret = -EPERM;
  960. if (!capable(CAP_SYS_ADMIN))
  961. goto out;
  962. ret = mnt_want_write_file(filp);
  963. if (ret)
  964. goto out;
  965. ret = -EFAULT;
  966. if (copy_from_user(&newsize, argp, sizeof(newsize)))
  967. goto out_drop_write;
  968. ret = nilfs_resize_fs(inode->i_sb, newsize);
  969. out_drop_write:
  970. mnt_drop_write_file(filp);
  971. out:
  972. return ret;
  973. }
  974. /**
  975. * nilfs_ioctl_trim_fs() - trim ioctl handle function
  976. * @inode: inode object
  977. * @argp: pointer on argument from userspace
  978. *
  979. * Decription: nilfs_ioctl_trim_fs is the FITRIM ioctl handle function. It
  980. * checks the arguments from userspace and calls nilfs_sufile_trim_fs, which
  981. * performs the actual trim operation.
  982. *
  983. * Return Value: On success, 0 is returned or negative error code, otherwise.
  984. */
  985. static int nilfs_ioctl_trim_fs(struct inode *inode, void __user *argp)
  986. {
  987. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  988. struct request_queue *q = bdev_get_queue(nilfs->ns_bdev);
  989. struct fstrim_range range;
  990. int ret;
  991. if (!capable(CAP_SYS_ADMIN))
  992. return -EPERM;
  993. if (!blk_queue_discard(q))
  994. return -EOPNOTSUPP;
  995. if (copy_from_user(&range, argp, sizeof(range)))
  996. return -EFAULT;
  997. range.minlen = max_t(u64, range.minlen, q->limits.discard_granularity);
  998. down_read(&nilfs->ns_segctor_sem);
  999. ret = nilfs_sufile_trim_fs(nilfs->ns_sufile, &range);
  1000. up_read(&nilfs->ns_segctor_sem);
  1001. if (ret < 0)
  1002. return ret;
  1003. if (copy_to_user(argp, &range, sizeof(range)))
  1004. return -EFAULT;
  1005. return 0;
  1006. }
  1007. /**
  1008. * nilfs_ioctl_set_alloc_range - limit range of segments to be allocated
  1009. * @inode: inode object
  1010. * @argp: pointer on argument from userspace
  1011. *
  1012. * Decription: nilfs_ioctl_set_alloc_range() function defines lower limit
  1013. * of segments in bytes and upper limit of segments in bytes.
  1014. * The NILFS_IOCTL_SET_ALLOC_RANGE is used by nilfs_resize utility.
  1015. *
  1016. * Return Value: On success, 0 is returned or error code, otherwise.
  1017. */
  1018. static int nilfs_ioctl_set_alloc_range(struct inode *inode, void __user *argp)
  1019. {
  1020. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  1021. __u64 range[2];
  1022. __u64 minseg, maxseg;
  1023. unsigned long segbytes;
  1024. int ret = -EPERM;
  1025. if (!capable(CAP_SYS_ADMIN))
  1026. goto out;
  1027. ret = -EFAULT;
  1028. if (copy_from_user(range, argp, sizeof(__u64[2])))
  1029. goto out;
  1030. ret = -ERANGE;
  1031. if (range[1] > i_size_read(inode->i_sb->s_bdev->bd_inode))
  1032. goto out;
  1033. segbytes = nilfs->ns_blocks_per_segment * nilfs->ns_blocksize;
  1034. minseg = range[0] + segbytes - 1;
  1035. do_div(minseg, segbytes);
  1036. maxseg = NILFS_SB2_OFFSET_BYTES(range[1]);
  1037. do_div(maxseg, segbytes);
  1038. maxseg--;
  1039. ret = nilfs_sufile_set_alloc_range(nilfs->ns_sufile, minseg, maxseg);
  1040. out:
  1041. return ret;
  1042. }
  1043. /**
  1044. * nilfs_ioctl_get_info - wrapping function of get metadata info
  1045. * @inode: inode object
  1046. * @filp: file object
  1047. * @cmd: ioctl's request code
  1048. * @argp: pointer on argument from userspace
  1049. * @membsz: size of an item in bytes
  1050. * @dofunc: concrete function of getting metadata info
  1051. *
  1052. * Description: nilfs_ioctl_get_info() gets metadata info by means of
  1053. * calling dofunc() function.
  1054. *
  1055. * Return Value: On success, 0 is returned and requested metadata info
  1056. * is copied into userspace. On error, one of the following
  1057. * negative error codes is returned.
  1058. *
  1059. * %-EINVAL - Invalid arguments from userspace.
  1060. *
  1061. * %-ENOMEM - Insufficient amount of memory available.
  1062. *
  1063. * %-EFAULT - Failure during execution of requested operation.
  1064. */
  1065. static int nilfs_ioctl_get_info(struct inode *inode, struct file *filp,
  1066. unsigned int cmd, void __user *argp,
  1067. size_t membsz,
  1068. ssize_t (*dofunc)(struct the_nilfs *,
  1069. __u64 *, int,
  1070. void *, size_t, size_t))
  1071. {
  1072. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  1073. struct nilfs_argv argv;
  1074. int ret;
  1075. if (copy_from_user(&argv, argp, sizeof(argv)))
  1076. return -EFAULT;
  1077. if (argv.v_size < membsz)
  1078. return -EINVAL;
  1079. ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), dofunc);
  1080. if (ret < 0)
  1081. return ret;
  1082. if (copy_to_user(argp, &argv, sizeof(argv)))
  1083. ret = -EFAULT;
  1084. return ret;
  1085. }
  1086. /**
  1087. * nilfs_ioctl_set_suinfo - set segment usage info
  1088. * @inode: inode object
  1089. * @filp: file object
  1090. * @cmd: ioctl's request code
  1091. * @argp: pointer on argument from userspace
  1092. *
  1093. * Description: Expects an array of nilfs_suinfo_update structures
  1094. * encapsulated in nilfs_argv and updates the segment usage info
  1095. * according to the flags in nilfs_suinfo_update.
  1096. *
  1097. * Return Value: On success, 0 is returned. On error, one of the
  1098. * following negative error codes is returned.
  1099. *
  1100. * %-EPERM - Not enough permissions
  1101. *
  1102. * %-EFAULT - Error copying input data
  1103. *
  1104. * %-EIO - I/O error.
  1105. *
  1106. * %-ENOMEM - Insufficient amount of memory available.
  1107. *
  1108. * %-EINVAL - Invalid values in input (segment number, flags or nblocks)
  1109. */
  1110. static int nilfs_ioctl_set_suinfo(struct inode *inode, struct file *filp,
  1111. unsigned int cmd, void __user *argp)
  1112. {
  1113. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  1114. struct nilfs_transaction_info ti;
  1115. struct nilfs_argv argv;
  1116. size_t len;
  1117. void __user *base;
  1118. void *kbuf;
  1119. int ret;
  1120. if (!capable(CAP_SYS_ADMIN))
  1121. return -EPERM;
  1122. ret = mnt_want_write_file(filp);
  1123. if (ret)
  1124. return ret;
  1125. ret = -EFAULT;
  1126. if (copy_from_user(&argv, argp, sizeof(argv)))
  1127. goto out;
  1128. ret = -EINVAL;
  1129. if (argv.v_size < sizeof(struct nilfs_suinfo_update))
  1130. goto out;
  1131. if (argv.v_nmembs > nilfs->ns_nsegments)
  1132. goto out;
  1133. if (argv.v_nmembs >= UINT_MAX / argv.v_size)
  1134. goto out;
  1135. len = argv.v_size * argv.v_nmembs;
  1136. if (!len) {
  1137. ret = 0;
  1138. goto out;
  1139. }
  1140. base = (void __user *)(unsigned long)argv.v_base;
  1141. kbuf = vmalloc(len);
  1142. if (!kbuf) {
  1143. ret = -ENOMEM;
  1144. goto out;
  1145. }
  1146. if (copy_from_user(kbuf, base, len)) {
  1147. ret = -EFAULT;
  1148. goto out_free;
  1149. }
  1150. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  1151. ret = nilfs_sufile_set_suinfo(nilfs->ns_sufile, kbuf, argv.v_size,
  1152. argv.v_nmembs);
  1153. if (unlikely(ret < 0))
  1154. nilfs_transaction_abort(inode->i_sb);
  1155. else
  1156. nilfs_transaction_commit(inode->i_sb); /* never fails */
  1157. out_free:
  1158. vfree(kbuf);
  1159. out:
  1160. mnt_drop_write_file(filp);
  1161. return ret;
  1162. }
  1163. long nilfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1164. {
  1165. struct inode *inode = file_inode(filp);
  1166. void __user *argp = (void __user *)arg;
  1167. switch (cmd) {
  1168. case FS_IOC_GETFLAGS:
  1169. return nilfs_ioctl_getflags(inode, argp);
  1170. case FS_IOC_SETFLAGS:
  1171. return nilfs_ioctl_setflags(inode, filp, argp);
  1172. case FS_IOC_GETVERSION:
  1173. return nilfs_ioctl_getversion(inode, argp);
  1174. case NILFS_IOCTL_CHANGE_CPMODE:
  1175. return nilfs_ioctl_change_cpmode(inode, filp, cmd, argp);
  1176. case NILFS_IOCTL_DELETE_CHECKPOINT:
  1177. return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp);
  1178. case NILFS_IOCTL_GET_CPINFO:
  1179. return nilfs_ioctl_get_info(inode, filp, cmd, argp,
  1180. sizeof(struct nilfs_cpinfo),
  1181. nilfs_ioctl_do_get_cpinfo);
  1182. case NILFS_IOCTL_GET_CPSTAT:
  1183. return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp);
  1184. case NILFS_IOCTL_GET_SUINFO:
  1185. return nilfs_ioctl_get_info(inode, filp, cmd, argp,
  1186. sizeof(struct nilfs_suinfo),
  1187. nilfs_ioctl_do_get_suinfo);
  1188. case NILFS_IOCTL_SET_SUINFO:
  1189. return nilfs_ioctl_set_suinfo(inode, filp, cmd, argp);
  1190. case NILFS_IOCTL_GET_SUSTAT:
  1191. return nilfs_ioctl_get_sustat(inode, filp, cmd, argp);
  1192. case NILFS_IOCTL_GET_VINFO:
  1193. return nilfs_ioctl_get_info(inode, filp, cmd, argp,
  1194. sizeof(struct nilfs_vinfo),
  1195. nilfs_ioctl_do_get_vinfo);
  1196. case NILFS_IOCTL_GET_BDESCS:
  1197. return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp);
  1198. case NILFS_IOCTL_CLEAN_SEGMENTS:
  1199. return nilfs_ioctl_clean_segments(inode, filp, cmd, argp);
  1200. case NILFS_IOCTL_SYNC:
  1201. return nilfs_ioctl_sync(inode, filp, cmd, argp);
  1202. case NILFS_IOCTL_RESIZE:
  1203. return nilfs_ioctl_resize(inode, filp, argp);
  1204. case NILFS_IOCTL_SET_ALLOC_RANGE:
  1205. return nilfs_ioctl_set_alloc_range(inode, argp);
  1206. case FITRIM:
  1207. return nilfs_ioctl_trim_fs(inode, argp);
  1208. default:
  1209. return -ENOTTY;
  1210. }
  1211. }
  1212. #ifdef CONFIG_COMPAT
  1213. long nilfs_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1214. {
  1215. switch (cmd) {
  1216. case FS_IOC32_GETFLAGS:
  1217. cmd = FS_IOC_GETFLAGS;
  1218. break;
  1219. case FS_IOC32_SETFLAGS:
  1220. cmd = FS_IOC_SETFLAGS;
  1221. break;
  1222. case FS_IOC32_GETVERSION:
  1223. cmd = FS_IOC_GETVERSION;
  1224. break;
  1225. case NILFS_IOCTL_CHANGE_CPMODE:
  1226. case NILFS_IOCTL_DELETE_CHECKPOINT:
  1227. case NILFS_IOCTL_GET_CPINFO:
  1228. case NILFS_IOCTL_GET_CPSTAT:
  1229. case NILFS_IOCTL_GET_SUINFO:
  1230. case NILFS_IOCTL_SET_SUINFO:
  1231. case NILFS_IOCTL_GET_SUSTAT:
  1232. case NILFS_IOCTL_GET_VINFO:
  1233. case NILFS_IOCTL_GET_BDESCS:
  1234. case NILFS_IOCTL_CLEAN_SEGMENTS:
  1235. case NILFS_IOCTL_SYNC:
  1236. case NILFS_IOCTL_RESIZE:
  1237. case NILFS_IOCTL_SET_ALLOC_RANGE:
  1238. break;
  1239. default:
  1240. return -ENOIOCTLCMD;
  1241. }
  1242. return nilfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  1243. }
  1244. #endif