inode.c 58 KB

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