inode.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * inode.c
  4. *
  5. * PURPOSE
  6. * Inode handling routines for the OSTA-UDF(tm) filesystem.
  7. *
  8. * COPYRIGHT
  9. * (C) 1998 Dave Boynton
  10. * (C) 1998-2004 Ben Fennema
  11. * (C) 1999-2000 Stelias Computing Inc
  12. *
  13. * HISTORY
  14. *
  15. * 10/04/98 dgb Added rudimentary directory functions
  16. * 10/07/98 Fully working udf_block_map! It works!
  17. * 11/25/98 bmap altered to better support extents
  18. * 12/06/98 blf partition support in udf_iget, udf_block_map
  19. * and udf_read_inode
  20. * 12/12/98 rewrote udf_block_map to handle next extents and descs across
  21. * block boundaries (which is not actually allowed)
  22. * 12/20/98 added support for strategy 4096
  23. * 03/07/99 rewrote udf_block_map (again)
  24. * New funcs, inode_bmap, udf_next_aext
  25. * 04/19/99 Support for writing device EA's for major/minor #
  26. */
  27. #include "udfdecl.h"
  28. #include <linux/mm.h>
  29. #include <linux/module.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/writeback.h>
  32. #include <linux/slab.h>
  33. #include <linux/crc-itu-t.h>
  34. #include <linux/mpage.h>
  35. #include <linux/uio.h>
  36. #include <linux/bio.h>
  37. #include "udf_i.h"
  38. #include "udf_sb.h"
  39. #define EXTENT_MERGE_SIZE 5
  40. #define FE_MAPPED_PERMS (FE_PERM_U_READ | FE_PERM_U_WRITE | FE_PERM_U_EXEC | \
  41. FE_PERM_G_READ | FE_PERM_G_WRITE | FE_PERM_G_EXEC | \
  42. FE_PERM_O_READ | FE_PERM_O_WRITE | FE_PERM_O_EXEC)
  43. #define FE_DELETE_PERMS (FE_PERM_U_DELETE | FE_PERM_G_DELETE | \
  44. FE_PERM_O_DELETE)
  45. struct udf_map_rq;
  46. static umode_t udf_convert_permissions(struct fileEntry *);
  47. static int udf_update_inode(struct inode *, int);
  48. static int udf_sync_inode(struct inode *inode);
  49. static int udf_alloc_i_data(struct inode *inode, size_t size);
  50. static int inode_getblk(struct inode *inode, struct udf_map_rq *map);
  51. static int udf_insert_aext(struct inode *, struct extent_position,
  52. struct kernel_lb_addr, uint32_t);
  53. static void udf_split_extents(struct inode *, int *, int, udf_pblk_t,
  54. struct kernel_long_ad *, int *);
  55. static void udf_prealloc_extents(struct inode *, int, int,
  56. struct kernel_long_ad *, int *);
  57. static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
  58. static int udf_update_extents(struct inode *, struct kernel_long_ad *, int,
  59. int, struct extent_position *);
  60. static int udf_get_block_wb(struct inode *inode, sector_t block,
  61. struct buffer_head *bh_result, int create);
  62. static void __udf_clear_extent_cache(struct inode *inode)
  63. {
  64. struct udf_inode_info *iinfo = UDF_I(inode);
  65. if (iinfo->cached_extent.lstart != -1) {
  66. brelse(iinfo->cached_extent.epos.bh);
  67. iinfo->cached_extent.lstart = -1;
  68. }
  69. }
  70. /* Invalidate extent cache */
  71. static void udf_clear_extent_cache(struct inode *inode)
  72. {
  73. struct udf_inode_info *iinfo = UDF_I(inode);
  74. spin_lock(&iinfo->i_extent_cache_lock);
  75. __udf_clear_extent_cache(inode);
  76. spin_unlock(&iinfo->i_extent_cache_lock);
  77. }
  78. /* Return contents of extent cache */
  79. static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
  80. loff_t *lbcount, struct extent_position *pos)
  81. {
  82. struct udf_inode_info *iinfo = UDF_I(inode);
  83. int ret = 0;
  84. spin_lock(&iinfo->i_extent_cache_lock);
  85. if ((iinfo->cached_extent.lstart <= bcount) &&
  86. (iinfo->cached_extent.lstart != -1)) {
  87. /* Cache hit */
  88. *lbcount = iinfo->cached_extent.lstart;
  89. memcpy(pos, &iinfo->cached_extent.epos,
  90. sizeof(struct extent_position));
  91. if (pos->bh)
  92. get_bh(pos->bh);
  93. ret = 1;
  94. }
  95. spin_unlock(&iinfo->i_extent_cache_lock);
  96. return ret;
  97. }
  98. /* Add extent to extent cache */
  99. static void udf_update_extent_cache(struct inode *inode, loff_t estart,
  100. struct extent_position *pos)
  101. {
  102. struct udf_inode_info *iinfo = UDF_I(inode);
  103. spin_lock(&iinfo->i_extent_cache_lock);
  104. /* Invalidate previously cached extent */
  105. __udf_clear_extent_cache(inode);
  106. if (pos->bh)
  107. get_bh(pos->bh);
  108. memcpy(&iinfo->cached_extent.epos, pos, sizeof(*pos));
  109. iinfo->cached_extent.lstart = estart;
  110. switch (iinfo->i_alloc_type) {
  111. case ICBTAG_FLAG_AD_SHORT:
  112. iinfo->cached_extent.epos.offset -= sizeof(struct short_ad);
  113. break;
  114. case ICBTAG_FLAG_AD_LONG:
  115. iinfo->cached_extent.epos.offset -= sizeof(struct long_ad);
  116. break;
  117. }
  118. spin_unlock(&iinfo->i_extent_cache_lock);
  119. }
  120. void udf_evict_inode(struct inode *inode)
  121. {
  122. struct udf_inode_info *iinfo = UDF_I(inode);
  123. int want_delete = 0;
  124. if (!is_bad_inode(inode)) {
  125. if (!inode->i_nlink) {
  126. want_delete = 1;
  127. udf_setsize(inode, 0);
  128. udf_update_inode(inode, IS_SYNC(inode));
  129. }
  130. if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
  131. inode->i_size != iinfo->i_lenExtents) {
  132. udf_warn(inode->i_sb,
  133. "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
  134. inode->i_ino, inode->i_mode,
  135. (unsigned long long)inode->i_size,
  136. (unsigned long long)iinfo->i_lenExtents);
  137. }
  138. }
  139. truncate_inode_pages_final(&inode->i_data);
  140. invalidate_inode_buffers(inode);
  141. clear_inode(inode);
  142. kfree(iinfo->i_data);
  143. iinfo->i_data = NULL;
  144. udf_clear_extent_cache(inode);
  145. if (want_delete) {
  146. udf_free_inode(inode);
  147. }
  148. }
  149. static void udf_write_failed(struct address_space *mapping, loff_t to)
  150. {
  151. struct inode *inode = mapping->host;
  152. struct udf_inode_info *iinfo = UDF_I(inode);
  153. loff_t isize = inode->i_size;
  154. if (to > isize) {
  155. truncate_pagecache(inode, isize);
  156. if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  157. down_write(&iinfo->i_data_sem);
  158. udf_clear_extent_cache(inode);
  159. udf_truncate_extents(inode);
  160. up_write(&iinfo->i_data_sem);
  161. }
  162. }
  163. }
  164. static int udf_handle_page_wb(struct folio *folio,
  165. struct writeback_control *wbc)
  166. {
  167. struct inode *inode = folio->mapping->host;
  168. struct udf_inode_info *iinfo = UDF_I(inode);
  169. /*
  170. * Inodes in the normal format are handled by the generic code. This
  171. * check is race-free as the folio lock protects us from inode type
  172. * conversion.
  173. */
  174. if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB)
  175. return 1;
  176. memcpy_from_file_folio(iinfo->i_data + iinfo->i_lenEAttr, folio,
  177. 0, i_size_read(inode));
  178. folio_unlock(folio);
  179. mark_inode_dirty(inode);
  180. return 0;
  181. }
  182. static int udf_writepages(struct address_space *mapping,
  183. struct writeback_control *wbc)
  184. {
  185. return __mpage_writepages(mapping, wbc, udf_get_block_wb,
  186. udf_handle_page_wb);
  187. }
  188. static void udf_adinicb_read_folio(struct folio *folio)
  189. {
  190. struct inode *inode = folio->mapping->host;
  191. struct udf_inode_info *iinfo = UDF_I(inode);
  192. loff_t isize = i_size_read(inode);
  193. folio_fill_tail(folio, 0, iinfo->i_data + iinfo->i_lenEAttr, isize);
  194. folio_mark_uptodate(folio);
  195. }
  196. static int udf_read_folio(struct file *file, struct folio *folio)
  197. {
  198. struct udf_inode_info *iinfo = UDF_I(file_inode(file));
  199. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  200. udf_adinicb_read_folio(folio);
  201. folio_unlock(folio);
  202. return 0;
  203. }
  204. return mpage_read_folio(folio, udf_get_block);
  205. }
  206. static void udf_readahead(struct readahead_control *rac)
  207. {
  208. struct udf_inode_info *iinfo = UDF_I(rac->mapping->host);
  209. /*
  210. * No readahead needed for in-ICB files and udf_get_block() would get
  211. * confused for such file anyway.
  212. */
  213. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  214. return;
  215. mpage_readahead(rac, udf_get_block);
  216. }
  217. static int udf_write_begin(const struct kiocb *iocb,
  218. struct address_space *mapping,
  219. loff_t pos, unsigned len,
  220. struct folio **foliop, void **fsdata)
  221. {
  222. struct file *file = iocb->ki_filp;
  223. struct udf_inode_info *iinfo = UDF_I(file_inode(file));
  224. struct folio *folio;
  225. int ret;
  226. if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  227. ret = block_write_begin(mapping, pos, len, foliop,
  228. udf_get_block);
  229. if (unlikely(ret))
  230. udf_write_failed(mapping, pos + len);
  231. return ret;
  232. }
  233. if (WARN_ON_ONCE(pos >= PAGE_SIZE))
  234. return -EIO;
  235. folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN,
  236. mapping_gfp_mask(mapping));
  237. if (IS_ERR(folio))
  238. return PTR_ERR(folio);
  239. *foliop = folio;
  240. if (!folio_test_uptodate(folio))
  241. udf_adinicb_read_folio(folio);
  242. return 0;
  243. }
  244. static int udf_write_end(const struct kiocb *iocb,
  245. struct address_space *mapping,
  246. loff_t pos, unsigned len, unsigned copied,
  247. struct folio *folio, void *fsdata)
  248. {
  249. struct inode *inode = file_inode(iocb->ki_filp);
  250. loff_t last_pos;
  251. if (UDF_I(inode)->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB)
  252. return generic_write_end(iocb, mapping, pos, len, copied, folio,
  253. fsdata);
  254. last_pos = pos + copied;
  255. if (last_pos > inode->i_size)
  256. i_size_write(inode, last_pos);
  257. folio_mark_dirty(folio);
  258. folio_unlock(folio);
  259. folio_put(folio);
  260. return copied;
  261. }
  262. static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  263. {
  264. struct file *file = iocb->ki_filp;
  265. struct address_space *mapping = file->f_mapping;
  266. struct inode *inode = mapping->host;
  267. size_t count = iov_iter_count(iter);
  268. ssize_t ret;
  269. /* Fallback to buffered IO for in-ICB files */
  270. if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  271. return 0;
  272. ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
  273. if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
  274. udf_write_failed(mapping, iocb->ki_pos + count);
  275. return ret;
  276. }
  277. static sector_t udf_bmap(struct address_space *mapping, sector_t block)
  278. {
  279. struct udf_inode_info *iinfo = UDF_I(mapping->host);
  280. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  281. return -EINVAL;
  282. return generic_block_bmap(mapping, block, udf_get_block);
  283. }
  284. const struct address_space_operations udf_aops = {
  285. .dirty_folio = block_dirty_folio,
  286. .invalidate_folio = block_invalidate_folio,
  287. .read_folio = udf_read_folio,
  288. .readahead = udf_readahead,
  289. .writepages = udf_writepages,
  290. .write_begin = udf_write_begin,
  291. .write_end = udf_write_end,
  292. .direct_IO = udf_direct_IO,
  293. .bmap = udf_bmap,
  294. .migrate_folio = buffer_migrate_folio,
  295. };
  296. /*
  297. * Expand file stored in ICB to a normal one-block-file
  298. *
  299. * This function requires i_mutex held
  300. */
  301. int udf_expand_file_adinicb(struct inode *inode)
  302. {
  303. struct folio *folio;
  304. struct udf_inode_info *iinfo = UDF_I(inode);
  305. int err;
  306. WARN_ON_ONCE(!inode_is_locked(inode));
  307. if (!iinfo->i_lenAlloc) {
  308. down_write(&iinfo->i_data_sem);
  309. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  310. iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
  311. else
  312. iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
  313. up_write(&iinfo->i_data_sem);
  314. mark_inode_dirty(inode);
  315. return 0;
  316. }
  317. folio = __filemap_get_folio(inode->i_mapping, 0,
  318. FGP_LOCK | FGP_ACCESSED | FGP_CREAT, GFP_KERNEL);
  319. if (IS_ERR(folio))
  320. return PTR_ERR(folio);
  321. if (!folio_test_uptodate(folio))
  322. udf_adinicb_read_folio(folio);
  323. down_write(&iinfo->i_data_sem);
  324. memset(iinfo->i_data + iinfo->i_lenEAttr, 0x00,
  325. iinfo->i_lenAlloc);
  326. iinfo->i_lenAlloc = 0;
  327. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  328. iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
  329. else
  330. iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
  331. folio_mark_dirty(folio);
  332. folio_unlock(folio);
  333. up_write(&iinfo->i_data_sem);
  334. err = filemap_fdatawrite(inode->i_mapping);
  335. if (err) {
  336. /* Restore everything back so that we don't lose data... */
  337. folio_lock(folio);
  338. down_write(&iinfo->i_data_sem);
  339. memcpy_from_folio(iinfo->i_data + iinfo->i_lenEAttr,
  340. folio, 0, inode->i_size);
  341. folio_unlock(folio);
  342. iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
  343. iinfo->i_lenAlloc = inode->i_size;
  344. up_write(&iinfo->i_data_sem);
  345. }
  346. folio_put(folio);
  347. mark_inode_dirty(inode);
  348. return err;
  349. }
  350. #define UDF_MAP_CREATE 0x01 /* Mapping can allocate new blocks */
  351. #define UDF_MAP_NOPREALLOC 0x02 /* Do not preallocate blocks */
  352. #define UDF_BLK_MAPPED 0x01 /* Block was successfully mapped */
  353. #define UDF_BLK_NEW 0x02 /* Block was freshly allocated */
  354. struct udf_map_rq {
  355. sector_t lblk;
  356. udf_pblk_t pblk;
  357. int iflags; /* UDF_MAP_ flags determining behavior */
  358. int oflags; /* UDF_BLK_ flags reporting results */
  359. };
  360. static int udf_map_block(struct inode *inode, struct udf_map_rq *map)
  361. {
  362. int ret;
  363. struct udf_inode_info *iinfo = UDF_I(inode);
  364. if (WARN_ON_ONCE(iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB))
  365. return -EFSCORRUPTED;
  366. map->oflags = 0;
  367. if (!(map->iflags & UDF_MAP_CREATE)) {
  368. struct kernel_lb_addr eloc;
  369. uint32_t elen;
  370. sector_t offset;
  371. struct extent_position epos = {};
  372. int8_t etype;
  373. down_read(&iinfo->i_data_sem);
  374. ret = inode_bmap(inode, map->lblk, &epos, &eloc, &elen, &offset,
  375. &etype);
  376. if (ret < 0)
  377. goto out_read;
  378. if (ret > 0 && etype == (EXT_RECORDED_ALLOCATED >> 30)) {
  379. map->pblk = udf_get_lb_pblock(inode->i_sb, &eloc,
  380. offset);
  381. map->oflags |= UDF_BLK_MAPPED;
  382. ret = 0;
  383. }
  384. out_read:
  385. up_read(&iinfo->i_data_sem);
  386. brelse(epos.bh);
  387. return ret;
  388. }
  389. down_write(&iinfo->i_data_sem);
  390. /*
  391. * Block beyond EOF and prealloc extents? Just discard preallocation
  392. * as it is not useful and complicates things.
  393. */
  394. if (((loff_t)map->lblk) << inode->i_blkbits >= iinfo->i_lenExtents)
  395. udf_discard_prealloc(inode);
  396. udf_clear_extent_cache(inode);
  397. ret = inode_getblk(inode, map);
  398. up_write(&iinfo->i_data_sem);
  399. return ret;
  400. }
  401. static int __udf_get_block(struct inode *inode, sector_t block,
  402. struct buffer_head *bh_result, int flags)
  403. {
  404. int err;
  405. struct udf_map_rq map = {
  406. .lblk = block,
  407. .iflags = flags,
  408. };
  409. err = udf_map_block(inode, &map);
  410. if (err < 0)
  411. return err;
  412. if (map.oflags & UDF_BLK_MAPPED) {
  413. map_bh(bh_result, inode->i_sb, map.pblk);
  414. if (map.oflags & UDF_BLK_NEW)
  415. set_buffer_new(bh_result);
  416. }
  417. return 0;
  418. }
  419. int udf_get_block(struct inode *inode, sector_t block,
  420. struct buffer_head *bh_result, int create)
  421. {
  422. int flags = create ? UDF_MAP_CREATE : 0;
  423. /*
  424. * We preallocate blocks only for regular files. It also makes sense
  425. * for directories but there's a problem when to drop the
  426. * preallocation. We might use some delayed work for that but I feel
  427. * it's overengineering for a filesystem like UDF.
  428. */
  429. if (!S_ISREG(inode->i_mode))
  430. flags |= UDF_MAP_NOPREALLOC;
  431. return __udf_get_block(inode, block, bh_result, flags);
  432. }
  433. /*
  434. * We shouldn't be allocating blocks on page writeback since we allocate them
  435. * on page fault. We can spot dirty buffers without allocated blocks though
  436. * when truncate expands file. These however don't have valid data so we can
  437. * safely ignore them. So never allocate blocks from page writeback.
  438. */
  439. static int udf_get_block_wb(struct inode *inode, sector_t block,
  440. struct buffer_head *bh_result, int create)
  441. {
  442. return __udf_get_block(inode, block, bh_result, 0);
  443. }
  444. /* Extend the file with new blocks totaling 'new_block_bytes',
  445. * return the number of extents added
  446. */
  447. static int udf_do_extend_file(struct inode *inode,
  448. struct extent_position *last_pos,
  449. struct kernel_long_ad *last_ext,
  450. loff_t new_block_bytes)
  451. {
  452. uint32_t add;
  453. int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  454. struct super_block *sb = inode->i_sb;
  455. struct udf_inode_info *iinfo;
  456. int err;
  457. /* The previous extent is fake and we should not extend by anything
  458. * - there's nothing to do... */
  459. if (!new_block_bytes && fake)
  460. return 0;
  461. iinfo = UDF_I(inode);
  462. /* Round the last extent up to a multiple of block size */
  463. if (last_ext->extLength & (sb->s_blocksize - 1)) {
  464. last_ext->extLength =
  465. (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
  466. (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
  467. sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
  468. iinfo->i_lenExtents =
  469. (iinfo->i_lenExtents + sb->s_blocksize - 1) &
  470. ~(sb->s_blocksize - 1);
  471. }
  472. add = 0;
  473. /* Can we merge with the previous extent? */
  474. if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
  475. EXT_NOT_RECORDED_NOT_ALLOCATED) {
  476. add = (1 << 30) - sb->s_blocksize -
  477. (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  478. if (add > new_block_bytes)
  479. add = new_block_bytes;
  480. new_block_bytes -= add;
  481. last_ext->extLength += add;
  482. }
  483. if (fake) {
  484. err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
  485. last_ext->extLength, 1);
  486. if (err < 0)
  487. goto out_err;
  488. count++;
  489. } else {
  490. struct kernel_lb_addr tmploc;
  491. uint32_t tmplen;
  492. int8_t tmptype;
  493. udf_write_aext(inode, last_pos, &last_ext->extLocation,
  494. last_ext->extLength, 1);
  495. /*
  496. * We've rewritten the last extent. If we are going to add
  497. * more extents, we may need to enter possible following
  498. * empty indirect extent.
  499. */
  500. if (new_block_bytes) {
  501. err = udf_next_aext(inode, last_pos, &tmploc, &tmplen,
  502. &tmptype, 0);
  503. if (err < 0)
  504. goto out_err;
  505. }
  506. }
  507. iinfo->i_lenExtents += add;
  508. /* Managed to do everything necessary? */
  509. if (!new_block_bytes)
  510. goto out;
  511. /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
  512. last_ext->extLocation.logicalBlockNum = 0;
  513. last_ext->extLocation.partitionReferenceNum = 0;
  514. add = (1 << 30) - sb->s_blocksize;
  515. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
  516. /* Create enough extents to cover the whole hole */
  517. while (new_block_bytes > add) {
  518. new_block_bytes -= add;
  519. err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
  520. last_ext->extLength, 1);
  521. if (err)
  522. goto out_err;
  523. iinfo->i_lenExtents += add;
  524. count++;
  525. }
  526. if (new_block_bytes) {
  527. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  528. new_block_bytes;
  529. err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
  530. last_ext->extLength, 1);
  531. if (err)
  532. goto out_err;
  533. iinfo->i_lenExtents += new_block_bytes;
  534. count++;
  535. }
  536. out:
  537. /* last_pos should point to the last written extent... */
  538. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  539. last_pos->offset -= sizeof(struct short_ad);
  540. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  541. last_pos->offset -= sizeof(struct long_ad);
  542. else
  543. return -EIO;
  544. return count;
  545. out_err:
  546. /* Remove extents we've created so far */
  547. udf_clear_extent_cache(inode);
  548. udf_truncate_extents(inode);
  549. return err;
  550. }
  551. /* Extend the final block of the file to final_block_len bytes */
  552. static void udf_do_extend_final_block(struct inode *inode,
  553. struct extent_position *last_pos,
  554. struct kernel_long_ad *last_ext,
  555. uint32_t new_elen)
  556. {
  557. uint32_t added_bytes;
  558. /*
  559. * Extent already large enough? It may be already rounded up to block
  560. * size...
  561. */
  562. if (new_elen <= (last_ext->extLength & UDF_EXTENT_LENGTH_MASK))
  563. return;
  564. added_bytes = new_elen - (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  565. last_ext->extLength += added_bytes;
  566. UDF_I(inode)->i_lenExtents += added_bytes;
  567. udf_write_aext(inode, last_pos, &last_ext->extLocation,
  568. last_ext->extLength, 1);
  569. }
  570. static int udf_extend_file(struct inode *inode, loff_t newsize)
  571. {
  572. struct extent_position epos;
  573. struct kernel_lb_addr eloc;
  574. uint32_t elen;
  575. int8_t etype;
  576. struct super_block *sb = inode->i_sb;
  577. sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
  578. loff_t new_elen;
  579. int adsize;
  580. struct udf_inode_info *iinfo = UDF_I(inode);
  581. struct kernel_long_ad extent;
  582. int err = 0;
  583. bool within_last_ext;
  584. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  585. adsize = sizeof(struct short_ad);
  586. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  587. adsize = sizeof(struct long_ad);
  588. else
  589. BUG();
  590. down_write(&iinfo->i_data_sem);
  591. /*
  592. * When creating hole in file, just don't bother with preserving
  593. * preallocation. It likely won't be very useful anyway.
  594. */
  595. udf_discard_prealloc(inode);
  596. err = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset, &etype);
  597. if (err < 0)
  598. goto out;
  599. within_last_ext = (err == 1);
  600. /* We don't expect extents past EOF... */
  601. WARN_ON_ONCE(within_last_ext &&
  602. elen > ((loff_t)offset + 1) << inode->i_blkbits);
  603. if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
  604. (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
  605. /* File has no extents at all or has empty last
  606. * indirect extent! Create a fake extent... */
  607. extent.extLocation.logicalBlockNum = 0;
  608. extent.extLocation.partitionReferenceNum = 0;
  609. extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
  610. } else {
  611. epos.offset -= adsize;
  612. err = udf_next_aext(inode, &epos, &extent.extLocation,
  613. &extent.extLength, &etype, 0);
  614. if (err <= 0)
  615. goto out;
  616. extent.extLength |= etype << 30;
  617. }
  618. new_elen = ((loff_t)offset << inode->i_blkbits) |
  619. (newsize & (sb->s_blocksize - 1));
  620. /* File has extent covering the new size (could happen when extending
  621. * inside a block)?
  622. */
  623. if (within_last_ext) {
  624. /* Extending file within the last file block */
  625. udf_do_extend_final_block(inode, &epos, &extent, new_elen);
  626. } else {
  627. err = udf_do_extend_file(inode, &epos, &extent, new_elen);
  628. }
  629. if (err < 0)
  630. goto out;
  631. err = 0;
  632. out:
  633. brelse(epos.bh);
  634. up_write(&iinfo->i_data_sem);
  635. return err;
  636. }
  637. static int inode_getblk(struct inode *inode, struct udf_map_rq *map)
  638. {
  639. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
  640. struct extent_position prev_epos, cur_epos, next_epos;
  641. int count = 0, startnum = 0, endnum = 0;
  642. uint32_t elen = 0, tmpelen;
  643. struct kernel_lb_addr eloc, tmpeloc;
  644. int c = 1;
  645. loff_t lbcount = 0, b_off = 0;
  646. udf_pblk_t newblocknum;
  647. sector_t offset = 0;
  648. int8_t etype, tmpetype;
  649. struct udf_inode_info *iinfo = UDF_I(inode);
  650. udf_pblk_t goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
  651. int lastblock = 0;
  652. bool isBeyondEOF = false;
  653. int ret = 0;
  654. prev_epos.offset = udf_file_entry_alloc_offset(inode);
  655. prev_epos.block = iinfo->i_location;
  656. prev_epos.bh = NULL;
  657. cur_epos = next_epos = prev_epos;
  658. b_off = (loff_t)map->lblk << inode->i_sb->s_blocksize_bits;
  659. /* find the extent which contains the block we are looking for.
  660. alternate between laarr[0] and laarr[1] for locations of the
  661. current extent, and the previous extent */
  662. do {
  663. if (prev_epos.bh != cur_epos.bh) {
  664. brelse(prev_epos.bh);
  665. get_bh(cur_epos.bh);
  666. prev_epos.bh = cur_epos.bh;
  667. }
  668. if (cur_epos.bh != next_epos.bh) {
  669. brelse(cur_epos.bh);
  670. get_bh(next_epos.bh);
  671. cur_epos.bh = next_epos.bh;
  672. }
  673. lbcount += elen;
  674. prev_epos.block = cur_epos.block;
  675. cur_epos.block = next_epos.block;
  676. prev_epos.offset = cur_epos.offset;
  677. cur_epos.offset = next_epos.offset;
  678. ret = udf_next_aext(inode, &next_epos, &eloc, &elen, &etype, 1);
  679. if (ret < 0) {
  680. goto out_free;
  681. } else if (ret == 0) {
  682. isBeyondEOF = true;
  683. break;
  684. }
  685. c = !c;
  686. laarr[c].extLength = (etype << 30) | elen;
  687. laarr[c].extLocation = eloc;
  688. if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  689. pgoal = eloc.logicalBlockNum +
  690. ((elen + inode->i_sb->s_blocksize - 1) >>
  691. inode->i_sb->s_blocksize_bits);
  692. count++;
  693. } while (lbcount + elen <= b_off);
  694. b_off -= lbcount;
  695. offset = b_off >> inode->i_sb->s_blocksize_bits;
  696. /*
  697. * Move prev_epos and cur_epos into indirect extent if we are at
  698. * the pointer to it
  699. */
  700. ret = udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, &tmpetype, 0);
  701. if (ret < 0)
  702. goto out_free;
  703. ret = udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, &tmpetype, 0);
  704. if (ret < 0)
  705. goto out_free;
  706. /* if the extent is allocated and recorded, return the block
  707. if the extent is not a multiple of the blocksize, round up */
  708. if (!isBeyondEOF && etype == (EXT_RECORDED_ALLOCATED >> 30)) {
  709. if (elen & (inode->i_sb->s_blocksize - 1)) {
  710. elen = EXT_RECORDED_ALLOCATED |
  711. ((elen + inode->i_sb->s_blocksize - 1) &
  712. ~(inode->i_sb->s_blocksize - 1));
  713. iinfo->i_lenExtents =
  714. ALIGN(iinfo->i_lenExtents,
  715. inode->i_sb->s_blocksize);
  716. udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
  717. }
  718. map->oflags = UDF_BLK_MAPPED;
  719. map->pblk = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
  720. ret = 0;
  721. goto out_free;
  722. }
  723. /* Are we beyond EOF and preallocated extent? */
  724. if (isBeyondEOF) {
  725. loff_t hole_len;
  726. if (count) {
  727. if (c)
  728. laarr[0] = laarr[1];
  729. startnum = 1;
  730. } else {
  731. /* Create a fake extent when there's not one */
  732. memset(&laarr[0].extLocation, 0x00,
  733. sizeof(struct kernel_lb_addr));
  734. laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
  735. /* Will udf_do_extend_file() create real extent from
  736. a fake one? */
  737. startnum = (offset > 0);
  738. }
  739. /* Create extents for the hole between EOF and offset */
  740. hole_len = (loff_t)offset << inode->i_blkbits;
  741. ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
  742. if (ret < 0)
  743. goto out_free;
  744. c = 0;
  745. offset = 0;
  746. count += ret;
  747. /*
  748. * Is there any real extent? - otherwise we overwrite the fake
  749. * one...
  750. */
  751. if (count)
  752. c = !c;
  753. laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  754. inode->i_sb->s_blocksize;
  755. memset(&laarr[c].extLocation, 0x00,
  756. sizeof(struct kernel_lb_addr));
  757. count++;
  758. endnum = c + 1;
  759. lastblock = 1;
  760. } else {
  761. endnum = startnum = ((count > 2) ? 2 : count);
  762. /* if the current extent is in position 0,
  763. swap it with the previous */
  764. if (!c && count != 1) {
  765. laarr[2] = laarr[0];
  766. laarr[0] = laarr[1];
  767. laarr[1] = laarr[2];
  768. c = 1;
  769. }
  770. /* if the current block is located in an extent,
  771. read the next extent */
  772. ret = udf_next_aext(inode, &next_epos, &eloc, &elen, &etype, 0);
  773. if (ret > 0) {
  774. laarr[c + 1].extLength = (etype << 30) | elen;
  775. laarr[c + 1].extLocation = eloc;
  776. count++;
  777. startnum++;
  778. endnum++;
  779. } else if (ret == 0)
  780. lastblock = 1;
  781. else
  782. goto out_free;
  783. }
  784. /* if the current extent is not recorded but allocated, get the
  785. * block in the extent corresponding to the requested block */
  786. if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  787. newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
  788. else { /* otherwise, allocate a new block */
  789. if (iinfo->i_next_alloc_block == map->lblk)
  790. goal = iinfo->i_next_alloc_goal;
  791. if (!goal) {
  792. if (!(goal = pgoal)) /* XXX: what was intended here? */
  793. goal = iinfo->i_location.logicalBlockNum + 1;
  794. }
  795. newblocknum = udf_new_block(inode->i_sb, inode,
  796. iinfo->i_location.partitionReferenceNum,
  797. goal, &ret);
  798. if (!newblocknum)
  799. goto out_free;
  800. if (isBeyondEOF)
  801. iinfo->i_lenExtents += inode->i_sb->s_blocksize;
  802. }
  803. /* if the extent the requsted block is located in contains multiple
  804. * blocks, split the extent into at most three extents. blocks prior
  805. * to requested block, requested block, and blocks after requested
  806. * block */
  807. udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
  808. if (!(map->iflags & UDF_MAP_NOPREALLOC))
  809. udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
  810. /* merge any continuous blocks in laarr */
  811. udf_merge_extents(inode, laarr, &endnum);
  812. /* write back the new extents, inserting new extents if the new number
  813. * of extents is greater than the old number, and deleting extents if
  814. * the new number of extents is less than the old number */
  815. ret = udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
  816. if (ret < 0)
  817. goto out_free;
  818. map->pblk = udf_get_pblock(inode->i_sb, newblocknum,
  819. iinfo->i_location.partitionReferenceNum, 0);
  820. if (!map->pblk) {
  821. ret = -EFSCORRUPTED;
  822. goto out_free;
  823. }
  824. map->oflags = UDF_BLK_NEW | UDF_BLK_MAPPED;
  825. iinfo->i_next_alloc_block = map->lblk + 1;
  826. iinfo->i_next_alloc_goal = newblocknum + 1;
  827. inode_set_ctime_current(inode);
  828. if (IS_SYNC(inode))
  829. udf_sync_inode(inode);
  830. else
  831. mark_inode_dirty(inode);
  832. ret = 0;
  833. out_free:
  834. brelse(prev_epos.bh);
  835. brelse(cur_epos.bh);
  836. brelse(next_epos.bh);
  837. return ret;
  838. }
  839. static void udf_split_extents(struct inode *inode, int *c, int offset,
  840. udf_pblk_t newblocknum,
  841. struct kernel_long_ad *laarr, int *endnum)
  842. {
  843. unsigned long blocksize = inode->i_sb->s_blocksize;
  844. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  845. if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
  846. (laarr[*c].extLength >> 30) ==
  847. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
  848. int curr = *c;
  849. int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
  850. blocksize - 1) >> blocksize_bits;
  851. int8_t etype = (laarr[curr].extLength >> 30);
  852. if (blen == 1)
  853. ;
  854. else if (!offset || blen == offset + 1) {
  855. laarr[curr + 2] = laarr[curr + 1];
  856. laarr[curr + 1] = laarr[curr];
  857. } else {
  858. laarr[curr + 3] = laarr[curr + 1];
  859. laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
  860. }
  861. if (offset) {
  862. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  863. udf_free_blocks(inode->i_sb, inode,
  864. &laarr[curr].extLocation,
  865. 0, offset);
  866. laarr[curr].extLength =
  867. EXT_NOT_RECORDED_NOT_ALLOCATED |
  868. (offset << blocksize_bits);
  869. laarr[curr].extLocation.logicalBlockNum = 0;
  870. laarr[curr].extLocation.
  871. partitionReferenceNum = 0;
  872. } else
  873. laarr[curr].extLength = (etype << 30) |
  874. (offset << blocksize_bits);
  875. curr++;
  876. (*c)++;
  877. (*endnum)++;
  878. }
  879. laarr[curr].extLocation.logicalBlockNum = newblocknum;
  880. if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  881. laarr[curr].extLocation.partitionReferenceNum =
  882. UDF_I(inode)->i_location.partitionReferenceNum;
  883. laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
  884. blocksize;
  885. curr++;
  886. if (blen != offset + 1) {
  887. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  888. laarr[curr].extLocation.logicalBlockNum +=
  889. offset + 1;
  890. laarr[curr].extLength = (etype << 30) |
  891. ((blen - (offset + 1)) << blocksize_bits);
  892. curr++;
  893. (*endnum)++;
  894. }
  895. }
  896. }
  897. static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
  898. struct kernel_long_ad *laarr,
  899. int *endnum)
  900. {
  901. int start, length = 0, currlength = 0, i;
  902. if (*endnum >= (c + 1)) {
  903. if (!lastblock)
  904. return;
  905. else
  906. start = c;
  907. } else {
  908. if ((laarr[c + 1].extLength >> 30) ==
  909. (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  910. start = c + 1;
  911. length = currlength =
  912. (((laarr[c + 1].extLength &
  913. UDF_EXTENT_LENGTH_MASK) +
  914. inode->i_sb->s_blocksize - 1) >>
  915. inode->i_sb->s_blocksize_bits);
  916. } else
  917. start = c;
  918. }
  919. for (i = start + 1; i <= *endnum; i++) {
  920. if (i == *endnum) {
  921. if (lastblock)
  922. length += UDF_DEFAULT_PREALLOC_BLOCKS;
  923. } else if ((laarr[i].extLength >> 30) ==
  924. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
  925. length += (((laarr[i].extLength &
  926. UDF_EXTENT_LENGTH_MASK) +
  927. inode->i_sb->s_blocksize - 1) >>
  928. inode->i_sb->s_blocksize_bits);
  929. } else
  930. break;
  931. }
  932. if (length) {
  933. int next = laarr[start].extLocation.logicalBlockNum +
  934. (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
  935. inode->i_sb->s_blocksize - 1) >>
  936. inode->i_sb->s_blocksize_bits);
  937. int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
  938. laarr[start].extLocation.partitionReferenceNum,
  939. next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
  940. length : UDF_DEFAULT_PREALLOC_BLOCKS) -
  941. currlength);
  942. if (numalloc) {
  943. if (start == (c + 1))
  944. laarr[start].extLength +=
  945. (numalloc <<
  946. inode->i_sb->s_blocksize_bits);
  947. else {
  948. memmove(&laarr[c + 2], &laarr[c + 1],
  949. sizeof(struct long_ad) * (*endnum - (c + 1)));
  950. (*endnum)++;
  951. laarr[c + 1].extLocation.logicalBlockNum = next;
  952. laarr[c + 1].extLocation.partitionReferenceNum =
  953. laarr[c].extLocation.
  954. partitionReferenceNum;
  955. laarr[c + 1].extLength =
  956. EXT_NOT_RECORDED_ALLOCATED |
  957. (numalloc <<
  958. inode->i_sb->s_blocksize_bits);
  959. start = c + 1;
  960. }
  961. for (i = start + 1; numalloc && i < *endnum; i++) {
  962. int elen = ((laarr[i].extLength &
  963. UDF_EXTENT_LENGTH_MASK) +
  964. inode->i_sb->s_blocksize - 1) >>
  965. inode->i_sb->s_blocksize_bits;
  966. if (elen > numalloc) {
  967. laarr[i].extLength -=
  968. (numalloc <<
  969. inode->i_sb->s_blocksize_bits);
  970. numalloc = 0;
  971. } else {
  972. numalloc -= elen;
  973. if (*endnum > (i + 1))
  974. memmove(&laarr[i],
  975. &laarr[i + 1],
  976. sizeof(struct long_ad) *
  977. (*endnum - (i + 1)));
  978. i--;
  979. (*endnum)--;
  980. }
  981. }
  982. UDF_I(inode)->i_lenExtents +=
  983. numalloc << inode->i_sb->s_blocksize_bits;
  984. }
  985. }
  986. }
  987. static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
  988. int *endnum)
  989. {
  990. int i;
  991. unsigned long blocksize = inode->i_sb->s_blocksize;
  992. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  993. for (i = 0; i < (*endnum - 1); i++) {
  994. struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
  995. struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
  996. if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
  997. (((li->extLength >> 30) ==
  998. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
  999. ((lip1->extLocation.logicalBlockNum -
  1000. li->extLocation.logicalBlockNum) ==
  1001. (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  1002. blocksize - 1) >> blocksize_bits)))) {
  1003. if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  1004. (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
  1005. blocksize - 1) <= UDF_EXTENT_LENGTH_MASK) {
  1006. li->extLength = lip1->extLength +
  1007. (((li->extLength &
  1008. UDF_EXTENT_LENGTH_MASK) +
  1009. blocksize - 1) & ~(blocksize - 1));
  1010. if (*endnum > (i + 2))
  1011. memmove(&laarr[i + 1], &laarr[i + 2],
  1012. sizeof(struct long_ad) *
  1013. (*endnum - (i + 2)));
  1014. i--;
  1015. (*endnum)--;
  1016. }
  1017. } else if (((li->extLength >> 30) ==
  1018. (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
  1019. ((lip1->extLength >> 30) ==
  1020. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
  1021. udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
  1022. ((li->extLength &
  1023. UDF_EXTENT_LENGTH_MASK) +
  1024. blocksize - 1) >> blocksize_bits);
  1025. li->extLocation.logicalBlockNum = 0;
  1026. li->extLocation.partitionReferenceNum = 0;
  1027. if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  1028. (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
  1029. blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
  1030. lip1->extLength = (lip1->extLength -
  1031. (li->extLength &
  1032. UDF_EXTENT_LENGTH_MASK) +
  1033. UDF_EXTENT_LENGTH_MASK) &
  1034. ~(blocksize - 1);
  1035. li->extLength = (li->extLength &
  1036. UDF_EXTENT_FLAG_MASK) +
  1037. (UDF_EXTENT_LENGTH_MASK + 1) -
  1038. blocksize;
  1039. } else {
  1040. li->extLength = lip1->extLength +
  1041. (((li->extLength &
  1042. UDF_EXTENT_LENGTH_MASK) +
  1043. blocksize - 1) & ~(blocksize - 1));
  1044. if (*endnum > (i + 2))
  1045. memmove(&laarr[i + 1], &laarr[i + 2],
  1046. sizeof(struct long_ad) *
  1047. (*endnum - (i + 2)));
  1048. i--;
  1049. (*endnum)--;
  1050. }
  1051. } else if ((li->extLength >> 30) ==
  1052. (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  1053. udf_free_blocks(inode->i_sb, inode,
  1054. &li->extLocation, 0,
  1055. ((li->extLength &
  1056. UDF_EXTENT_LENGTH_MASK) +
  1057. blocksize - 1) >> blocksize_bits);
  1058. li->extLocation.logicalBlockNum = 0;
  1059. li->extLocation.partitionReferenceNum = 0;
  1060. li->extLength = (li->extLength &
  1061. UDF_EXTENT_LENGTH_MASK) |
  1062. EXT_NOT_RECORDED_NOT_ALLOCATED;
  1063. }
  1064. }
  1065. }
  1066. static int udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
  1067. int startnum, int endnum,
  1068. struct extent_position *epos)
  1069. {
  1070. int start = 0, i;
  1071. struct kernel_lb_addr tmploc;
  1072. uint32_t tmplen;
  1073. int8_t tmpetype;
  1074. int err;
  1075. if (startnum > endnum) {
  1076. for (i = 0; i < (startnum - endnum); i++)
  1077. udf_delete_aext(inode, *epos);
  1078. } else if (startnum < endnum) {
  1079. for (i = 0; i < (endnum - startnum); i++) {
  1080. err = udf_insert_aext(inode, *epos,
  1081. laarr[i].extLocation,
  1082. laarr[i].extLength);
  1083. /*
  1084. * If we fail here, we are likely corrupting the extent
  1085. * list and leaking blocks. At least stop early to
  1086. * limit the damage.
  1087. */
  1088. if (err < 0)
  1089. return err;
  1090. err = udf_next_aext(inode, epos, &laarr[i].extLocation,
  1091. &laarr[i].extLength, &tmpetype, 1);
  1092. if (err < 0)
  1093. return err;
  1094. start++;
  1095. }
  1096. }
  1097. for (i = start; i < endnum; i++) {
  1098. err = udf_next_aext(inode, epos, &tmploc, &tmplen, &tmpetype, 0);
  1099. if (err < 0)
  1100. return err;
  1101. udf_write_aext(inode, epos, &laarr[i].extLocation,
  1102. laarr[i].extLength, 1);
  1103. }
  1104. return 0;
  1105. }
  1106. struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block,
  1107. int create, int *err)
  1108. {
  1109. struct buffer_head *bh = NULL;
  1110. struct udf_map_rq map = {
  1111. .lblk = block,
  1112. .iflags = UDF_MAP_NOPREALLOC | (create ? UDF_MAP_CREATE : 0),
  1113. };
  1114. *err = udf_map_block(inode, &map);
  1115. if (*err || !(map.oflags & UDF_BLK_MAPPED))
  1116. return NULL;
  1117. bh = sb_getblk(inode->i_sb, map.pblk);
  1118. if (!bh) {
  1119. *err = -ENOMEM;
  1120. return NULL;
  1121. }
  1122. if (map.oflags & UDF_BLK_NEW) {
  1123. lock_buffer(bh);
  1124. memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
  1125. set_buffer_uptodate(bh);
  1126. unlock_buffer(bh);
  1127. mark_buffer_dirty_inode(bh, inode);
  1128. return bh;
  1129. }
  1130. if (bh_read(bh, 0) >= 0)
  1131. return bh;
  1132. brelse(bh);
  1133. *err = -EIO;
  1134. return NULL;
  1135. }
  1136. int udf_setsize(struct inode *inode, loff_t newsize)
  1137. {
  1138. int err = 0;
  1139. struct udf_inode_info *iinfo;
  1140. unsigned int bsize = i_blocksize(inode);
  1141. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1142. S_ISLNK(inode->i_mode)))
  1143. return -EINVAL;
  1144. iinfo = UDF_I(inode);
  1145. if (newsize > inode->i_size) {
  1146. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  1147. if (bsize >=
  1148. (udf_file_entry_alloc_offset(inode) + newsize)) {
  1149. down_write(&iinfo->i_data_sem);
  1150. iinfo->i_lenAlloc = newsize;
  1151. up_write(&iinfo->i_data_sem);
  1152. goto set_size;
  1153. }
  1154. err = udf_expand_file_adinicb(inode);
  1155. if (err)
  1156. return err;
  1157. }
  1158. err = udf_extend_file(inode, newsize);
  1159. if (err)
  1160. return err;
  1161. set_size:
  1162. truncate_setsize(inode, newsize);
  1163. } else {
  1164. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  1165. down_write(&iinfo->i_data_sem);
  1166. udf_clear_extent_cache(inode);
  1167. memset(iinfo->i_data + iinfo->i_lenEAttr + newsize,
  1168. 0x00, bsize - newsize -
  1169. udf_file_entry_alloc_offset(inode));
  1170. iinfo->i_lenAlloc = newsize;
  1171. truncate_setsize(inode, newsize);
  1172. up_write(&iinfo->i_data_sem);
  1173. goto update_time;
  1174. }
  1175. err = block_truncate_page(inode->i_mapping, newsize,
  1176. udf_get_block);
  1177. if (err)
  1178. return err;
  1179. truncate_setsize(inode, newsize);
  1180. down_write(&iinfo->i_data_sem);
  1181. udf_clear_extent_cache(inode);
  1182. err = udf_truncate_extents(inode);
  1183. up_write(&iinfo->i_data_sem);
  1184. if (err)
  1185. return err;
  1186. }
  1187. update_time:
  1188. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  1189. if (IS_SYNC(inode))
  1190. udf_sync_inode(inode);
  1191. else
  1192. mark_inode_dirty(inode);
  1193. return err;
  1194. }
  1195. /*
  1196. * Maximum length of linked list formed by ICB hierarchy. The chosen number is
  1197. * arbitrary - just that we hopefully don't limit any real use of rewritten
  1198. * inode on write-once media but avoid looping for too long on corrupted media.
  1199. */
  1200. #define UDF_MAX_ICB_NESTING 1024
  1201. static int udf_read_inode(struct inode *inode, bool hidden_inode)
  1202. {
  1203. struct buffer_head *bh = NULL;
  1204. struct fileEntry *fe;
  1205. struct extendedFileEntry *efe;
  1206. uint16_t ident;
  1207. struct udf_inode_info *iinfo = UDF_I(inode);
  1208. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1209. struct kernel_lb_addr *iloc = &iinfo->i_location;
  1210. unsigned int link_count;
  1211. unsigned int indirections = 0;
  1212. int bs = inode->i_sb->s_blocksize;
  1213. int ret = -EIO;
  1214. uint32_t uid, gid;
  1215. struct timespec64 ts;
  1216. reread:
  1217. if (iloc->partitionReferenceNum >= sbi->s_partitions) {
  1218. udf_debug("partition reference: %u > logical volume partitions: %u\n",
  1219. iloc->partitionReferenceNum, sbi->s_partitions);
  1220. return -EIO;
  1221. }
  1222. if (iloc->logicalBlockNum >=
  1223. sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
  1224. udf_debug("block=%u, partition=%u out of range\n",
  1225. iloc->logicalBlockNum, iloc->partitionReferenceNum);
  1226. return -EIO;
  1227. }
  1228. /*
  1229. * Set defaults, but the inode is still incomplete!
  1230. * Note: get_new_inode() sets the following on a new inode:
  1231. * i_sb = sb
  1232. * i_no = ino
  1233. * i_flags = sb->s_flags
  1234. * i_state = 0
  1235. * clean_inode(): zero fills and sets
  1236. * i_count = 1
  1237. * i_nlink = 1
  1238. * i_op = NULL;
  1239. */
  1240. bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
  1241. if (!bh) {
  1242. udf_err(inode->i_sb, "(ino %lu) failed !bh\n", inode->i_ino);
  1243. return -EIO;
  1244. }
  1245. if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
  1246. ident != TAG_IDENT_USE) {
  1247. udf_err(inode->i_sb, "(ino %lu) failed ident=%u\n",
  1248. inode->i_ino, ident);
  1249. goto out;
  1250. }
  1251. fe = (struct fileEntry *)bh->b_data;
  1252. efe = (struct extendedFileEntry *)bh->b_data;
  1253. if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
  1254. struct buffer_head *ibh;
  1255. ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
  1256. if (ident == TAG_IDENT_IE && ibh) {
  1257. struct kernel_lb_addr loc;
  1258. struct indirectEntry *ie;
  1259. ie = (struct indirectEntry *)ibh->b_data;
  1260. loc = lelb_to_cpu(ie->indirectICB.extLocation);
  1261. if (ie->indirectICB.extLength) {
  1262. brelse(ibh);
  1263. memcpy(&iinfo->i_location, &loc,
  1264. sizeof(struct kernel_lb_addr));
  1265. if (++indirections > UDF_MAX_ICB_NESTING) {
  1266. udf_err(inode->i_sb,
  1267. "too many ICBs in ICB hierarchy"
  1268. " (max %d supported)\n",
  1269. UDF_MAX_ICB_NESTING);
  1270. goto out;
  1271. }
  1272. brelse(bh);
  1273. goto reread;
  1274. }
  1275. }
  1276. brelse(ibh);
  1277. } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
  1278. udf_err(inode->i_sb, "unsupported strategy type: %u\n",
  1279. le16_to_cpu(fe->icbTag.strategyType));
  1280. goto out;
  1281. }
  1282. if (fe->icbTag.strategyType == cpu_to_le16(4))
  1283. iinfo->i_strat4096 = 0;
  1284. else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
  1285. iinfo->i_strat4096 = 1;
  1286. iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
  1287. ICBTAG_FLAG_AD_MASK;
  1288. if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
  1289. iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
  1290. iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  1291. ret = -EIO;
  1292. goto out;
  1293. }
  1294. iinfo->i_hidden = hidden_inode;
  1295. iinfo->i_unique = 0;
  1296. iinfo->i_lenEAttr = 0;
  1297. iinfo->i_lenExtents = 0;
  1298. iinfo->i_lenAlloc = 0;
  1299. iinfo->i_next_alloc_block = 0;
  1300. iinfo->i_next_alloc_goal = 0;
  1301. if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
  1302. iinfo->i_efe = 1;
  1303. iinfo->i_use = 0;
  1304. ret = udf_alloc_i_data(inode, bs -
  1305. sizeof(struct extendedFileEntry));
  1306. if (ret)
  1307. goto out;
  1308. memcpy(iinfo->i_data,
  1309. bh->b_data + sizeof(struct extendedFileEntry),
  1310. bs - sizeof(struct extendedFileEntry));
  1311. } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
  1312. iinfo->i_efe = 0;
  1313. iinfo->i_use = 0;
  1314. ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
  1315. if (ret)
  1316. goto out;
  1317. memcpy(iinfo->i_data,
  1318. bh->b_data + sizeof(struct fileEntry),
  1319. bs - sizeof(struct fileEntry));
  1320. } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
  1321. iinfo->i_efe = 0;
  1322. iinfo->i_use = 1;
  1323. iinfo->i_lenAlloc = le32_to_cpu(
  1324. ((struct unallocSpaceEntry *)bh->b_data)->
  1325. lengthAllocDescs);
  1326. ret = udf_alloc_i_data(inode, bs -
  1327. sizeof(struct unallocSpaceEntry));
  1328. if (ret)
  1329. goto out;
  1330. memcpy(iinfo->i_data,
  1331. bh->b_data + sizeof(struct unallocSpaceEntry),
  1332. bs - sizeof(struct unallocSpaceEntry));
  1333. return 0;
  1334. }
  1335. ret = -EIO;
  1336. read_lock(&sbi->s_cred_lock);
  1337. uid = le32_to_cpu(fe->uid);
  1338. if (uid == UDF_INVALID_ID ||
  1339. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
  1340. inode->i_uid = sbi->s_uid;
  1341. else
  1342. i_uid_write(inode, uid);
  1343. gid = le32_to_cpu(fe->gid);
  1344. if (gid == UDF_INVALID_ID ||
  1345. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
  1346. inode->i_gid = sbi->s_gid;
  1347. else
  1348. i_gid_write(inode, gid);
  1349. if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
  1350. sbi->s_fmode != UDF_INVALID_MODE)
  1351. inode->i_mode = sbi->s_fmode;
  1352. else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
  1353. sbi->s_dmode != UDF_INVALID_MODE)
  1354. inode->i_mode = sbi->s_dmode;
  1355. else
  1356. inode->i_mode = udf_convert_permissions(fe);
  1357. inode->i_mode &= ~sbi->s_umask;
  1358. iinfo->i_extraPerms = le32_to_cpu(fe->permissions) & ~FE_MAPPED_PERMS;
  1359. read_unlock(&sbi->s_cred_lock);
  1360. link_count = le16_to_cpu(fe->fileLinkCount);
  1361. if (!link_count) {
  1362. if (!hidden_inode) {
  1363. ret = -ESTALE;
  1364. goto out;
  1365. }
  1366. link_count = 1;
  1367. }
  1368. set_nlink(inode, link_count);
  1369. inode->i_size = le64_to_cpu(fe->informationLength);
  1370. iinfo->i_lenExtents = inode->i_size;
  1371. if (iinfo->i_efe == 0) {
  1372. inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
  1373. (inode->i_sb->s_blocksize_bits - 9);
  1374. udf_disk_stamp_to_time(&ts, fe->accessTime);
  1375. inode_set_atime_to_ts(inode, ts);
  1376. udf_disk_stamp_to_time(&ts, fe->modificationTime);
  1377. inode_set_mtime_to_ts(inode, ts);
  1378. udf_disk_stamp_to_time(&ts, fe->attrTime);
  1379. inode_set_ctime_to_ts(inode, ts);
  1380. iinfo->i_unique = le64_to_cpu(fe->uniqueID);
  1381. iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
  1382. iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
  1383. iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
  1384. iinfo->i_streamdir = 0;
  1385. iinfo->i_lenStreams = 0;
  1386. } else {
  1387. inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
  1388. (inode->i_sb->s_blocksize_bits - 9);
  1389. udf_disk_stamp_to_time(&ts, efe->accessTime);
  1390. inode_set_atime_to_ts(inode, ts);
  1391. udf_disk_stamp_to_time(&ts, efe->modificationTime);
  1392. inode_set_mtime_to_ts(inode, ts);
  1393. udf_disk_stamp_to_time(&ts, efe->attrTime);
  1394. inode_set_ctime_to_ts(inode, ts);
  1395. udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime);
  1396. iinfo->i_unique = le64_to_cpu(efe->uniqueID);
  1397. iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
  1398. iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
  1399. iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
  1400. /* Named streams */
  1401. iinfo->i_streamdir = (efe->streamDirectoryICB.extLength != 0);
  1402. iinfo->i_locStreamdir =
  1403. lelb_to_cpu(efe->streamDirectoryICB.extLocation);
  1404. iinfo->i_lenStreams = le64_to_cpu(efe->objectSize);
  1405. if (iinfo->i_lenStreams >= inode->i_size)
  1406. iinfo->i_lenStreams -= inode->i_size;
  1407. else
  1408. iinfo->i_lenStreams = 0;
  1409. }
  1410. inode->i_generation = iinfo->i_unique;
  1411. /*
  1412. * Sanity check length of allocation descriptors and extended attrs to
  1413. * avoid integer overflows
  1414. */
  1415. if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
  1416. goto out;
  1417. /* Now do exact checks */
  1418. if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
  1419. goto out;
  1420. /* Sanity checks for files in ICB so that we don't get confused later */
  1421. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  1422. /*
  1423. * For file in ICB data is stored in allocation descriptor
  1424. * so sizes should match
  1425. */
  1426. if (iinfo->i_lenAlloc != inode->i_size)
  1427. goto out;
  1428. /* File in ICB has to fit in there... */
  1429. if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
  1430. goto out;
  1431. }
  1432. switch (fe->icbTag.fileType) {
  1433. case ICBTAG_FILE_TYPE_DIRECTORY:
  1434. inode->i_op = &udf_dir_inode_operations;
  1435. inode->i_fop = &udf_dir_operations;
  1436. inode->i_mode |= S_IFDIR;
  1437. inc_nlink(inode);
  1438. break;
  1439. case ICBTAG_FILE_TYPE_REALTIME:
  1440. case ICBTAG_FILE_TYPE_REGULAR:
  1441. case ICBTAG_FILE_TYPE_UNDEF:
  1442. case ICBTAG_FILE_TYPE_VAT20:
  1443. inode->i_data.a_ops = &udf_aops;
  1444. inode->i_op = &udf_file_inode_operations;
  1445. inode->i_fop = &udf_file_operations;
  1446. inode->i_mode |= S_IFREG;
  1447. break;
  1448. case ICBTAG_FILE_TYPE_BLOCK:
  1449. inode->i_mode |= S_IFBLK;
  1450. break;
  1451. case ICBTAG_FILE_TYPE_CHAR:
  1452. inode->i_mode |= S_IFCHR;
  1453. break;
  1454. case ICBTAG_FILE_TYPE_FIFO:
  1455. init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
  1456. break;
  1457. case ICBTAG_FILE_TYPE_SOCKET:
  1458. init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
  1459. break;
  1460. case ICBTAG_FILE_TYPE_SYMLINK:
  1461. inode->i_data.a_ops = &udf_symlink_aops;
  1462. inode->i_op = &udf_symlink_inode_operations;
  1463. inode_nohighmem(inode);
  1464. inode->i_mode = S_IFLNK | 0777;
  1465. break;
  1466. case ICBTAG_FILE_TYPE_MAIN:
  1467. udf_debug("METADATA FILE-----\n");
  1468. break;
  1469. case ICBTAG_FILE_TYPE_MIRROR:
  1470. udf_debug("METADATA MIRROR FILE-----\n");
  1471. break;
  1472. case ICBTAG_FILE_TYPE_BITMAP:
  1473. udf_debug("METADATA BITMAP FILE-----\n");
  1474. break;
  1475. default:
  1476. udf_err(inode->i_sb, "(ino %lu) failed unknown file type=%u\n",
  1477. inode->i_ino, fe->icbTag.fileType);
  1478. goto out;
  1479. }
  1480. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1481. struct deviceSpec *dsea =
  1482. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1483. if (dsea) {
  1484. init_special_inode(inode, inode->i_mode,
  1485. MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
  1486. le32_to_cpu(dsea->minorDeviceIdent)));
  1487. /* Developer ID ??? */
  1488. } else
  1489. goto out;
  1490. }
  1491. ret = 0;
  1492. out:
  1493. brelse(bh);
  1494. return ret;
  1495. }
  1496. static int udf_alloc_i_data(struct inode *inode, size_t size)
  1497. {
  1498. struct udf_inode_info *iinfo = UDF_I(inode);
  1499. iinfo->i_data = kmalloc(size, GFP_KERNEL);
  1500. if (!iinfo->i_data)
  1501. return -ENOMEM;
  1502. return 0;
  1503. }
  1504. static umode_t udf_convert_permissions(struct fileEntry *fe)
  1505. {
  1506. umode_t mode;
  1507. uint32_t permissions;
  1508. uint32_t flags;
  1509. permissions = le32_to_cpu(fe->permissions);
  1510. flags = le16_to_cpu(fe->icbTag.flags);
  1511. mode = ((permissions) & 0007) |
  1512. ((permissions >> 2) & 0070) |
  1513. ((permissions >> 4) & 0700) |
  1514. ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
  1515. ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
  1516. ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
  1517. return mode;
  1518. }
  1519. void udf_update_extra_perms(struct inode *inode, umode_t mode)
  1520. {
  1521. struct udf_inode_info *iinfo = UDF_I(inode);
  1522. /*
  1523. * UDF 2.01 sec. 3.3.3.3 Note 2:
  1524. * In Unix, delete permission tracks write
  1525. */
  1526. iinfo->i_extraPerms &= ~FE_DELETE_PERMS;
  1527. if (mode & 0200)
  1528. iinfo->i_extraPerms |= FE_PERM_U_DELETE;
  1529. if (mode & 0020)
  1530. iinfo->i_extraPerms |= FE_PERM_G_DELETE;
  1531. if (mode & 0002)
  1532. iinfo->i_extraPerms |= FE_PERM_O_DELETE;
  1533. }
  1534. int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
  1535. {
  1536. return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1537. }
  1538. static int udf_sync_inode(struct inode *inode)
  1539. {
  1540. return udf_update_inode(inode, 1);
  1541. }
  1542. static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec64 time)
  1543. {
  1544. if (iinfo->i_crtime.tv_sec > time.tv_sec ||
  1545. (iinfo->i_crtime.tv_sec == time.tv_sec &&
  1546. iinfo->i_crtime.tv_nsec > time.tv_nsec))
  1547. iinfo->i_crtime = time;
  1548. }
  1549. static int udf_update_inode(struct inode *inode, int do_sync)
  1550. {
  1551. struct buffer_head *bh = NULL;
  1552. struct fileEntry *fe;
  1553. struct extendedFileEntry *efe;
  1554. uint64_t lb_recorded;
  1555. uint32_t udfperms;
  1556. uint16_t icbflags;
  1557. uint16_t crclen;
  1558. int err = 0;
  1559. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1560. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  1561. struct udf_inode_info *iinfo = UDF_I(inode);
  1562. bh = sb_getblk(inode->i_sb,
  1563. udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
  1564. if (!bh) {
  1565. udf_debug("getblk failure\n");
  1566. return -EIO;
  1567. }
  1568. lock_buffer(bh);
  1569. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  1570. fe = (struct fileEntry *)bh->b_data;
  1571. efe = (struct extendedFileEntry *)bh->b_data;
  1572. if (iinfo->i_use) {
  1573. struct unallocSpaceEntry *use =
  1574. (struct unallocSpaceEntry *)bh->b_data;
  1575. use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1576. memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
  1577. iinfo->i_data, inode->i_sb->s_blocksize -
  1578. sizeof(struct unallocSpaceEntry));
  1579. use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
  1580. crclen = sizeof(struct unallocSpaceEntry);
  1581. goto finish;
  1582. }
  1583. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
  1584. fe->uid = cpu_to_le32(UDF_INVALID_ID);
  1585. else
  1586. fe->uid = cpu_to_le32(i_uid_read(inode));
  1587. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
  1588. fe->gid = cpu_to_le32(UDF_INVALID_ID);
  1589. else
  1590. fe->gid = cpu_to_le32(i_gid_read(inode));
  1591. udfperms = ((inode->i_mode & 0007)) |
  1592. ((inode->i_mode & 0070) << 2) |
  1593. ((inode->i_mode & 0700) << 4);
  1594. udfperms |= iinfo->i_extraPerms;
  1595. fe->permissions = cpu_to_le32(udfperms);
  1596. if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
  1597. fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
  1598. else {
  1599. if (iinfo->i_hidden)
  1600. fe->fileLinkCount = cpu_to_le16(0);
  1601. else
  1602. fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
  1603. }
  1604. fe->informationLength = cpu_to_le64(inode->i_size);
  1605. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1606. struct regid *eid;
  1607. struct deviceSpec *dsea =
  1608. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1609. if (!dsea) {
  1610. dsea = (struct deviceSpec *)
  1611. udf_add_extendedattr(inode,
  1612. sizeof(struct deviceSpec) +
  1613. sizeof(struct regid), 12, 0x3);
  1614. dsea->attrType = cpu_to_le32(12);
  1615. dsea->attrSubtype = 1;
  1616. dsea->attrLength = cpu_to_le32(
  1617. sizeof(struct deviceSpec) +
  1618. sizeof(struct regid));
  1619. dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
  1620. }
  1621. eid = (struct regid *)dsea->impUse;
  1622. memset(eid, 0, sizeof(*eid));
  1623. strcpy(eid->ident, UDF_ID_DEVELOPER);
  1624. eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
  1625. eid->identSuffix[1] = UDF_OS_ID_LINUX;
  1626. dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
  1627. dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
  1628. }
  1629. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  1630. lb_recorded = 0; /* No extents => no blocks! */
  1631. else
  1632. lb_recorded =
  1633. (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
  1634. (blocksize_bits - 9);
  1635. if (iinfo->i_efe == 0) {
  1636. memcpy(bh->b_data + sizeof(struct fileEntry),
  1637. iinfo->i_data,
  1638. inode->i_sb->s_blocksize - sizeof(struct fileEntry));
  1639. fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
  1640. udf_time_to_disk_stamp(&fe->accessTime, inode_get_atime(inode));
  1641. udf_time_to_disk_stamp(&fe->modificationTime, inode_get_mtime(inode));
  1642. udf_time_to_disk_stamp(&fe->attrTime, inode_get_ctime(inode));
  1643. memset(&(fe->impIdent), 0, sizeof(struct regid));
  1644. strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
  1645. fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1646. fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1647. fe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1648. fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1649. fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1650. fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
  1651. fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
  1652. crclen = sizeof(struct fileEntry);
  1653. } else {
  1654. memcpy(bh->b_data + sizeof(struct extendedFileEntry),
  1655. iinfo->i_data,
  1656. inode->i_sb->s_blocksize -
  1657. sizeof(struct extendedFileEntry));
  1658. efe->objectSize =
  1659. cpu_to_le64(inode->i_size + iinfo->i_lenStreams);
  1660. efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
  1661. if (iinfo->i_streamdir) {
  1662. struct long_ad *icb_lad = &efe->streamDirectoryICB;
  1663. icb_lad->extLocation =
  1664. cpu_to_lelb(iinfo->i_locStreamdir);
  1665. icb_lad->extLength =
  1666. cpu_to_le32(inode->i_sb->s_blocksize);
  1667. }
  1668. udf_adjust_time(iinfo, inode_get_atime(inode));
  1669. udf_adjust_time(iinfo, inode_get_mtime(inode));
  1670. udf_adjust_time(iinfo, inode_get_ctime(inode));
  1671. udf_time_to_disk_stamp(&efe->accessTime,
  1672. inode_get_atime(inode));
  1673. udf_time_to_disk_stamp(&efe->modificationTime,
  1674. inode_get_mtime(inode));
  1675. udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
  1676. udf_time_to_disk_stamp(&efe->attrTime, inode_get_ctime(inode));
  1677. memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
  1678. strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
  1679. efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1680. efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1681. efe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1682. efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1683. efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1684. efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
  1685. efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
  1686. crclen = sizeof(struct extendedFileEntry);
  1687. }
  1688. finish:
  1689. if (iinfo->i_strat4096) {
  1690. fe->icbTag.strategyType = cpu_to_le16(4096);
  1691. fe->icbTag.strategyParameter = cpu_to_le16(1);
  1692. fe->icbTag.numEntries = cpu_to_le16(2);
  1693. } else {
  1694. fe->icbTag.strategyType = cpu_to_le16(4);
  1695. fe->icbTag.numEntries = cpu_to_le16(1);
  1696. }
  1697. if (iinfo->i_use)
  1698. fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
  1699. else if (S_ISDIR(inode->i_mode))
  1700. fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
  1701. else if (S_ISREG(inode->i_mode))
  1702. fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
  1703. else if (S_ISLNK(inode->i_mode))
  1704. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
  1705. else if (S_ISBLK(inode->i_mode))
  1706. fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
  1707. else if (S_ISCHR(inode->i_mode))
  1708. fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
  1709. else if (S_ISFIFO(inode->i_mode))
  1710. fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
  1711. else if (S_ISSOCK(inode->i_mode))
  1712. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
  1713. icbflags = iinfo->i_alloc_type |
  1714. ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
  1715. ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
  1716. ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
  1717. (le16_to_cpu(fe->icbTag.flags) &
  1718. ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
  1719. ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
  1720. fe->icbTag.flags = cpu_to_le16(icbflags);
  1721. if (sbi->s_udfrev >= 0x0200)
  1722. fe->descTag.descVersion = cpu_to_le16(3);
  1723. else
  1724. fe->descTag.descVersion = cpu_to_le16(2);
  1725. fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
  1726. fe->descTag.tagLocation = cpu_to_le32(
  1727. iinfo->i_location.logicalBlockNum);
  1728. crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
  1729. fe->descTag.descCRCLength = cpu_to_le16(crclen);
  1730. fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
  1731. crclen));
  1732. fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
  1733. set_buffer_uptodate(bh);
  1734. unlock_buffer(bh);
  1735. /* write the data blocks */
  1736. mark_buffer_dirty(bh);
  1737. if (do_sync) {
  1738. sync_dirty_buffer(bh);
  1739. if (buffer_write_io_error(bh)) {
  1740. udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
  1741. inode->i_ino);
  1742. err = -EIO;
  1743. }
  1744. }
  1745. brelse(bh);
  1746. return err;
  1747. }
  1748. struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
  1749. bool hidden_inode)
  1750. {
  1751. unsigned long block = udf_get_lb_pblock(sb, ino, 0);
  1752. struct inode *inode = iget_locked(sb, block);
  1753. int err;
  1754. if (!inode)
  1755. return ERR_PTR(-ENOMEM);
  1756. if (!(inode_state_read_once(inode) & I_NEW)) {
  1757. if (UDF_I(inode)->i_hidden != hidden_inode) {
  1758. iput(inode);
  1759. return ERR_PTR(-EFSCORRUPTED);
  1760. }
  1761. return inode;
  1762. }
  1763. memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
  1764. err = udf_read_inode(inode, hidden_inode);
  1765. if (err < 0) {
  1766. iget_failed(inode);
  1767. return ERR_PTR(err);
  1768. }
  1769. unlock_new_inode(inode);
  1770. return inode;
  1771. }
  1772. int udf_setup_indirect_aext(struct inode *inode, udf_pblk_t block,
  1773. struct extent_position *epos)
  1774. {
  1775. struct super_block *sb = inode->i_sb;
  1776. struct buffer_head *bh;
  1777. struct allocExtDesc *aed;
  1778. struct extent_position nepos;
  1779. struct kernel_lb_addr neloc;
  1780. int ver, adsize;
  1781. int err = 0;
  1782. if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1783. adsize = sizeof(struct short_ad);
  1784. else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1785. adsize = sizeof(struct long_ad);
  1786. else
  1787. return -EIO;
  1788. neloc.logicalBlockNum = block;
  1789. neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
  1790. bh = sb_getblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
  1791. if (!bh)
  1792. return -EIO;
  1793. lock_buffer(bh);
  1794. memset(bh->b_data, 0x00, sb->s_blocksize);
  1795. set_buffer_uptodate(bh);
  1796. unlock_buffer(bh);
  1797. mark_buffer_dirty_inode(bh, inode);
  1798. aed = (struct allocExtDesc *)(bh->b_data);
  1799. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
  1800. aed->previousAllocExtLocation =
  1801. cpu_to_le32(epos->block.logicalBlockNum);
  1802. }
  1803. aed->lengthAllocDescs = cpu_to_le32(0);
  1804. if (UDF_SB(sb)->s_udfrev >= 0x0200)
  1805. ver = 3;
  1806. else
  1807. ver = 2;
  1808. udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
  1809. sizeof(struct tag));
  1810. nepos.block = neloc;
  1811. nepos.offset = sizeof(struct allocExtDesc);
  1812. nepos.bh = bh;
  1813. /*
  1814. * Do we have to copy current last extent to make space for indirect
  1815. * one?
  1816. */
  1817. if (epos->offset + adsize > sb->s_blocksize) {
  1818. struct kernel_lb_addr cp_loc;
  1819. uint32_t cp_len;
  1820. int8_t cp_type;
  1821. epos->offset -= adsize;
  1822. err = udf_current_aext(inode, epos, &cp_loc, &cp_len, &cp_type, 0);
  1823. if (err <= 0)
  1824. goto err_out;
  1825. cp_len |= ((uint32_t)cp_type) << 30;
  1826. __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
  1827. udf_write_aext(inode, epos, &nepos.block,
  1828. sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
  1829. } else {
  1830. __udf_add_aext(inode, epos, &nepos.block,
  1831. sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
  1832. }
  1833. brelse(epos->bh);
  1834. *epos = nepos;
  1835. return 0;
  1836. err_out:
  1837. brelse(bh);
  1838. return err;
  1839. }
  1840. /*
  1841. * Append extent at the given position - should be the first free one in inode
  1842. * / indirect extent. This function assumes there is enough space in the inode
  1843. * or indirect extent. Use udf_add_aext() if you didn't check for this before.
  1844. */
  1845. int __udf_add_aext(struct inode *inode, struct extent_position *epos,
  1846. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1847. {
  1848. struct udf_inode_info *iinfo = UDF_I(inode);
  1849. struct allocExtDesc *aed;
  1850. int adsize;
  1851. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1852. adsize = sizeof(struct short_ad);
  1853. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1854. adsize = sizeof(struct long_ad);
  1855. else
  1856. return -EIO;
  1857. if (!epos->bh) {
  1858. WARN_ON(iinfo->i_lenAlloc !=
  1859. epos->offset - udf_file_entry_alloc_offset(inode));
  1860. } else {
  1861. aed = (struct allocExtDesc *)epos->bh->b_data;
  1862. WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
  1863. epos->offset - sizeof(struct allocExtDesc));
  1864. WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
  1865. }
  1866. udf_write_aext(inode, epos, eloc, elen, inc);
  1867. if (!epos->bh) {
  1868. iinfo->i_lenAlloc += adsize;
  1869. mark_inode_dirty(inode);
  1870. } else {
  1871. aed = (struct allocExtDesc *)epos->bh->b_data;
  1872. le32_add_cpu(&aed->lengthAllocDescs, adsize);
  1873. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1874. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1875. udf_update_tag(epos->bh->b_data,
  1876. epos->offset + (inc ? 0 : adsize));
  1877. else
  1878. udf_update_tag(epos->bh->b_data,
  1879. sizeof(struct allocExtDesc));
  1880. mark_buffer_dirty_inode(epos->bh, inode);
  1881. }
  1882. return 0;
  1883. }
  1884. /*
  1885. * Append extent at given position - should be the first free one in inode
  1886. * / indirect extent. Takes care of allocating and linking indirect blocks.
  1887. */
  1888. int udf_add_aext(struct inode *inode, struct extent_position *epos,
  1889. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1890. {
  1891. int adsize;
  1892. struct super_block *sb = inode->i_sb;
  1893. if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1894. adsize = sizeof(struct short_ad);
  1895. else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1896. adsize = sizeof(struct long_ad);
  1897. else
  1898. return -EIO;
  1899. if (epos->offset + (2 * adsize) > sb->s_blocksize) {
  1900. int err;
  1901. udf_pblk_t new_block;
  1902. new_block = udf_new_block(sb, NULL,
  1903. epos->block.partitionReferenceNum,
  1904. epos->block.logicalBlockNum, &err);
  1905. if (!new_block)
  1906. return -ENOSPC;
  1907. err = udf_setup_indirect_aext(inode, new_block, epos);
  1908. if (err)
  1909. return err;
  1910. }
  1911. return __udf_add_aext(inode, epos, eloc, elen, inc);
  1912. }
  1913. void udf_write_aext(struct inode *inode, struct extent_position *epos,
  1914. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1915. {
  1916. int adsize;
  1917. uint8_t *ptr;
  1918. struct short_ad *sad;
  1919. struct long_ad *lad;
  1920. struct udf_inode_info *iinfo = UDF_I(inode);
  1921. if (!epos->bh)
  1922. ptr = iinfo->i_data + epos->offset -
  1923. udf_file_entry_alloc_offset(inode) +
  1924. iinfo->i_lenEAttr;
  1925. else
  1926. ptr = epos->bh->b_data + epos->offset;
  1927. switch (iinfo->i_alloc_type) {
  1928. case ICBTAG_FLAG_AD_SHORT:
  1929. sad = (struct short_ad *)ptr;
  1930. sad->extLength = cpu_to_le32(elen);
  1931. sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
  1932. adsize = sizeof(struct short_ad);
  1933. break;
  1934. case ICBTAG_FLAG_AD_LONG:
  1935. lad = (struct long_ad *)ptr;
  1936. lad->extLength = cpu_to_le32(elen);
  1937. lad->extLocation = cpu_to_lelb(*eloc);
  1938. memset(lad->impUse, 0x00, sizeof(lad->impUse));
  1939. adsize = sizeof(struct long_ad);
  1940. break;
  1941. default:
  1942. return;
  1943. }
  1944. if (epos->bh) {
  1945. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1946. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
  1947. struct allocExtDesc *aed =
  1948. (struct allocExtDesc *)epos->bh->b_data;
  1949. udf_update_tag(epos->bh->b_data,
  1950. le32_to_cpu(aed->lengthAllocDescs) +
  1951. sizeof(struct allocExtDesc));
  1952. }
  1953. mark_buffer_dirty_inode(epos->bh, inode);
  1954. } else {
  1955. mark_inode_dirty(inode);
  1956. }
  1957. if (inc)
  1958. epos->offset += adsize;
  1959. }
  1960. /*
  1961. * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
  1962. * someone does some weird stuff.
  1963. */
  1964. #define UDF_MAX_INDIR_EXTS 16
  1965. /*
  1966. * Returns 1 on success, -errno on error, 0 on hit EOF.
  1967. */
  1968. int udf_next_aext(struct inode *inode, struct extent_position *epos,
  1969. struct kernel_lb_addr *eloc, uint32_t *elen, int8_t *etype,
  1970. int inc)
  1971. {
  1972. unsigned int indirections = 0;
  1973. int ret = 0;
  1974. udf_pblk_t block;
  1975. while (1) {
  1976. ret = udf_current_aext(inode, epos, eloc, elen,
  1977. etype, inc);
  1978. if (ret <= 0)
  1979. return ret;
  1980. if (*etype != (EXT_NEXT_EXTENT_ALLOCDESCS >> 30))
  1981. return ret;
  1982. if (++indirections > UDF_MAX_INDIR_EXTS) {
  1983. udf_err(inode->i_sb,
  1984. "too many indirect extents in inode %lu\n",
  1985. inode->i_ino);
  1986. return -EFSCORRUPTED;
  1987. }
  1988. epos->block = *eloc;
  1989. epos->offset = sizeof(struct allocExtDesc);
  1990. brelse(epos->bh);
  1991. block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
  1992. epos->bh = sb_bread(inode->i_sb, block);
  1993. if (!epos->bh) {
  1994. udf_debug("reading block %u failed!\n", block);
  1995. return -EIO;
  1996. }
  1997. }
  1998. }
  1999. /*
  2000. * Returns 1 on success, -errno on error, 0 on hit EOF.
  2001. */
  2002. int udf_current_aext(struct inode *inode, struct extent_position *epos,
  2003. struct kernel_lb_addr *eloc, uint32_t *elen, int8_t *etype,
  2004. int inc)
  2005. {
  2006. int alen;
  2007. uint8_t *ptr;
  2008. struct short_ad *sad;
  2009. struct long_ad *lad;
  2010. struct udf_inode_info *iinfo = UDF_I(inode);
  2011. if (!epos->bh) {
  2012. if (!epos->offset)
  2013. epos->offset = udf_file_entry_alloc_offset(inode);
  2014. ptr = iinfo->i_data + epos->offset -
  2015. udf_file_entry_alloc_offset(inode) +
  2016. iinfo->i_lenEAttr;
  2017. alen = udf_file_entry_alloc_offset(inode) +
  2018. iinfo->i_lenAlloc;
  2019. } else {
  2020. struct allocExtDesc *header =
  2021. (struct allocExtDesc *)epos->bh->b_data;
  2022. if (!epos->offset)
  2023. epos->offset = sizeof(struct allocExtDesc);
  2024. ptr = epos->bh->b_data + epos->offset;
  2025. if (check_add_overflow(sizeof(struct allocExtDesc),
  2026. le32_to_cpu(header->lengthAllocDescs), &alen))
  2027. return -1;
  2028. if (alen > epos->bh->b_size)
  2029. return -1;
  2030. }
  2031. switch (iinfo->i_alloc_type) {
  2032. case ICBTAG_FLAG_AD_SHORT:
  2033. sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
  2034. if (!sad)
  2035. return 0;
  2036. *etype = le32_to_cpu(sad->extLength) >> 30;
  2037. eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
  2038. eloc->partitionReferenceNum =
  2039. iinfo->i_location.partitionReferenceNum;
  2040. *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
  2041. break;
  2042. case ICBTAG_FLAG_AD_LONG:
  2043. lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
  2044. if (!lad)
  2045. return 0;
  2046. *etype = le32_to_cpu(lad->extLength) >> 30;
  2047. *eloc = lelb_to_cpu(lad->extLocation);
  2048. *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
  2049. break;
  2050. default:
  2051. udf_debug("alloc_type = %u unsupported\n", iinfo->i_alloc_type);
  2052. return -EINVAL;
  2053. }
  2054. return 1;
  2055. }
  2056. static int udf_insert_aext(struct inode *inode, struct extent_position epos,
  2057. struct kernel_lb_addr neloc, uint32_t nelen)
  2058. {
  2059. struct kernel_lb_addr oeloc;
  2060. uint32_t oelen;
  2061. int8_t etype;
  2062. int ret;
  2063. if (epos.bh)
  2064. get_bh(epos.bh);
  2065. while (1) {
  2066. ret = udf_next_aext(inode, &epos, &oeloc, &oelen, &etype, 0);
  2067. if (ret <= 0)
  2068. break;
  2069. udf_write_aext(inode, &epos, &neloc, nelen, 1);
  2070. neloc = oeloc;
  2071. nelen = (etype << 30) | oelen;
  2072. }
  2073. if (ret == 0)
  2074. ret = udf_add_aext(inode, &epos, &neloc, nelen, 1);
  2075. brelse(epos.bh);
  2076. return ret;
  2077. }
  2078. int8_t udf_delete_aext(struct inode *inode, struct extent_position epos)
  2079. {
  2080. struct extent_position oepos;
  2081. int adsize;
  2082. int8_t etype;
  2083. struct allocExtDesc *aed;
  2084. struct udf_inode_info *iinfo;
  2085. struct kernel_lb_addr eloc;
  2086. uint32_t elen;
  2087. int ret;
  2088. if (epos.bh) {
  2089. get_bh(epos.bh);
  2090. get_bh(epos.bh);
  2091. }
  2092. iinfo = UDF_I(inode);
  2093. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  2094. adsize = sizeof(struct short_ad);
  2095. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  2096. adsize = sizeof(struct long_ad);
  2097. else
  2098. adsize = 0;
  2099. oepos = epos;
  2100. if (udf_next_aext(inode, &epos, &eloc, &elen, &etype, 1) <= 0)
  2101. return -1;
  2102. while (1) {
  2103. ret = udf_next_aext(inode, &epos, &eloc, &elen, &etype, 1);
  2104. if (ret < 0) {
  2105. brelse(epos.bh);
  2106. brelse(oepos.bh);
  2107. return -1;
  2108. }
  2109. if (ret == 0)
  2110. break;
  2111. udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
  2112. if (oepos.bh != epos.bh) {
  2113. oepos.block = epos.block;
  2114. brelse(oepos.bh);
  2115. get_bh(epos.bh);
  2116. oepos.bh = epos.bh;
  2117. oepos.offset = epos.offset - adsize;
  2118. }
  2119. }
  2120. memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
  2121. elen = 0;
  2122. if (epos.bh != oepos.bh) {
  2123. udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
  2124. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  2125. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  2126. if (!oepos.bh) {
  2127. iinfo->i_lenAlloc -= (adsize * 2);
  2128. mark_inode_dirty(inode);
  2129. } else {
  2130. aed = (struct allocExtDesc *)oepos.bh->b_data;
  2131. le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
  2132. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  2133. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  2134. udf_update_tag(oepos.bh->b_data,
  2135. oepos.offset - (2 * adsize));
  2136. else
  2137. udf_update_tag(oepos.bh->b_data,
  2138. sizeof(struct allocExtDesc));
  2139. mark_buffer_dirty_inode(oepos.bh, inode);
  2140. }
  2141. } else {
  2142. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  2143. if (!oepos.bh) {
  2144. iinfo->i_lenAlloc -= adsize;
  2145. mark_inode_dirty(inode);
  2146. } else {
  2147. aed = (struct allocExtDesc *)oepos.bh->b_data;
  2148. le32_add_cpu(&aed->lengthAllocDescs, -adsize);
  2149. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  2150. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  2151. udf_update_tag(oepos.bh->b_data,
  2152. epos.offset - adsize);
  2153. else
  2154. udf_update_tag(oepos.bh->b_data,
  2155. sizeof(struct allocExtDesc));
  2156. mark_buffer_dirty_inode(oepos.bh, inode);
  2157. }
  2158. }
  2159. brelse(epos.bh);
  2160. brelse(oepos.bh);
  2161. return (elen >> 30);
  2162. }
  2163. /*
  2164. * Returns 1 on success, -errno on error, 0 on hit EOF.
  2165. */
  2166. int inode_bmap(struct inode *inode, sector_t block, struct extent_position *pos,
  2167. struct kernel_lb_addr *eloc, uint32_t *elen, sector_t *offset,
  2168. int8_t *etype)
  2169. {
  2170. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  2171. loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
  2172. struct udf_inode_info *iinfo;
  2173. int err = 0;
  2174. iinfo = UDF_I(inode);
  2175. if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
  2176. pos->offset = 0;
  2177. pos->block = iinfo->i_location;
  2178. pos->bh = NULL;
  2179. }
  2180. *elen = 0;
  2181. do {
  2182. err = udf_next_aext(inode, pos, eloc, elen, etype, 1);
  2183. if (err <= 0) {
  2184. if (err == 0) {
  2185. *offset = (bcount - lbcount) >> blocksize_bits;
  2186. iinfo->i_lenExtents = lbcount;
  2187. }
  2188. return err;
  2189. }
  2190. lbcount += *elen;
  2191. } while (lbcount <= bcount);
  2192. /* update extent cache */
  2193. udf_update_extent_cache(inode, lbcount - *elen, pos);
  2194. *offset = (bcount + *elen - lbcount) >> blocksize_bits;
  2195. return 1;
  2196. }