inode.c 67 KB

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