inode.c 61 KB

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