super.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Simple file system for zoned block devices exposing zones as files.
  4. *
  5. * Copyright (C) 2019 Western Digital Corporation or its affiliates.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/fs.h>
  9. #include <linux/magic.h>
  10. #include <linux/iomap.h>
  11. #include <linux/init.h>
  12. #include <linux/slab.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/statfs.h>
  15. #include <linux/writeback.h>
  16. #include <linux/quotaops.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/parser.h>
  19. #include <linux/uio.h>
  20. #include <linux/mman.h>
  21. #include <linux/sched/mm.h>
  22. #include <linux/crc32.h>
  23. #include <linux/task_io_accounting_ops.h>
  24. #include "zonefs.h"
  25. static inline int zonefs_zone_mgmt(struct inode *inode,
  26. enum req_opf op)
  27. {
  28. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  29. int ret;
  30. lockdep_assert_held(&zi->i_truncate_mutex);
  31. ret = blkdev_zone_mgmt(inode->i_sb->s_bdev, op, zi->i_zsector,
  32. zi->i_zone_size >> SECTOR_SHIFT, GFP_NOFS);
  33. if (ret) {
  34. zonefs_err(inode->i_sb,
  35. "Zone management operation %s at %llu failed %d\n",
  36. blk_op_str(op), zi->i_zsector, ret);
  37. return ret;
  38. }
  39. return 0;
  40. }
  41. static inline void zonefs_i_size_write(struct inode *inode, loff_t isize)
  42. {
  43. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  44. i_size_write(inode, isize);
  45. /*
  46. * A full zone is no longer open/active and does not need
  47. * explicit closing.
  48. */
  49. if (isize >= zi->i_max_size)
  50. zi->i_flags &= ~ZONEFS_ZONE_OPEN;
  51. }
  52. static int zonefs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
  53. unsigned int flags, struct iomap *iomap,
  54. struct iomap *srcmap)
  55. {
  56. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  57. struct super_block *sb = inode->i_sb;
  58. loff_t isize;
  59. /* All I/Os should always be within the file maximum size */
  60. if (WARN_ON_ONCE(offset + length > zi->i_max_size))
  61. return -EIO;
  62. /*
  63. * Sequential zones can only accept direct writes. This is already
  64. * checked when writes are issued, so warn if we see a page writeback
  65. * operation.
  66. */
  67. if (WARN_ON_ONCE(zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
  68. (flags & IOMAP_WRITE) && !(flags & IOMAP_DIRECT)))
  69. return -EIO;
  70. /*
  71. * For conventional zones, all blocks are always mapped. For sequential
  72. * zones, all blocks after always mapped below the inode size (zone
  73. * write pointer) and unwriten beyond.
  74. */
  75. mutex_lock(&zi->i_truncate_mutex);
  76. isize = i_size_read(inode);
  77. if (offset >= isize)
  78. iomap->type = IOMAP_UNWRITTEN;
  79. else
  80. iomap->type = IOMAP_MAPPED;
  81. if (flags & IOMAP_WRITE)
  82. length = zi->i_max_size - offset;
  83. else
  84. length = min(length, isize - offset);
  85. mutex_unlock(&zi->i_truncate_mutex);
  86. iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
  87. iomap->length = ALIGN(offset + length, sb->s_blocksize) - iomap->offset;
  88. iomap->bdev = inode->i_sb->s_bdev;
  89. iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
  90. return 0;
  91. }
  92. static const struct iomap_ops zonefs_iomap_ops = {
  93. .iomap_begin = zonefs_iomap_begin,
  94. };
  95. static int zonefs_readpage(struct file *unused, struct page *page)
  96. {
  97. return iomap_readpage(page, &zonefs_iomap_ops);
  98. }
  99. static void zonefs_readahead(struct readahead_control *rac)
  100. {
  101. iomap_readahead(rac, &zonefs_iomap_ops);
  102. }
  103. /*
  104. * Map blocks for page writeback. This is used only on conventional zone files,
  105. * which implies that the page range can only be within the fixed inode size.
  106. */
  107. static int zonefs_map_blocks(struct iomap_writepage_ctx *wpc,
  108. struct inode *inode, loff_t offset)
  109. {
  110. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  111. if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
  112. return -EIO;
  113. if (WARN_ON_ONCE(offset >= i_size_read(inode)))
  114. return -EIO;
  115. /* If the mapping is already OK, nothing needs to be done */
  116. if (offset >= wpc->iomap.offset &&
  117. offset < wpc->iomap.offset + wpc->iomap.length)
  118. return 0;
  119. return zonefs_iomap_begin(inode, offset, zi->i_max_size - offset,
  120. IOMAP_WRITE, &wpc->iomap, NULL);
  121. }
  122. static const struct iomap_writeback_ops zonefs_writeback_ops = {
  123. .map_blocks = zonefs_map_blocks,
  124. };
  125. static int zonefs_writepage(struct page *page, struct writeback_control *wbc)
  126. {
  127. struct iomap_writepage_ctx wpc = { };
  128. return iomap_writepage(page, wbc, &wpc, &zonefs_writeback_ops);
  129. }
  130. static int zonefs_writepages(struct address_space *mapping,
  131. struct writeback_control *wbc)
  132. {
  133. struct iomap_writepage_ctx wpc = { };
  134. return iomap_writepages(mapping, wbc, &wpc, &zonefs_writeback_ops);
  135. }
  136. static int zonefs_swap_activate(struct swap_info_struct *sis,
  137. struct file *swap_file, sector_t *span)
  138. {
  139. struct inode *inode = file_inode(swap_file);
  140. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  141. if (zi->i_ztype != ZONEFS_ZTYPE_CNV) {
  142. zonefs_err(inode->i_sb,
  143. "swap file: not a conventional zone file\n");
  144. return -EINVAL;
  145. }
  146. return iomap_swapfile_activate(sis, swap_file, span, &zonefs_iomap_ops);
  147. }
  148. static const struct address_space_operations zonefs_file_aops = {
  149. .readpage = zonefs_readpage,
  150. .readahead = zonefs_readahead,
  151. .writepage = zonefs_writepage,
  152. .writepages = zonefs_writepages,
  153. .set_page_dirty = iomap_set_page_dirty,
  154. .releasepage = iomap_releasepage,
  155. .invalidatepage = iomap_invalidatepage,
  156. .migratepage = iomap_migrate_page,
  157. .is_partially_uptodate = iomap_is_partially_uptodate,
  158. .error_remove_page = generic_error_remove_page,
  159. .direct_IO = noop_direct_IO,
  160. .swap_activate = zonefs_swap_activate,
  161. };
  162. static void zonefs_update_stats(struct inode *inode, loff_t new_isize)
  163. {
  164. struct super_block *sb = inode->i_sb;
  165. struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
  166. loff_t old_isize = i_size_read(inode);
  167. loff_t nr_blocks;
  168. if (new_isize == old_isize)
  169. return;
  170. spin_lock(&sbi->s_lock);
  171. /*
  172. * This may be called for an update after an IO error.
  173. * So beware of the values seen.
  174. */
  175. if (new_isize < old_isize) {
  176. nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
  177. if (sbi->s_used_blocks > nr_blocks)
  178. sbi->s_used_blocks -= nr_blocks;
  179. else
  180. sbi->s_used_blocks = 0;
  181. } else {
  182. sbi->s_used_blocks +=
  183. (new_isize - old_isize) >> sb->s_blocksize_bits;
  184. if (sbi->s_used_blocks > sbi->s_blocks)
  185. sbi->s_used_blocks = sbi->s_blocks;
  186. }
  187. spin_unlock(&sbi->s_lock);
  188. }
  189. /*
  190. * Check a zone condition and adjust its file inode access permissions for
  191. * offline and readonly zones. Return the inode size corresponding to the
  192. * amount of readable data in the zone.
  193. */
  194. static loff_t zonefs_check_zone_condition(struct inode *inode,
  195. struct blk_zone *zone, bool warn,
  196. bool mount)
  197. {
  198. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  199. switch (zone->cond) {
  200. case BLK_ZONE_COND_OFFLINE:
  201. /*
  202. * Dead zone: make the inode immutable, disable all accesses
  203. * and set the file size to 0 (zone wp set to zone start).
  204. */
  205. if (warn)
  206. zonefs_warn(inode->i_sb, "inode %lu: offline zone\n",
  207. inode->i_ino);
  208. inode->i_flags |= S_IMMUTABLE;
  209. inode->i_mode &= ~0777;
  210. zone->wp = zone->start;
  211. return 0;
  212. case BLK_ZONE_COND_READONLY:
  213. /*
  214. * The write pointer of read-only zones is invalid. If such a
  215. * zone is found during mount, the file size cannot be retrieved
  216. * so we treat the zone as offline (mount == true case).
  217. * Otherwise, keep the file size as it was when last updated
  218. * so that the user can recover data. In both cases, writes are
  219. * always disabled for the zone.
  220. */
  221. if (warn)
  222. zonefs_warn(inode->i_sb, "inode %lu: read-only zone\n",
  223. inode->i_ino);
  224. inode->i_flags |= S_IMMUTABLE;
  225. if (mount) {
  226. zone->cond = BLK_ZONE_COND_OFFLINE;
  227. inode->i_mode &= ~0777;
  228. zone->wp = zone->start;
  229. return 0;
  230. }
  231. inode->i_mode &= ~0222;
  232. return i_size_read(inode);
  233. case BLK_ZONE_COND_FULL:
  234. /* The write pointer of full zones is invalid. */
  235. return zi->i_max_size;
  236. default:
  237. if (zi->i_ztype == ZONEFS_ZTYPE_CNV)
  238. return zi->i_max_size;
  239. return (zone->wp - zone->start) << SECTOR_SHIFT;
  240. }
  241. }
  242. struct zonefs_ioerr_data {
  243. struct inode *inode;
  244. bool write;
  245. };
  246. static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
  247. void *data)
  248. {
  249. struct zonefs_ioerr_data *err = data;
  250. struct inode *inode = err->inode;
  251. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  252. struct super_block *sb = inode->i_sb;
  253. struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
  254. loff_t isize, data_size;
  255. /*
  256. * Check the zone condition: if the zone is not "bad" (offline or
  257. * read-only), read errors are simply signaled to the IO issuer as long
  258. * as there is no inconsistency between the inode size and the amount of
  259. * data writen in the zone (data_size).
  260. */
  261. data_size = zonefs_check_zone_condition(inode, zone, true, false);
  262. isize = i_size_read(inode);
  263. if (zone->cond != BLK_ZONE_COND_OFFLINE &&
  264. zone->cond != BLK_ZONE_COND_READONLY &&
  265. !err->write && isize == data_size)
  266. return 0;
  267. /*
  268. * At this point, we detected either a bad zone or an inconsistency
  269. * between the inode size and the amount of data written in the zone.
  270. * For the latter case, the cause may be a write IO error or an external
  271. * action on the device. Two error patterns exist:
  272. * 1) The inode size is lower than the amount of data in the zone:
  273. * a write operation partially failed and data was writen at the end
  274. * of the file. This can happen in the case of a large direct IO
  275. * needing several BIOs and/or write requests to be processed.
  276. * 2) The inode size is larger than the amount of data in the zone:
  277. * this can happen with a deferred write error with the use of the
  278. * device side write cache after getting successful write IO
  279. * completions. Other possibilities are (a) an external corruption,
  280. * e.g. an application reset the zone directly, or (b) the device
  281. * has a serious problem (e.g. firmware bug).
  282. *
  283. * In all cases, warn about inode size inconsistency and handle the
  284. * IO error according to the zone condition and to the mount options.
  285. */
  286. if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && isize != data_size)
  287. zonefs_warn(sb, "inode %lu: invalid size %lld (should be %lld)\n",
  288. inode->i_ino, isize, data_size);
  289. /*
  290. * First handle bad zones signaled by hardware. The mount options
  291. * errors=zone-ro and errors=zone-offline result in changing the
  292. * zone condition to read-only and offline respectively, as if the
  293. * condition was signaled by the hardware.
  294. */
  295. if (zone->cond == BLK_ZONE_COND_OFFLINE ||
  296. sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) {
  297. zonefs_warn(sb, "inode %lu: read/write access disabled\n",
  298. inode->i_ino);
  299. if (zone->cond != BLK_ZONE_COND_OFFLINE) {
  300. zone->cond = BLK_ZONE_COND_OFFLINE;
  301. data_size = zonefs_check_zone_condition(inode, zone,
  302. false, false);
  303. }
  304. } else if (zone->cond == BLK_ZONE_COND_READONLY ||
  305. sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) {
  306. zonefs_warn(sb, "inode %lu: write access disabled\n",
  307. inode->i_ino);
  308. if (zone->cond != BLK_ZONE_COND_READONLY) {
  309. zone->cond = BLK_ZONE_COND_READONLY;
  310. data_size = zonefs_check_zone_condition(inode, zone,
  311. false, false);
  312. }
  313. }
  314. /*
  315. * If the filesystem is mounted with the explicit-open mount option, we
  316. * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
  317. * the read-only or offline condition, to avoid attempting an explicit
  318. * close of the zone when the inode file is closed.
  319. */
  320. if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
  321. (zone->cond == BLK_ZONE_COND_OFFLINE ||
  322. zone->cond == BLK_ZONE_COND_READONLY))
  323. zi->i_flags &= ~ZONEFS_ZONE_OPEN;
  324. /*
  325. * If error=remount-ro was specified, any error result in remounting
  326. * the volume as read-only.
  327. */
  328. if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
  329. zonefs_warn(sb, "remounting filesystem read-only\n");
  330. sb->s_flags |= SB_RDONLY;
  331. }
  332. /*
  333. * Update block usage stats and the inode size to prevent access to
  334. * invalid data.
  335. */
  336. zonefs_update_stats(inode, data_size);
  337. zonefs_i_size_write(inode, data_size);
  338. zi->i_wpoffset = data_size;
  339. return 0;
  340. }
  341. /*
  342. * When an file IO error occurs, check the file zone to see if there is a change
  343. * in the zone condition (e.g. offline or read-only). For a failed write to a
  344. * sequential zone, the zone write pointer position must also be checked to
  345. * eventually correct the file size and zonefs inode write pointer offset
  346. * (which can be out of sync with the drive due to partial write failures).
  347. */
  348. static void __zonefs_io_error(struct inode *inode, bool write)
  349. {
  350. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  351. struct super_block *sb = inode->i_sb;
  352. struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
  353. unsigned int noio_flag;
  354. unsigned int nr_zones =
  355. zi->i_zone_size >> (sbi->s_zone_sectors_shift + SECTOR_SHIFT);
  356. struct zonefs_ioerr_data err = {
  357. .inode = inode,
  358. .write = write,
  359. };
  360. int ret;
  361. /*
  362. * Memory allocations in blkdev_report_zones() can trigger a memory
  363. * reclaim which may in turn cause a recursion into zonefs as well as
  364. * struct request allocations for the same device. The former case may
  365. * end up in a deadlock on the inode truncate mutex, while the latter
  366. * may prevent IO forward progress. Executing the report zones under
  367. * the GFP_NOIO context avoids both problems.
  368. */
  369. noio_flag = memalloc_noio_save();
  370. ret = blkdev_report_zones(sb->s_bdev, zi->i_zsector, nr_zones,
  371. zonefs_io_error_cb, &err);
  372. if (ret != nr_zones)
  373. zonefs_err(sb, "Get inode %lu zone information failed %d\n",
  374. inode->i_ino, ret);
  375. memalloc_noio_restore(noio_flag);
  376. }
  377. static void zonefs_io_error(struct inode *inode, bool write)
  378. {
  379. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  380. mutex_lock(&zi->i_truncate_mutex);
  381. __zonefs_io_error(inode, write);
  382. mutex_unlock(&zi->i_truncate_mutex);
  383. }
  384. static int zonefs_file_truncate(struct inode *inode, loff_t isize)
  385. {
  386. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  387. loff_t old_isize;
  388. enum req_opf op;
  389. int ret = 0;
  390. /*
  391. * Only sequential zone files can be truncated and truncation is allowed
  392. * only down to a 0 size, which is equivalent to a zone reset, and to
  393. * the maximum file size, which is equivalent to a zone finish.
  394. */
  395. if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
  396. return -EPERM;
  397. if (!isize)
  398. op = REQ_OP_ZONE_RESET;
  399. else if (isize == zi->i_max_size)
  400. op = REQ_OP_ZONE_FINISH;
  401. else
  402. return -EPERM;
  403. inode_dio_wait(inode);
  404. /* Serialize against page faults */
  405. down_write(&zi->i_mmap_sem);
  406. /* Serialize against zonefs_iomap_begin() */
  407. mutex_lock(&zi->i_truncate_mutex);
  408. old_isize = i_size_read(inode);
  409. if (isize == old_isize)
  410. goto unlock;
  411. ret = zonefs_zone_mgmt(inode, op);
  412. if (ret)
  413. goto unlock;
  414. /*
  415. * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set,
  416. * take care of open zones.
  417. */
  418. if (zi->i_flags & ZONEFS_ZONE_OPEN) {
  419. /*
  420. * Truncating a zone to EMPTY or FULL is the equivalent of
  421. * closing the zone. For a truncation to 0, we need to
  422. * re-open the zone to ensure new writes can be processed.
  423. * For a truncation to the maximum file size, the zone is
  424. * closed and writes cannot be accepted anymore, so clear
  425. * the open flag.
  426. */
  427. if (!isize)
  428. ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
  429. else
  430. zi->i_flags &= ~ZONEFS_ZONE_OPEN;
  431. }
  432. zonefs_update_stats(inode, isize);
  433. truncate_setsize(inode, isize);
  434. zi->i_wpoffset = isize;
  435. unlock:
  436. mutex_unlock(&zi->i_truncate_mutex);
  437. up_write(&zi->i_mmap_sem);
  438. return ret;
  439. }
  440. static int zonefs_inode_setattr(struct dentry *dentry, struct iattr *iattr)
  441. {
  442. struct inode *inode = d_inode(dentry);
  443. int ret;
  444. if (unlikely(IS_IMMUTABLE(inode)))
  445. return -EPERM;
  446. ret = setattr_prepare(dentry, iattr);
  447. if (ret)
  448. return ret;
  449. /*
  450. * Since files and directories cannot be created nor deleted, do not
  451. * allow setting any write attributes on the sub-directories grouping
  452. * files by zone type.
  453. */
  454. if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
  455. (iattr->ia_mode & 0222))
  456. return -EPERM;
  457. if (((iattr->ia_valid & ATTR_UID) &&
  458. !uid_eq(iattr->ia_uid, inode->i_uid)) ||
  459. ((iattr->ia_valid & ATTR_GID) &&
  460. !gid_eq(iattr->ia_gid, inode->i_gid))) {
  461. ret = dquot_transfer(inode, iattr);
  462. if (ret)
  463. return ret;
  464. }
  465. if (iattr->ia_valid & ATTR_SIZE) {
  466. ret = zonefs_file_truncate(inode, iattr->ia_size);
  467. if (ret)
  468. return ret;
  469. }
  470. setattr_copy(inode, iattr);
  471. return 0;
  472. }
  473. static const struct inode_operations zonefs_file_inode_operations = {
  474. .setattr = zonefs_inode_setattr,
  475. };
  476. static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end,
  477. int datasync)
  478. {
  479. struct inode *inode = file_inode(file);
  480. int ret = 0;
  481. if (unlikely(IS_IMMUTABLE(inode)))
  482. return -EPERM;
  483. /*
  484. * Since only direct writes are allowed in sequential files, page cache
  485. * flush is needed only for conventional zone files.
  486. */
  487. if (ZONEFS_I(inode)->i_ztype == ZONEFS_ZTYPE_CNV)
  488. ret = file_write_and_wait_range(file, start, end);
  489. if (!ret)
  490. ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL);
  491. if (ret)
  492. zonefs_io_error(inode, true);
  493. return ret;
  494. }
  495. static vm_fault_t zonefs_filemap_fault(struct vm_fault *vmf)
  496. {
  497. struct zonefs_inode_info *zi = ZONEFS_I(file_inode(vmf->vma->vm_file));
  498. vm_fault_t ret;
  499. down_read(&zi->i_mmap_sem);
  500. ret = filemap_fault(vmf);
  501. up_read(&zi->i_mmap_sem);
  502. return ret;
  503. }
  504. static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)
  505. {
  506. struct inode *inode = file_inode(vmf->vma->vm_file);
  507. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  508. vm_fault_t ret;
  509. if (unlikely(IS_IMMUTABLE(inode)))
  510. return VM_FAULT_SIGBUS;
  511. /*
  512. * Sanity check: only conventional zone files can have shared
  513. * writeable mappings.
  514. */
  515. if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
  516. return VM_FAULT_NOPAGE;
  517. sb_start_pagefault(inode->i_sb);
  518. file_update_time(vmf->vma->vm_file);
  519. /* Serialize against truncates */
  520. down_read(&zi->i_mmap_sem);
  521. ret = iomap_page_mkwrite(vmf, &zonefs_iomap_ops);
  522. up_read(&zi->i_mmap_sem);
  523. sb_end_pagefault(inode->i_sb);
  524. return ret;
  525. }
  526. static const struct vm_operations_struct zonefs_file_vm_ops = {
  527. .fault = zonefs_filemap_fault,
  528. .map_pages = filemap_map_pages,
  529. .page_mkwrite = zonefs_filemap_page_mkwrite,
  530. };
  531. static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma)
  532. {
  533. /*
  534. * Conventional zones accept random writes, so their files can support
  535. * shared writable mappings. For sequential zone files, only read
  536. * mappings are possible since there are no guarantees for write
  537. * ordering between msync() and page cache writeback.
  538. */
  539. if (ZONEFS_I(file_inode(file))->i_ztype == ZONEFS_ZTYPE_SEQ &&
  540. (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
  541. return -EINVAL;
  542. file_accessed(file);
  543. vma->vm_ops = &zonefs_file_vm_ops;
  544. return 0;
  545. }
  546. static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence)
  547. {
  548. loff_t isize = i_size_read(file_inode(file));
  549. /*
  550. * Seeks are limited to below the zone size for conventional zones
  551. * and below the zone write pointer for sequential zones. In both
  552. * cases, this limit is the inode size.
  553. */
  554. return generic_file_llseek_size(file, offset, whence, isize, isize);
  555. }
  556. static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
  557. int error, unsigned int flags)
  558. {
  559. struct inode *inode = file_inode(iocb->ki_filp);
  560. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  561. if (error) {
  562. zonefs_io_error(inode, true);
  563. return error;
  564. }
  565. if (size && zi->i_ztype != ZONEFS_ZTYPE_CNV) {
  566. /*
  567. * Note that we may be seeing completions out of order,
  568. * but that is not a problem since a write completed
  569. * successfully necessarily means that all preceding writes
  570. * were also successful. So we can safely increase the inode
  571. * size to the write end location.
  572. */
  573. mutex_lock(&zi->i_truncate_mutex);
  574. if (i_size_read(inode) < iocb->ki_pos + size) {
  575. zonefs_update_stats(inode, iocb->ki_pos + size);
  576. zonefs_i_size_write(inode, iocb->ki_pos + size);
  577. }
  578. mutex_unlock(&zi->i_truncate_mutex);
  579. }
  580. return 0;
  581. }
  582. static const struct iomap_dio_ops zonefs_write_dio_ops = {
  583. .end_io = zonefs_file_write_dio_end_io,
  584. };
  585. static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from)
  586. {
  587. struct inode *inode = file_inode(iocb->ki_filp);
  588. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  589. struct block_device *bdev = inode->i_sb->s_bdev;
  590. unsigned int max;
  591. struct bio *bio;
  592. ssize_t size;
  593. int nr_pages;
  594. ssize_t ret;
  595. max = queue_max_zone_append_sectors(bdev_get_queue(bdev));
  596. max = ALIGN_DOWN(max << SECTOR_SHIFT, inode->i_sb->s_blocksize);
  597. iov_iter_truncate(from, max);
  598. nr_pages = iov_iter_npages(from, BIO_MAX_PAGES);
  599. if (!nr_pages)
  600. return 0;
  601. bio = bio_alloc_bioset(GFP_NOFS, nr_pages, &fs_bio_set);
  602. if (!bio)
  603. return -ENOMEM;
  604. bio_set_dev(bio, bdev);
  605. bio->bi_iter.bi_sector = zi->i_zsector;
  606. bio->bi_write_hint = iocb->ki_hint;
  607. bio->bi_ioprio = iocb->ki_ioprio;
  608. bio->bi_opf = REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE;
  609. if (iocb->ki_flags & IOCB_DSYNC)
  610. bio->bi_opf |= REQ_FUA;
  611. ret = bio_iov_iter_get_pages(bio, from);
  612. if (unlikely(ret))
  613. goto out_release;
  614. size = bio->bi_iter.bi_size;
  615. task_io_account_write(size);
  616. if (iocb->ki_flags & IOCB_HIPRI)
  617. bio_set_polled(bio, iocb);
  618. ret = submit_bio_wait(bio);
  619. zonefs_file_write_dio_end_io(iocb, size, ret, 0);
  620. out_release:
  621. bio_release_pages(bio, false);
  622. bio_put(bio);
  623. if (ret >= 0) {
  624. iocb->ki_pos += size;
  625. return size;
  626. }
  627. return ret;
  628. }
  629. /*
  630. * Do not exceed the LFS limits nor the file zone size. If pos is under the
  631. * limit it becomes a short access. If it exceeds the limit, return -EFBIG.
  632. */
  633. static loff_t zonefs_write_check_limits(struct file *file, loff_t pos,
  634. loff_t count)
  635. {
  636. struct inode *inode = file_inode(file);
  637. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  638. loff_t limit = rlimit(RLIMIT_FSIZE);
  639. loff_t max_size = zi->i_max_size;
  640. if (limit != RLIM_INFINITY) {
  641. if (pos >= limit) {
  642. send_sig(SIGXFSZ, current, 0);
  643. return -EFBIG;
  644. }
  645. count = min(count, limit - pos);
  646. }
  647. if (!(file->f_flags & O_LARGEFILE))
  648. max_size = min_t(loff_t, MAX_NON_LFS, max_size);
  649. if (unlikely(pos >= max_size))
  650. return -EFBIG;
  651. return min(count, max_size - pos);
  652. }
  653. static ssize_t zonefs_write_checks(struct kiocb *iocb, struct iov_iter *from)
  654. {
  655. struct file *file = iocb->ki_filp;
  656. struct inode *inode = file_inode(file);
  657. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  658. loff_t count;
  659. if (IS_SWAPFILE(inode))
  660. return -ETXTBSY;
  661. if (!iov_iter_count(from))
  662. return 0;
  663. if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
  664. return -EINVAL;
  665. if (iocb->ki_flags & IOCB_APPEND) {
  666. if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
  667. return -EINVAL;
  668. mutex_lock(&zi->i_truncate_mutex);
  669. iocb->ki_pos = zi->i_wpoffset;
  670. mutex_unlock(&zi->i_truncate_mutex);
  671. }
  672. count = zonefs_write_check_limits(file, iocb->ki_pos,
  673. iov_iter_count(from));
  674. if (count < 0)
  675. return count;
  676. iov_iter_truncate(from, count);
  677. return iov_iter_count(from);
  678. }
  679. /*
  680. * Handle direct writes. For sequential zone files, this is the only possible
  681. * write path. For these files, check that the user is issuing writes
  682. * sequentially from the end of the file. This code assumes that the block layer
  683. * delivers write requests to the device in sequential order. This is always the
  684. * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE
  685. * elevator feature is being used (e.g. mq-deadline). The block layer always
  686. * automatically select such an elevator for zoned block devices during the
  687. * device initialization.
  688. */
  689. static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
  690. {
  691. struct inode *inode = file_inode(iocb->ki_filp);
  692. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  693. struct super_block *sb = inode->i_sb;
  694. bool sync = is_sync_kiocb(iocb);
  695. bool append = false;
  696. ssize_t ret, count;
  697. /*
  698. * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
  699. * as this can cause write reordering (e.g. the first aio gets EAGAIN
  700. * on the inode lock but the second goes through but is now unaligned).
  701. */
  702. if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && !sync &&
  703. (iocb->ki_flags & IOCB_NOWAIT))
  704. return -EOPNOTSUPP;
  705. if (iocb->ki_flags & IOCB_NOWAIT) {
  706. if (!inode_trylock(inode))
  707. return -EAGAIN;
  708. } else {
  709. inode_lock(inode);
  710. }
  711. count = zonefs_write_checks(iocb, from);
  712. if (count <= 0) {
  713. ret = count;
  714. goto inode_unlock;
  715. }
  716. if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
  717. ret = -EINVAL;
  718. goto inode_unlock;
  719. }
  720. /* Enforce sequential writes (append only) in sequential zones */
  721. if (zi->i_ztype == ZONEFS_ZTYPE_SEQ) {
  722. mutex_lock(&zi->i_truncate_mutex);
  723. if (iocb->ki_pos != zi->i_wpoffset) {
  724. mutex_unlock(&zi->i_truncate_mutex);
  725. ret = -EINVAL;
  726. goto inode_unlock;
  727. }
  728. mutex_unlock(&zi->i_truncate_mutex);
  729. append = sync;
  730. }
  731. if (append)
  732. ret = zonefs_file_dio_append(iocb, from);
  733. else
  734. ret = iomap_dio_rw(iocb, from, &zonefs_iomap_ops,
  735. &zonefs_write_dio_ops, sync);
  736. if (zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
  737. (ret > 0 || ret == -EIOCBQUEUED)) {
  738. if (ret > 0)
  739. count = ret;
  740. mutex_lock(&zi->i_truncate_mutex);
  741. zi->i_wpoffset += count;
  742. mutex_unlock(&zi->i_truncate_mutex);
  743. }
  744. inode_unlock:
  745. inode_unlock(inode);
  746. return ret;
  747. }
  748. static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
  749. struct iov_iter *from)
  750. {
  751. struct inode *inode = file_inode(iocb->ki_filp);
  752. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  753. ssize_t ret;
  754. /*
  755. * Direct IO writes are mandatory for sequential zone files so that the
  756. * write IO issuing order is preserved.
  757. */
  758. if (zi->i_ztype != ZONEFS_ZTYPE_CNV)
  759. return -EIO;
  760. if (iocb->ki_flags & IOCB_NOWAIT) {
  761. if (!inode_trylock(inode))
  762. return -EAGAIN;
  763. } else {
  764. inode_lock(inode);
  765. }
  766. ret = zonefs_write_checks(iocb, from);
  767. if (ret <= 0)
  768. goto inode_unlock;
  769. ret = iomap_file_buffered_write(iocb, from, &zonefs_iomap_ops);
  770. if (ret > 0)
  771. iocb->ki_pos += ret;
  772. else if (ret == -EIO)
  773. zonefs_io_error(inode, true);
  774. inode_unlock:
  775. inode_unlock(inode);
  776. if (ret > 0)
  777. ret = generic_write_sync(iocb, ret);
  778. return ret;
  779. }
  780. static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  781. {
  782. struct inode *inode = file_inode(iocb->ki_filp);
  783. if (unlikely(IS_IMMUTABLE(inode)))
  784. return -EPERM;
  785. if (sb_rdonly(inode->i_sb))
  786. return -EROFS;
  787. /* Write operations beyond the zone size are not allowed */
  788. if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size)
  789. return -EFBIG;
  790. if (iocb->ki_flags & IOCB_DIRECT) {
  791. ssize_t ret = zonefs_file_dio_write(iocb, from);
  792. if (ret != -ENOTBLK)
  793. return ret;
  794. }
  795. return zonefs_file_buffered_write(iocb, from);
  796. }
  797. static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size,
  798. int error, unsigned int flags)
  799. {
  800. if (error) {
  801. zonefs_io_error(file_inode(iocb->ki_filp), false);
  802. return error;
  803. }
  804. return 0;
  805. }
  806. static const struct iomap_dio_ops zonefs_read_dio_ops = {
  807. .end_io = zonefs_file_read_dio_end_io,
  808. };
  809. static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  810. {
  811. struct inode *inode = file_inode(iocb->ki_filp);
  812. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  813. struct super_block *sb = inode->i_sb;
  814. loff_t isize;
  815. ssize_t ret;
  816. /* Offline zones cannot be read */
  817. if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777)))
  818. return -EPERM;
  819. if (iocb->ki_pos >= zi->i_max_size)
  820. return 0;
  821. if (iocb->ki_flags & IOCB_NOWAIT) {
  822. if (!inode_trylock_shared(inode))
  823. return -EAGAIN;
  824. } else {
  825. inode_lock_shared(inode);
  826. }
  827. /* Limit read operations to written data */
  828. mutex_lock(&zi->i_truncate_mutex);
  829. isize = i_size_read(inode);
  830. if (iocb->ki_pos >= isize) {
  831. mutex_unlock(&zi->i_truncate_mutex);
  832. ret = 0;
  833. goto inode_unlock;
  834. }
  835. iov_iter_truncate(to, isize - iocb->ki_pos);
  836. mutex_unlock(&zi->i_truncate_mutex);
  837. if (iocb->ki_flags & IOCB_DIRECT) {
  838. size_t count = iov_iter_count(to);
  839. if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
  840. ret = -EINVAL;
  841. goto inode_unlock;
  842. }
  843. file_accessed(iocb->ki_filp);
  844. ret = iomap_dio_rw(iocb, to, &zonefs_iomap_ops,
  845. &zonefs_read_dio_ops, is_sync_kiocb(iocb));
  846. } else {
  847. ret = generic_file_read_iter(iocb, to);
  848. if (ret == -EIO)
  849. zonefs_io_error(inode, false);
  850. }
  851. inode_unlock:
  852. inode_unlock_shared(inode);
  853. return ret;
  854. }
  855. static inline bool zonefs_file_use_exp_open(struct inode *inode, struct file *file)
  856. {
  857. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  858. struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
  859. if (!(sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN))
  860. return false;
  861. if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
  862. return false;
  863. if (!(file->f_mode & FMODE_WRITE))
  864. return false;
  865. return true;
  866. }
  867. static int zonefs_open_zone(struct inode *inode)
  868. {
  869. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  870. struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
  871. int ret = 0;
  872. mutex_lock(&zi->i_truncate_mutex);
  873. if (!zi->i_wr_refcnt) {
  874. if (atomic_inc_return(&sbi->s_open_zones) > sbi->s_max_open_zones) {
  875. atomic_dec(&sbi->s_open_zones);
  876. ret = -EBUSY;
  877. goto unlock;
  878. }
  879. if (i_size_read(inode) < zi->i_max_size) {
  880. ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
  881. if (ret) {
  882. atomic_dec(&sbi->s_open_zones);
  883. goto unlock;
  884. }
  885. zi->i_flags |= ZONEFS_ZONE_OPEN;
  886. }
  887. }
  888. zi->i_wr_refcnt++;
  889. unlock:
  890. mutex_unlock(&zi->i_truncate_mutex);
  891. return ret;
  892. }
  893. static int zonefs_file_open(struct inode *inode, struct file *file)
  894. {
  895. int ret;
  896. ret = generic_file_open(inode, file);
  897. if (ret)
  898. return ret;
  899. if (zonefs_file_use_exp_open(inode, file))
  900. return zonefs_open_zone(inode);
  901. return 0;
  902. }
  903. static void zonefs_close_zone(struct inode *inode)
  904. {
  905. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  906. int ret = 0;
  907. mutex_lock(&zi->i_truncate_mutex);
  908. zi->i_wr_refcnt--;
  909. if (!zi->i_wr_refcnt) {
  910. struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
  911. struct super_block *sb = inode->i_sb;
  912. /*
  913. * If the file zone is full, it is not open anymore and we only
  914. * need to decrement the open count.
  915. */
  916. if (!(zi->i_flags & ZONEFS_ZONE_OPEN))
  917. goto dec;
  918. ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
  919. if (ret) {
  920. __zonefs_io_error(inode, false);
  921. /*
  922. * Leaving zones explicitly open may lead to a state
  923. * where most zones cannot be written (zone resources
  924. * exhausted). So take preventive action by remounting
  925. * read-only.
  926. */
  927. if (zi->i_flags & ZONEFS_ZONE_OPEN &&
  928. !(sb->s_flags & SB_RDONLY)) {
  929. zonefs_warn(sb, "closing zone failed, remounting filesystem read-only\n");
  930. sb->s_flags |= SB_RDONLY;
  931. }
  932. }
  933. zi->i_flags &= ~ZONEFS_ZONE_OPEN;
  934. dec:
  935. atomic_dec(&sbi->s_open_zones);
  936. }
  937. mutex_unlock(&zi->i_truncate_mutex);
  938. }
  939. static int zonefs_file_release(struct inode *inode, struct file *file)
  940. {
  941. /*
  942. * If we explicitly open a zone we must close it again as well, but the
  943. * zone management operation can fail (either due to an IO error or as
  944. * the zone has gone offline or read-only). Make sure we don't fail the
  945. * close(2) for user-space.
  946. */
  947. if (zonefs_file_use_exp_open(inode, file))
  948. zonefs_close_zone(inode);
  949. return 0;
  950. }
  951. static const struct file_operations zonefs_file_operations = {
  952. .open = zonefs_file_open,
  953. .release = zonefs_file_release,
  954. .fsync = zonefs_file_fsync,
  955. .mmap = zonefs_file_mmap,
  956. .llseek = zonefs_file_llseek,
  957. .read_iter = zonefs_file_read_iter,
  958. .write_iter = zonefs_file_write_iter,
  959. .splice_read = generic_file_splice_read,
  960. .splice_write = iter_file_splice_write,
  961. .iopoll = iomap_dio_iopoll,
  962. };
  963. static struct kmem_cache *zonefs_inode_cachep;
  964. static struct inode *zonefs_alloc_inode(struct super_block *sb)
  965. {
  966. struct zonefs_inode_info *zi;
  967. zi = kmem_cache_alloc(zonefs_inode_cachep, GFP_KERNEL);
  968. if (!zi)
  969. return NULL;
  970. inode_init_once(&zi->i_vnode);
  971. mutex_init(&zi->i_truncate_mutex);
  972. init_rwsem(&zi->i_mmap_sem);
  973. zi->i_wr_refcnt = 0;
  974. return &zi->i_vnode;
  975. }
  976. static void zonefs_free_inode(struct inode *inode)
  977. {
  978. kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
  979. }
  980. /*
  981. * File system stat.
  982. */
  983. static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
  984. {
  985. struct super_block *sb = dentry->d_sb;
  986. struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
  987. enum zonefs_ztype t;
  988. u64 fsid;
  989. buf->f_type = ZONEFS_MAGIC;
  990. buf->f_bsize = sb->s_blocksize;
  991. buf->f_namelen = ZONEFS_NAME_MAX;
  992. spin_lock(&sbi->s_lock);
  993. buf->f_blocks = sbi->s_blocks;
  994. if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
  995. buf->f_bfree = 0;
  996. else
  997. buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
  998. buf->f_bavail = buf->f_bfree;
  999. for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
  1000. if (sbi->s_nr_files[t])
  1001. buf->f_files += sbi->s_nr_files[t] + 1;
  1002. }
  1003. buf->f_ffree = 0;
  1004. spin_unlock(&sbi->s_lock);
  1005. fsid = le64_to_cpup((void *)sbi->s_uuid.b) ^
  1006. le64_to_cpup((void *)sbi->s_uuid.b + sizeof(u64));
  1007. buf->f_fsid = u64_to_fsid(fsid);
  1008. return 0;
  1009. }
  1010. enum {
  1011. Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
  1012. Opt_explicit_open, Opt_err,
  1013. };
  1014. static const match_table_t tokens = {
  1015. { Opt_errors_ro, "errors=remount-ro"},
  1016. { Opt_errors_zro, "errors=zone-ro"},
  1017. { Opt_errors_zol, "errors=zone-offline"},
  1018. { Opt_errors_repair, "errors=repair"},
  1019. { Opt_explicit_open, "explicit-open" },
  1020. { Opt_err, NULL}
  1021. };
  1022. static int zonefs_parse_options(struct super_block *sb, char *options)
  1023. {
  1024. struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
  1025. substring_t args[MAX_OPT_ARGS];
  1026. char *p;
  1027. if (!options)
  1028. return 0;
  1029. while ((p = strsep(&options, ",")) != NULL) {
  1030. int token;
  1031. if (!*p)
  1032. continue;
  1033. token = match_token(p, tokens, args);
  1034. switch (token) {
  1035. case Opt_errors_ro:
  1036. sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
  1037. sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
  1038. break;
  1039. case Opt_errors_zro:
  1040. sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
  1041. sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
  1042. break;
  1043. case Opt_errors_zol:
  1044. sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
  1045. sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
  1046. break;
  1047. case Opt_errors_repair:
  1048. sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
  1049. sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
  1050. break;
  1051. case Opt_explicit_open:
  1052. sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
  1053. break;
  1054. default:
  1055. return -EINVAL;
  1056. }
  1057. }
  1058. return 0;
  1059. }
  1060. static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
  1061. {
  1062. struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
  1063. if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
  1064. seq_puts(seq, ",errors=remount-ro");
  1065. if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
  1066. seq_puts(seq, ",errors=zone-ro");
  1067. if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
  1068. seq_puts(seq, ",errors=zone-offline");
  1069. if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
  1070. seq_puts(seq, ",errors=repair");
  1071. return 0;
  1072. }
  1073. static int zonefs_remount(struct super_block *sb, int *flags, char *data)
  1074. {
  1075. sync_filesystem(sb);
  1076. return zonefs_parse_options(sb, data);
  1077. }
  1078. static const struct super_operations zonefs_sops = {
  1079. .alloc_inode = zonefs_alloc_inode,
  1080. .free_inode = zonefs_free_inode,
  1081. .statfs = zonefs_statfs,
  1082. .remount_fs = zonefs_remount,
  1083. .show_options = zonefs_show_options,
  1084. };
  1085. static const struct inode_operations zonefs_dir_inode_operations = {
  1086. .lookup = simple_lookup,
  1087. .setattr = zonefs_inode_setattr,
  1088. };
  1089. static void zonefs_init_dir_inode(struct inode *parent, struct inode *inode,
  1090. enum zonefs_ztype type)
  1091. {
  1092. struct super_block *sb = parent->i_sb;
  1093. inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk) + type + 1;
  1094. inode_init_owner(inode, parent, S_IFDIR | 0555);
  1095. inode->i_op = &zonefs_dir_inode_operations;
  1096. inode->i_fop = &simple_dir_operations;
  1097. set_nlink(inode, 2);
  1098. inc_nlink(parent);
  1099. }
  1100. static void zonefs_init_file_inode(struct inode *inode, struct blk_zone *zone,
  1101. enum zonefs_ztype type)
  1102. {
  1103. struct super_block *sb = inode->i_sb;
  1104. struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
  1105. struct zonefs_inode_info *zi = ZONEFS_I(inode);
  1106. inode->i_ino = zone->start >> sbi->s_zone_sectors_shift;
  1107. inode->i_mode = S_IFREG | sbi->s_perm;
  1108. zi->i_ztype = type;
  1109. zi->i_zsector = zone->start;
  1110. zi->i_zone_size = zone->len << SECTOR_SHIFT;
  1111. zi->i_max_size = min_t(loff_t, MAX_LFS_FILESIZE,
  1112. zone->capacity << SECTOR_SHIFT);
  1113. zi->i_wpoffset = zonefs_check_zone_condition(inode, zone, true, true);
  1114. inode->i_uid = sbi->s_uid;
  1115. inode->i_gid = sbi->s_gid;
  1116. inode->i_size = zi->i_wpoffset;
  1117. inode->i_blocks = zi->i_max_size >> SECTOR_SHIFT;
  1118. inode->i_op = &zonefs_file_inode_operations;
  1119. inode->i_fop = &zonefs_file_operations;
  1120. inode->i_mapping->a_ops = &zonefs_file_aops;
  1121. sb->s_maxbytes = max(zi->i_max_size, sb->s_maxbytes);
  1122. sbi->s_blocks += zi->i_max_size >> sb->s_blocksize_bits;
  1123. sbi->s_used_blocks += zi->i_wpoffset >> sb->s_blocksize_bits;
  1124. }
  1125. static struct dentry *zonefs_create_inode(struct dentry *parent,
  1126. const char *name, struct blk_zone *zone,
  1127. enum zonefs_ztype type)
  1128. {
  1129. struct inode *dir = d_inode(parent);
  1130. struct dentry *dentry;
  1131. struct inode *inode;
  1132. dentry = d_alloc_name(parent, name);
  1133. if (!dentry)
  1134. return NULL;
  1135. inode = new_inode(parent->d_sb);
  1136. if (!inode)
  1137. goto dput;
  1138. inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime;
  1139. if (zone)
  1140. zonefs_init_file_inode(inode, zone, type);
  1141. else
  1142. zonefs_init_dir_inode(dir, inode, type);
  1143. d_add(dentry, inode);
  1144. dir->i_size++;
  1145. return dentry;
  1146. dput:
  1147. dput(dentry);
  1148. return NULL;
  1149. }
  1150. struct zonefs_zone_data {
  1151. struct super_block *sb;
  1152. unsigned int nr_zones[ZONEFS_ZTYPE_MAX];
  1153. struct blk_zone *zones;
  1154. };
  1155. /*
  1156. * Create a zone group and populate it with zone files.
  1157. */
  1158. static int zonefs_create_zgroup(struct zonefs_zone_data *zd,
  1159. enum zonefs_ztype type)
  1160. {
  1161. struct super_block *sb = zd->sb;
  1162. struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
  1163. struct blk_zone *zone, *next, *end;
  1164. const char *zgroup_name;
  1165. char *file_name;
  1166. struct dentry *dir;
  1167. unsigned int n = 0;
  1168. int ret;
  1169. /* If the group is empty, there is nothing to do */
  1170. if (!zd->nr_zones[type])
  1171. return 0;
  1172. file_name = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
  1173. if (!file_name)
  1174. return -ENOMEM;
  1175. if (type == ZONEFS_ZTYPE_CNV)
  1176. zgroup_name = "cnv";
  1177. else
  1178. zgroup_name = "seq";
  1179. dir = zonefs_create_inode(sb->s_root, zgroup_name, NULL, type);
  1180. if (!dir) {
  1181. ret = -ENOMEM;
  1182. goto free;
  1183. }
  1184. /*
  1185. * The first zone contains the super block: skip it.
  1186. */
  1187. end = zd->zones + blkdev_nr_zones(sb->s_bdev->bd_disk);
  1188. for (zone = &zd->zones[1]; zone < end; zone = next) {
  1189. next = zone + 1;
  1190. if (zonefs_zone_type(zone) != type)
  1191. continue;
  1192. /*
  1193. * For conventional zones, contiguous zones can be aggregated
  1194. * together to form larger files. Note that this overwrites the
  1195. * length of the first zone of the set of contiguous zones
  1196. * aggregated together. If one offline or read-only zone is
  1197. * found, assume that all zones aggregated have the same
  1198. * condition.
  1199. */
  1200. if (type == ZONEFS_ZTYPE_CNV &&
  1201. (sbi->s_features & ZONEFS_F_AGGRCNV)) {
  1202. for (; next < end; next++) {
  1203. if (zonefs_zone_type(next) != type)
  1204. break;
  1205. zone->len += next->len;
  1206. zone->capacity += next->capacity;
  1207. if (next->cond == BLK_ZONE_COND_READONLY &&
  1208. zone->cond != BLK_ZONE_COND_OFFLINE)
  1209. zone->cond = BLK_ZONE_COND_READONLY;
  1210. else if (next->cond == BLK_ZONE_COND_OFFLINE)
  1211. zone->cond = BLK_ZONE_COND_OFFLINE;
  1212. }
  1213. if (zone->capacity != zone->len) {
  1214. zonefs_err(sb, "Invalid conventional zone capacity\n");
  1215. ret = -EINVAL;
  1216. goto free;
  1217. }
  1218. }
  1219. /*
  1220. * Use the file number within its group as file name.
  1221. */
  1222. snprintf(file_name, ZONEFS_NAME_MAX - 1, "%u", n);
  1223. if (!zonefs_create_inode(dir, file_name, zone, type)) {
  1224. ret = -ENOMEM;
  1225. goto free;
  1226. }
  1227. n++;
  1228. }
  1229. zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
  1230. zgroup_name, n, n > 1 ? "s" : "");
  1231. sbi->s_nr_files[type] = n;
  1232. ret = 0;
  1233. free:
  1234. kfree(file_name);
  1235. return ret;
  1236. }
  1237. static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
  1238. void *data)
  1239. {
  1240. struct zonefs_zone_data *zd = data;
  1241. /*
  1242. * Count the number of usable zones: the first zone at index 0 contains
  1243. * the super block and is ignored.
  1244. */
  1245. switch (zone->type) {
  1246. case BLK_ZONE_TYPE_CONVENTIONAL:
  1247. zone->wp = zone->start + zone->len;
  1248. if (idx)
  1249. zd->nr_zones[ZONEFS_ZTYPE_CNV]++;
  1250. break;
  1251. case BLK_ZONE_TYPE_SEQWRITE_REQ:
  1252. case BLK_ZONE_TYPE_SEQWRITE_PREF:
  1253. if (idx)
  1254. zd->nr_zones[ZONEFS_ZTYPE_SEQ]++;
  1255. break;
  1256. default:
  1257. zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
  1258. zone->type);
  1259. return -EIO;
  1260. }
  1261. memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
  1262. return 0;
  1263. }
  1264. static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
  1265. {
  1266. struct block_device *bdev = zd->sb->s_bdev;
  1267. int ret;
  1268. zd->zones = kvcalloc(blkdev_nr_zones(bdev->bd_disk),
  1269. sizeof(struct blk_zone), GFP_KERNEL);
  1270. if (!zd->zones)
  1271. return -ENOMEM;
  1272. /* Get zones information from the device */
  1273. ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
  1274. zonefs_get_zone_info_cb, zd);
  1275. if (ret < 0) {
  1276. zonefs_err(zd->sb, "Zone report failed %d\n", ret);
  1277. return ret;
  1278. }
  1279. if (ret != blkdev_nr_zones(bdev->bd_disk)) {
  1280. zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
  1281. ret, blkdev_nr_zones(bdev->bd_disk));
  1282. return -EIO;
  1283. }
  1284. return 0;
  1285. }
  1286. static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data *zd)
  1287. {
  1288. kvfree(zd->zones);
  1289. }
  1290. /*
  1291. * Read super block information from the device.
  1292. */
  1293. static int zonefs_read_super(struct super_block *sb)
  1294. {
  1295. struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
  1296. struct zonefs_super *super;
  1297. u32 crc, stored_crc;
  1298. struct page *page;
  1299. struct bio_vec bio_vec;
  1300. struct bio bio;
  1301. int ret;
  1302. page = alloc_page(GFP_KERNEL);
  1303. if (!page)
  1304. return -ENOMEM;
  1305. bio_init(&bio, &bio_vec, 1);
  1306. bio.bi_iter.bi_sector = 0;
  1307. bio.bi_opf = REQ_OP_READ;
  1308. bio_set_dev(&bio, sb->s_bdev);
  1309. bio_add_page(&bio, page, PAGE_SIZE, 0);
  1310. ret = submit_bio_wait(&bio);
  1311. if (ret)
  1312. goto free_page;
  1313. super = kmap(page);
  1314. ret = -EINVAL;
  1315. if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
  1316. goto unmap;
  1317. stored_crc = le32_to_cpu(super->s_crc);
  1318. super->s_crc = 0;
  1319. crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
  1320. if (crc != stored_crc) {
  1321. zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
  1322. crc, stored_crc);
  1323. goto unmap;
  1324. }
  1325. sbi->s_features = le64_to_cpu(super->s_features);
  1326. if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
  1327. zonefs_err(sb, "Unknown features set 0x%llx\n",
  1328. sbi->s_features);
  1329. goto unmap;
  1330. }
  1331. if (sbi->s_features & ZONEFS_F_UID) {
  1332. sbi->s_uid = make_kuid(current_user_ns(),
  1333. le32_to_cpu(super->s_uid));
  1334. if (!uid_valid(sbi->s_uid)) {
  1335. zonefs_err(sb, "Invalid UID feature\n");
  1336. goto unmap;
  1337. }
  1338. }
  1339. if (sbi->s_features & ZONEFS_F_GID) {
  1340. sbi->s_gid = make_kgid(current_user_ns(),
  1341. le32_to_cpu(super->s_gid));
  1342. if (!gid_valid(sbi->s_gid)) {
  1343. zonefs_err(sb, "Invalid GID feature\n");
  1344. goto unmap;
  1345. }
  1346. }
  1347. if (sbi->s_features & ZONEFS_F_PERM)
  1348. sbi->s_perm = le32_to_cpu(super->s_perm);
  1349. if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
  1350. zonefs_err(sb, "Reserved area is being used\n");
  1351. goto unmap;
  1352. }
  1353. import_uuid(&sbi->s_uuid, super->s_uuid);
  1354. ret = 0;
  1355. unmap:
  1356. kunmap(page);
  1357. free_page:
  1358. __free_page(page);
  1359. return ret;
  1360. }
  1361. /*
  1362. * Check that the device is zoned. If it is, get the list of zones and create
  1363. * sub-directories and files according to the device zone configuration and
  1364. * format options.
  1365. */
  1366. static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
  1367. {
  1368. struct zonefs_zone_data zd;
  1369. struct zonefs_sb_info *sbi;
  1370. struct inode *inode;
  1371. enum zonefs_ztype t;
  1372. int ret;
  1373. if (!bdev_is_zoned(sb->s_bdev)) {
  1374. zonefs_err(sb, "Not a zoned block device\n");
  1375. return -EINVAL;
  1376. }
  1377. /*
  1378. * Initialize super block information: the maximum file size is updated
  1379. * when the zone files are created so that the format option
  1380. * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
  1381. * beyond the zone size is taken into account.
  1382. */
  1383. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  1384. if (!sbi)
  1385. return -ENOMEM;
  1386. spin_lock_init(&sbi->s_lock);
  1387. sb->s_fs_info = sbi;
  1388. sb->s_magic = ZONEFS_MAGIC;
  1389. sb->s_maxbytes = 0;
  1390. sb->s_op = &zonefs_sops;
  1391. sb->s_time_gran = 1;
  1392. /*
  1393. * The block size is set to the device physical sector size to ensure
  1394. * that write operations on 512e devices (512B logical block and 4KB
  1395. * physical block) are always aligned to the device physical blocks,
  1396. * as mandated by the ZBC/ZAC specifications.
  1397. */
  1398. sb_set_blocksize(sb, bdev_physical_block_size(sb->s_bdev));
  1399. sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
  1400. sbi->s_uid = GLOBAL_ROOT_UID;
  1401. sbi->s_gid = GLOBAL_ROOT_GID;
  1402. sbi->s_perm = 0640;
  1403. sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
  1404. sbi->s_max_open_zones = bdev_max_open_zones(sb->s_bdev);
  1405. atomic_set(&sbi->s_open_zones, 0);
  1406. if (!sbi->s_max_open_zones &&
  1407. sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
  1408. zonefs_info(sb, "No open zones limit. Ignoring explicit_open mount option\n");
  1409. sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
  1410. }
  1411. ret = zonefs_read_super(sb);
  1412. if (ret)
  1413. return ret;
  1414. ret = zonefs_parse_options(sb, data);
  1415. if (ret)
  1416. return ret;
  1417. memset(&zd, 0, sizeof(struct zonefs_zone_data));
  1418. zd.sb = sb;
  1419. ret = zonefs_get_zone_info(&zd);
  1420. if (ret)
  1421. goto cleanup;
  1422. zonefs_info(sb, "Mounting %u zones",
  1423. blkdev_nr_zones(sb->s_bdev->bd_disk));
  1424. /* Create root directory inode */
  1425. ret = -ENOMEM;
  1426. inode = new_inode(sb);
  1427. if (!inode)
  1428. goto cleanup;
  1429. inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk);
  1430. inode->i_mode = S_IFDIR | 0555;
  1431. inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode);
  1432. inode->i_op = &zonefs_dir_inode_operations;
  1433. inode->i_fop = &simple_dir_operations;
  1434. set_nlink(inode, 2);
  1435. sb->s_root = d_make_root(inode);
  1436. if (!sb->s_root)
  1437. goto cleanup;
  1438. /* Create and populate files in zone groups directories */
  1439. for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
  1440. ret = zonefs_create_zgroup(&zd, t);
  1441. if (ret)
  1442. break;
  1443. }
  1444. cleanup:
  1445. zonefs_cleanup_zone_info(&zd);
  1446. return ret;
  1447. }
  1448. static struct dentry *zonefs_mount(struct file_system_type *fs_type,
  1449. int flags, const char *dev_name, void *data)
  1450. {
  1451. return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
  1452. }
  1453. static void zonefs_kill_super(struct super_block *sb)
  1454. {
  1455. struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
  1456. if (sb->s_root)
  1457. d_genocide(sb->s_root);
  1458. kill_block_super(sb);
  1459. kfree(sbi);
  1460. }
  1461. /*
  1462. * File system definition and registration.
  1463. */
  1464. static struct file_system_type zonefs_type = {
  1465. .owner = THIS_MODULE,
  1466. .name = "zonefs",
  1467. .mount = zonefs_mount,
  1468. .kill_sb = zonefs_kill_super,
  1469. .fs_flags = FS_REQUIRES_DEV,
  1470. };
  1471. static int __init zonefs_init_inodecache(void)
  1472. {
  1473. zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
  1474. sizeof(struct zonefs_inode_info), 0,
  1475. (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
  1476. NULL);
  1477. if (zonefs_inode_cachep == NULL)
  1478. return -ENOMEM;
  1479. return 0;
  1480. }
  1481. static void zonefs_destroy_inodecache(void)
  1482. {
  1483. /*
  1484. * Make sure all delayed rcu free inodes are flushed before we
  1485. * destroy the inode cache.
  1486. */
  1487. rcu_barrier();
  1488. kmem_cache_destroy(zonefs_inode_cachep);
  1489. }
  1490. static int __init zonefs_init(void)
  1491. {
  1492. int ret;
  1493. BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
  1494. ret = zonefs_init_inodecache();
  1495. if (ret)
  1496. return ret;
  1497. ret = register_filesystem(&zonefs_type);
  1498. if (ret) {
  1499. zonefs_destroy_inodecache();
  1500. return ret;
  1501. }
  1502. return 0;
  1503. }
  1504. static void __exit zonefs_exit(void)
  1505. {
  1506. zonefs_destroy_inodecache();
  1507. unregister_filesystem(&zonefs_type);
  1508. }
  1509. MODULE_AUTHOR("Damien Le Moal");
  1510. MODULE_DESCRIPTION("Zone file system for zoned block devices");
  1511. MODULE_LICENSE("GPL");
  1512. MODULE_ALIAS_FS("zonefs");
  1513. MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
  1514. module_init(zonefs_init);
  1515. module_exit(zonefs_exit);