inode.c 65 KB

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